BenchmarkBuffered creates buffered loggers of various capacities to see which perform best.
(b *testing.B)
| 25 | |
| 26 | // BenchmarkBuffered creates buffered loggers of various capacities to see which perform best. |
| 27 | func BenchmarkBuffered(b *testing.B) { |
| 28 | for i := 1; i <= 2048; i *= 2 { |
| 29 | f := outFile(b) |
| 30 | |
| 31 | bufLog := NewBufferedLogger(f, uint32(i), |
| 32 | WithFlushPeriod(flushPeriod), |
| 33 | WithPrellocatedBuffer(bufferSize), |
| 34 | ) |
| 35 | l := log.NewLogfmtLogger(bufLog) |
| 36 | |
| 37 | b.Run(fmt.Sprintf("capacity:%d", i), func(b *testing.B) { |
| 38 | b.ReportAllocs() |
| 39 | b.StartTimer() |
| 40 | |
| 41 | require.NoError(b, f.Truncate(0)) |
| 42 | |
| 43 | logger := log.With(l, "common_key", "common_value") |
| 44 | for j := 0; j < b.N; j++ { |
| 45 | logger.Log("foo_key", "foo_value") |
| 46 | } |
| 47 | |
| 48 | // force a final flush for outstanding lines in buffer |
| 49 | bufLog.Flush() |
| 50 | b.StopTimer() |
| 51 | |
| 52 | contents, err := os.ReadFile(f.Name()) |
| 53 | require.NoErrorf(b, err, "could not read test file: %s", f.Name()) |
| 54 | |
| 55 | lines := strings.Split(string(contents), "\n") |
| 56 | require.Len(b, lines, b.N+1) |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // BenchmarkUnbuffered should perform roughly equivalently to a buffered logger with a capacity of 1. |
| 62 | func BenchmarkUnbuffered(b *testing.B) { |
nothing calls this directly
no test coverage detected