MCPcopy
hub / github.com/grafana/dskit / BenchmarkBuffered

Function BenchmarkBuffered

log/buffered_test.go:27–59  ·  view source on GitHub ↗

BenchmarkBuffered creates buffered loggers of various capacities to see which perform best.

(b *testing.B)

Source from the content-addressed store, hash-verified

25
26// BenchmarkBuffered creates buffered loggers of various capacities to see which perform best.
27func 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.
62func BenchmarkUnbuffered(b *testing.B) {

Callers

nothing calls this directly

Calls 10

FlushMethod · 0.95
outFileFunction · 0.85
NewBufferedLoggerFunction · 0.85
WithFlushPeriodFunction · 0.85
WithPrellocatedBufferFunction · 0.85
RunMethod · 0.80
WithMethod · 0.80
NameMethod · 0.65
LogMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected