|
| 1 | +package block |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 9 | + |
| 10 | + blockmetrics "github.com/e2b-dev/infra/packages/orchestrator/pkg/sandbox/block/metrics" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + cbBlockSize int64 = 4096 |
| 15 | + cbNumBlocks int64 = 16384 // 64 MiB |
| 16 | + cbCacheSize int64 = cbNumBlocks * cbBlockSize |
| 17 | + cbChunkSize int64 = 4 * 1024 * 1024 // 4 MiB — MemoryChunkSize |
| 18 | + cbChunkCount int64 = cbCacheSize / cbChunkSize |
| 19 | +) |
| 20 | + |
| 21 | +// BenchmarkChunkerSlice_CacheHit benchmarks the full FullFetchChunker.Slice |
| 22 | +// hot path on a cache hit: bitmap check + mmap slice return + OTEL |
| 23 | +// timer.Success with attribute construction. |
| 24 | +func BenchmarkChunkerSlice_CacheHit(b *testing.B) { |
| 25 | + provider := sdkmetric.NewMeterProvider() |
| 26 | + b.Cleanup(func() { provider.Shutdown(context.Background()) }) |
| 27 | + |
| 28 | + m, err := blockmetrics.NewMetrics(provider) |
| 29 | + if err != nil { |
| 30 | + b.Fatal(err) |
| 31 | + } |
| 32 | + |
| 33 | + chunker, err := NewFullFetchChunker( |
| 34 | + cbCacheSize, cbBlockSize, |
| 35 | + nil, // base is never called on cache hit |
| 36 | + filepath.Join(b.TempDir(), "cache"), |
| 37 | + m, |
| 38 | + ) |
| 39 | + if err != nil { |
| 40 | + b.Fatal(err) |
| 41 | + } |
| 42 | + b.Cleanup(func() { chunker.Close() }) |
| 43 | + |
| 44 | + // Pre-populate the cache so every Slice hits. |
| 45 | + chunker.cache.setIsCached(0, cbCacheSize) |
| 46 | + |
| 47 | + ctx := context.Background() |
| 48 | + |
| 49 | + b.ResetTimer() |
| 50 | + for i := range b.N { |
| 51 | + off := int64(i%int(cbChunkCount)) * cbChunkSize |
| 52 | + s, sliceErr := chunker.Slice(ctx, off, cbChunkSize) |
| 53 | + if sliceErr != nil { |
| 54 | + b.Fatal(sliceErr) |
| 55 | + } |
| 56 | + if len(s) == 0 { |
| 57 | + b.Fatal("empty slice") |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments