forked from ipfs/go-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logbench_test.go
68 lines (56 loc) · 1.23 KB
/
logbench_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package log
import (
"sync"
"testing"
)
// To run bencharks:
// > go test -c .
// > ./go-log.test -test.run NONE -test.bench . 2>/dev/null
// Otherwise you test how fast your terminal can print.
func BenchmarkSimpleInfo(b *testing.B) {
l := Logger("bench")
err := SetLogLevel("bench", "info")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
l.Info("test")
}
}
var logString = "String, IDK what to write, let's punch a keyboard. jkdlsjklfdjfklsjfklsdjaflkdjfkdjsfkldjsfkdjklfjdslfjakdfjioerjieofjofdnvonoijdfneslkffjsdfljadljfdjkfjkf"
func BenchmarkFormatInfo(b *testing.B) {
l := Logger("bench")
err := SetLogLevel("bench", "info")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
l.Infof("test %d %s", logString)
}
}
func BenchmarkFormatInfoMulti(b *testing.B) {
l := Logger("bench")
err := SetLogLevel("bench", "info")
if err != nil {
b.Fatal(err)
}
var wg sync.WaitGroup
goroutines := 16
run := func() {
for i := 0; i < b.N/goroutines; i++ {
l.Infof("test %d %s", i, logString)
}
wg.Done()
}
wg.Add(goroutines)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < goroutines; i++ {
go run()
}
wg.Wait()
}