|
| 1 | +# BakedFileSystem Benchmarks |
| 2 | + |
| 3 | +Comprehensive benchmarking suite for comparing BakedFileSystem (compile-time asset embedding) against traditional File I/O approaches. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This benchmark suite measures four key aspects of BakedFileSystem performance: |
| 8 | + |
| 9 | +1. **Compile Time** - Overhead introduced by asset embedding during compilation |
| 10 | +2. **Binary Size** - Impact on compiled binary size and compression efficiency |
| 11 | +3. **Memory Usage** - Runtime memory footprint (RSS) at various stages |
| 12 | +4. **Performance** - Request latency and throughput for serving static files |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +### Run All Benchmarks |
| 17 | + |
| 18 | +```bash |
| 19 | +./run_all.sh |
| 20 | +``` |
| 21 | + |
| 22 | +This will: |
| 23 | +1. Run all four benchmark categories in sequence |
| 24 | +2. Generate JSON result files in `results/` |
| 25 | +3. Create a comprehensive markdown report at `results/REPORT.md` |
| 26 | + |
| 27 | +**Expected Duration:** ~5-10 minutes depending on your system |
| 28 | + |
| 29 | +### View Results |
| 30 | + |
| 31 | +```bash |
| 32 | +cat results/REPORT.md |
| 33 | +``` |
| 34 | + |
| 35 | +## Individual Benchmarks |
| 36 | + |
| 37 | +You can run benchmarks individually: |
| 38 | + |
| 39 | +### Compile Time Benchmark |
| 40 | + |
| 41 | +Measures compilation time overhead: |
| 42 | + |
| 43 | +```bash |
| 44 | +crystal run compile_time.cr |
| 45 | +``` |
| 46 | + |
| 47 | +- **Output:** `results/compile_time.json` |
| 48 | +- **Duration:** ~2-3 minutes (5 compile iterations per configuration) |
| 49 | + |
| 50 | +### Binary Size Analysis |
| 51 | + |
| 52 | +Analyzes compiled binary sizes and compression ratios: |
| 53 | + |
| 54 | +```bash |
| 55 | +crystal run binary_size.cr |
| 56 | +``` |
| 57 | + |
| 58 | +- **Output:** `results/binary_size.json` |
| 59 | +- **Duration:** ~30 seconds |
| 60 | + |
| 61 | +### Memory Usage Benchmark |
| 62 | + |
| 63 | +Profiles runtime memory usage (RSS): |
| 64 | + |
| 65 | +```bash |
| 66 | +crystal run memory.cr |
| 67 | +``` |
| 68 | + |
| 69 | +- **Output:** `results/memory.json` |
| 70 | +- **Duration:** ~30 seconds |
| 71 | +- **Note:** Starts both servers temporarily on ports 3000 and 3001 |
| 72 | + |
| 73 | +### Performance Benchmark |
| 74 | + |
| 75 | +Measures request latency and throughput: |
| 76 | + |
| 77 | +```bash |
| 78 | +crystal run performance.cr |
| 79 | +``` |
| 80 | + |
| 81 | +- **Output:** `results/performance.json` |
| 82 | +- **Duration:** ~2-3 minutes |
| 83 | +- **Note:** Runs 1000 requests per file size with 10 concurrent clients |
| 84 | + |
| 85 | +## Test Applications |
| 86 | + |
| 87 | +The benchmarks compare two identical Kemal web applications: |
| 88 | + |
| 89 | +### Baseline App (`baseline/`) |
| 90 | + |
| 91 | +Traditional approach using File I/O: |
| 92 | +- Uses `send_file` to serve files from disk |
| 93 | +- No compile-time overhead |
| 94 | +- Smaller binary size |
| 95 | +- Disk I/O on each request |
| 96 | + |
| 97 | +### Baked App (`baked/`) |
| 98 | + |
| 99 | +BakedFileSystem approach: |
| 100 | +- Uses `bake_folder` to embed assets at compile time |
| 101 | +- Assets compressed with gzip automatically |
| 102 | +- Larger binary size (includes embedded assets) |
| 103 | +- Zero disk I/O - assets served from memory |
| 104 | + |
| 105 | +## Test Assets |
| 106 | + |
| 107 | +Both applications serve the same test files from `public/`: |
| 108 | + |
| 109 | +- **small.txt** - ~1 KB text file |
| 110 | +- **medium.json** - ~100 KB JSON file |
| 111 | +- **large.dat** - ~1 MB binary data file |
| 112 | + |
| 113 | +These represent typical small/medium/large static assets in web applications. |
| 114 | + |
| 115 | +## Results Structure |
| 116 | + |
| 117 | +After running benchmarks, results are stored in `results/`: |
| 118 | + |
| 119 | +``` |
| 120 | +results/ |
| 121 | +├── compile_time.json # Compilation time data |
| 122 | +├── binary_size.json # Binary size analysis |
| 123 | +├── memory.json # Memory usage profiles |
| 124 | +├── performance.json # Latency and throughput data |
| 125 | +└── REPORT.md # Comprehensive markdown report |
| 126 | +``` |
| 127 | + |
| 128 | +### JSON Schema |
| 129 | + |
| 130 | +Each JSON file contains structured data with timestamps, system info, and benchmark-specific metrics. See individual benchmark scripts for schema details. |
| 131 | + |
| 132 | +## Report Generator |
| 133 | + |
| 134 | +The report generator aggregates all JSON results into a readable markdown report: |
| 135 | + |
| 136 | +```bash |
| 137 | +crystal run report_generator.cr |
| 138 | +``` |
| 139 | + |
| 140 | +The report includes: |
| 141 | +- Executive summary with key findings |
| 142 | +- System specifications |
| 143 | +- Methodology overview |
| 144 | +- Detailed results for each benchmark category |
| 145 | +- Visual comparisons (ASCII bar charts) |
| 146 | +- Conclusions and recommendations |
| 147 | + |
| 148 | +## Requirements |
| 149 | + |
| 150 | +- Crystal 1.0.0 or higher |
| 151 | +- Kemal web framework |
| 152 | +- Unix-like OS (macOS, Linux) for process monitoring |
| 153 | +- Ports 3000 and 3001 available |
| 154 | + |
| 155 | +## Cleanup |
| 156 | + |
| 157 | +To clean up after benchmarks: |
| 158 | + |
| 159 | +```bash |
| 160 | +# Remove compiled binaries |
| 161 | +rm -f baseline/baseline baked/baked |
| 162 | + |
| 163 | +# Remove dependencies |
| 164 | +rm -rf baseline/lib baked/lib |
| 165 | +rm -f baseline/shard.lock baked/shard.lock |
| 166 | + |
| 167 | +# Remove results (optional) |
| 168 | +rm -rf results/ |
| 169 | +``` |
| 170 | + |
| 171 | +## Troubleshooting |
| 172 | + |
| 173 | +### Port Already in Use |
| 174 | + |
| 175 | +If you see "Address already in use" errors: |
| 176 | + |
| 177 | +```bash |
| 178 | +# Kill processes on ports 3000 and 3001 |
| 179 | +lsof -ti:3000 | xargs kill -9 |
| 180 | +lsof -ti:3001 | xargs kill -9 |
| 181 | +``` |
| 182 | + |
| 183 | +The `run_all.sh` script handles this automatically. |
| 184 | + |
| 185 | +### Compilation Errors |
| 186 | + |
| 187 | +Ensure dependencies are installed: |
| 188 | + |
| 189 | +```bash |
| 190 | +cd baseline && shards install |
| 191 | +cd ../baked && shards install |
| 192 | +``` |
| 193 | + |
| 194 | +### Inconsistent Results |
| 195 | + |
| 196 | +For more accurate benchmarks: |
| 197 | +- Close unnecessary applications |
| 198 | +- Run multiple times and average results |
| 199 | +- Ensure system is not under heavy load |
| 200 | +- Use release builds (benchmarks do this automatically) |
| 201 | + |
| 202 | +## Interpreting Results |
| 203 | + |
| 204 | +### Compile Time |
| 205 | + |
| 206 | +- **Expected:** 1-2 second overhead for ~1 MB of assets |
| 207 | +- **Scales:** Approximately linear with asset size |
| 208 | +- **Impact:** Only affects build time, not runtime |
| 209 | + |
| 210 | +### Binary Size |
| 211 | + |
| 212 | +- **Expected:** ~1.5x compression ratio (gzip compressed assets) |
| 213 | +- **Overhead:** Binary size = base + (compressed asset size) |
| 214 | +- **Impact:** One-time cost, doesn't affect runtime memory |
| 215 | + |
| 216 | +### Memory Usage |
| 217 | + |
| 218 | +- **Expected:** Minimal overhead (< 2 MB) |
| 219 | +- **Key Insight:** Assets are NOT fully decompressed into RAM |
| 220 | +- **Benefit:** Lazy decompression on read |
| 221 | + |
| 222 | +### Performance |
| 223 | + |
| 224 | +- **Small Files:** 5-10x speedup expected (no disk I/O) |
| 225 | +- **Large Files:** Similar performance (I/O dominates) |
| 226 | +- **Concurrent:** BakedFileSystem scales well with concurrency |
| 227 | + |
| 228 | +## Use Cases |
| 229 | + |
| 230 | +Based on benchmark results, BakedFileSystem is ideal for: |
| 231 | + |
| 232 | +✅ Small to medium static assets (< 10 MB total) |
| 233 | +✅ Read-heavy workloads |
| 234 | +✅ Single-binary deployments |
| 235 | +✅ Performance-critical static file serving |
| 236 | + |
| 237 | +Not recommended for: |
| 238 | + |
| 239 | +❌ Very large asset collections (> 50 MB) |
| 240 | +❌ Frequently changing assets |
| 241 | +❌ Extremely memory-constrained environments |
| 242 | + |
| 243 | +## Contributing |
| 244 | + |
| 245 | +To add new benchmarks: |
| 246 | + |
| 247 | +1. Create a new `.cr` script in this directory |
| 248 | +2. Output results to `results/your_benchmark.json` |
| 249 | +3. Update `report_generator.cr` to include new data |
| 250 | +4. Update `run_all.sh` to run your benchmark |
| 251 | +5. Document in this README |
| 252 | + |
| 253 | +## License |
| 254 | + |
| 255 | +Same as BakedFileSystem project. |
0 commit comments