Skip to content

Commit 9b1896b

Browse files
log: using testing.B.Loop (#32663)
before: go test -run=^$ -bench=. ./log -timeout=1h 12.19s user 2.19s system 89% cpu 16.025 total after: go test -run=^$ -bench=. ./log -timeout=1h 10.64s user 1.53s system 89% cpu 13.607 total --------- Co-authored-by: lightclient <[email protected]>
1 parent 3ebb143 commit 9b1896b

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

log/format_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ var sink []byte
1010
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
1111
buf := make([]byte, 100)
1212
b.ReportAllocs()
13-
for i := 0; i < b.N; i++ {
13+
for b.Loop() {
1414
sink = appendInt64(buf, rand.Int63())
1515
}
1616
}
1717

1818
func BenchmarkPrettyUint64Logfmt(b *testing.B) {
1919
buf := make([]byte, 100)
2020
b.ReportAllocs()
21-
for i := 0; i < b.N; i++ {
21+
for b.Loop() {
2222
sink = appendUint64(buf, rand.Uint64(), false)
2323
}
2424
}

log/logger_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ func TestJSONHandler(t *testing.T) {
7070

7171
func BenchmarkTraceLogging(b *testing.B) {
7272
SetDefault(NewLogger(NewTerminalHandler(io.Discard, true)))
73-
b.ResetTimer()
74-
for i := 0; i < b.N; i++ {
73+
i := 0
74+
for b.Loop() {
7575
Trace("a message", "v", i)
76+
i++
7677
}
7778
}
7879

@@ -99,8 +100,8 @@ func benchmarkLogger(b *testing.B, l Logger) {
99100
err = errors.New("oh nooes it's crap")
100101
)
101102
b.ReportAllocs()
102-
b.ResetTimer()
103-
for i := 0; i < b.N; i++ {
103+
i := 0
104+
for b.Loop() {
104105
l.Info("This is a message",
105106
"foo", int16(i),
106107
"bytes", bb,
@@ -109,8 +110,8 @@ func benchmarkLogger(b *testing.B, l Logger) {
109110
"bigint", bigint,
110111
"nilbig", nilbig,
111112
"err", err)
113+
i++
112114
}
113-
b.StopTimer()
114115
}
115116

116117
func TestLoggerOutput(t *testing.T) {
@@ -161,18 +162,18 @@ const termTimeFormat = "01-02|15:04:05.000"
161162
func BenchmarkAppendFormat(b *testing.B) {
162163
var now = time.Now()
163164
b.Run("fmt time.Format", func(b *testing.B) {
164-
for i := 0; i < b.N; i++ {
165+
for b.Loop() {
165166
fmt.Fprintf(io.Discard, "%s", now.Format(termTimeFormat))
166167
}
167168
})
168169
b.Run("time.AppendFormat", func(b *testing.B) {
169-
for i := 0; i < b.N; i++ {
170+
for b.Loop() {
170171
now.AppendFormat(nil, termTimeFormat)
171172
}
172173
})
173174
var buf = new(bytes.Buffer)
174175
b.Run("time.Custom", func(b *testing.B) {
175-
for i := 0; i < b.N; i++ {
176+
for b.Loop() {
176177
writeTimeTermFormat(buf, now)
177178
buf.Reset()
178179
}

0 commit comments

Comments
 (0)