File: dht_test.go

package info (click to toggle)
golang-github-anacrolix-log 0.13.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package log_test

import (
	"io"
	"testing"

	"github.com/anacrolix/log"
)

// Mirrors usage seen for a particularly expensive logging call-site in anacrolix/dht.
func BenchmarkEmulateDhtServerReplyLogger(b *testing.B) {
	l := log.Default.FilterLevel(log.Info).WithValues(&struct{}{}).WithContextText("some dht prefix").WithDefaultLevel(log.Debug)
	makeMsg := func() log.Msg {
		return log.Fmsg(
			"reply to %q",
			"your mum", // dht.Addr caches its String method return value
		)
	}
	b.Run("Filtered", func(b *testing.B) {
		b.Run("Direct", func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				makeMsg().Log(l)
			}
		})
		b.Run("Lazy", func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				l.LazyLog(log.Debug, makeMsg)
			}
		})
	})
	b.Run("Unfiltered", func(b *testing.B) {
		h := log.DefaultHandler
		h.W = io.Discard
		l := l
		l.Handlers = []log.Handler{h}
		b.Run("Direct", func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				makeMsg().LogLevel(log.Info, l)
			}
		})
		b.Run("Lazy", func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				l.LazyLog(log.Info, makeMsg)
			}
		})
	})
}