|
1 | 1 | package benchmarks |
2 | 2 |
|
3 | | -// Benchmarks focused on the builder pattern before migration to functional options. |
| 3 | +// Benchmarks for the OopsErrorBuilder API. |
4 | 4 | // |
5 | | -// The core overhead: every builder method calls copy(), which clones 3 maps |
6 | | -// (context, userData, tenantData) and 1 slice (tags). A chain of N methods |
7 | | -// pays that cost N times, regardless of whether the error is ever read. |
| 5 | +// These benchmarks target the current implementation and are designed for |
| 6 | +// cross-commit comparisons (e.g. benchstat main...HEAD). |
8 | 7 | // |
9 | 8 | // Run: |
10 | | -// go test -bench=. -benchmem -count=10 ./benchmarks/ | tee bench-builder.txt |
| 9 | +// go test -bench=. -benchmem -count=10 ./benchmarks/ | tee bench.txt |
11 | 10 | // |
12 | | -// Compare after refactoring: |
13 | | -// benchstat bench-builder.txt bench-functional-options.txt |
| 11 | +// Compare two commits: |
| 12 | +// benchstat bench-before.txt bench-after.txt |
14 | 13 |
|
15 | 14 | import ( |
16 | 15 | "errors" |
@@ -278,7 +277,7 @@ func BenchmarkBuilderVsStdlib(b *testing.B) { |
278 | 277 | b.Run("stdlib/fmt.Errorf", func(b *testing.B) { |
279 | 278 | b.ReportAllocs() |
280 | 279 | for i := 0; i < b.N; i++ { |
281 | | - _ = errors.New("wrapped: " + inner.Error()) |
| 280 | + _ = fmt.Errorf("wrapped: %w", inner) |
282 | 281 | } |
283 | 282 | }) |
284 | 283 |
|
@@ -391,10 +390,7 @@ func BenchmarkOopsErrorMemoryFootprint(b *testing.B) { |
391 | 390 | var after runtime.MemStats |
392 | 391 | runtime.ReadMemStats(&after) |
393 | 392 |
|
394 | | - liveTotal := int64(after.HeapInuse) - int64(before.HeapInuse) |
395 | | - if liveTotal < 0 { |
396 | | - liveTotal = 0 |
397 | | - } |
| 393 | + liveTotal := max(int64(after.HeapInuse)-int64(before.HeapInuse), 0) |
398 | 394 | b.ReportMetric(float64(liveTotal), "live-B/total") |
399 | 395 | b.ReportMetric(float64(liveTotal)/float64(count), "live-B/error") |
400 | 396 |
|
|
0 commit comments