Skip to content

Commit 0885686

Browse files
committed
codec: always log BenchOnePass output regardless of test.v flag
1 parent a03e26d commit 0885686

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

codec/2_init_bench_test.go

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ package codec
3737

3838
import (
3939
"bytes"
40+
"fmt"
4041
"reflect"
4142
"runtime"
4243
"runtime/metrics"
@@ -123,32 +124,75 @@ func benchmarkDivider() {
123124
// testOnce.Do(testInitAll)
124125
// }
125126

126-
func TestBenchOnePass(t *testing.T) {
127+
func TestBenchOnePassCheck(t *testing.T) {
127128
// testOnce.Do(testInitAll)
128-
// t.Logf("..............................................")
129-
t.Logf("BENCHMARK INIT: %v", time.Now())
130-
// t.Logf("To run full benchmark comparing encodings, use: \"go test -bench=.\"")
131-
t.Logf("Benchmark: ")
132-
t.Logf("\tStruct recursive Depth: %d", testv.Depth)
129+
// benchOnePassLogf("..............................................")
130+
benchOnePassLogf("BENCHMARK INIT: %v", time.Now())
131+
// benchOnePassLogf("To run full benchmark comparing encodings, use: \"go test -bench=.\"")
132+
benchOnePassLogf("Benchmark: ")
133+
benchOnePassLogf("\tStruct recursive Depth: %d", testv.Depth)
133134
if approxSize > 0 {
134-
t.Logf("\tApproxDeepSize Of benchmark Struct: %d bytes", approxSize)
135+
benchOnePassLogf("\tApproxDeepSize Of benchmark Struct: %d bytes", approxSize)
135136
}
136137
if benchUnscientificRes {
137-
t.Logf("Benchmark One-Pass Run (with Unscientific Encode/Decode times): ")
138+
benchOnePassLogf("Benchmark One-Pass Run (with Unscientific Encode/Decode times): ")
138139
} else {
139-
t.Logf("Benchmark One-Pass Run:")
140+
benchOnePassLogf("Benchmark One-Pass Run:")
140141
}
141142
for _, bc := range benchCheckers {
142-
doBenchCheck(t, bc.name, bc.encodefn, bc.decodefn)
143+
benchOnePassCheck(t, bc.name, bc.encodefn, bc.decodefn)
143144
}
144145
if testv.Verbose {
145-
t.Logf("..............................................")
146-
t.Logf("<<<<====>>>> depth: %v, ts: %#v\n", testv.Depth, benchTs)
146+
benchOnePassLogf("..............................................")
147+
benchOnePassLogf("<<<<====>>>> depth: %v, ts: %#v\n", testv.Depth, benchTs)
147148
}
148149
runtime.GC()
149150
time.Sleep(100 * time.Millisecond)
150151
}
151152

153+
func benchOnePassCheck(t *testing.T, name string, encfn benchEncFn, decfn benchDecFn) {
154+
// if benchUnscientificRes {
155+
// benchOnePassLogf("-------------- %s ----------------", name)
156+
// }
157+
defer benchRecoverPanicT(t)
158+
runtime.GC()
159+
tnow := time.Now()
160+
buf, err := encfn(benchTs, nil)
161+
if err != nil {
162+
benchOnePassLogf("\t%10s: **** Error encoding benchTs: %v", name, err)
163+
return
164+
}
165+
encDur := time.Since(tnow)
166+
encLen := len(buf)
167+
runtime.GC()
168+
if !benchUnscientificRes {
169+
benchOnePassLogf("\t%10s: len: %d bytes\n", name, encLen)
170+
return
171+
}
172+
tnow = time.Now()
173+
var ts2 TestStruc
174+
if err = decfn(buf, &ts2); err != nil {
175+
benchOnePassLogf("\t%10s: **** Error decoding into new TestStruc: %v", name, err)
176+
return
177+
}
178+
decDur := time.Since(tnow)
179+
// if benchCheckDoDeepEqual {
180+
if benchVerify {
181+
if reflect.DeepEqual(benchTs, &ts2) {
182+
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v,\tencoded == decoded", name, encLen, encDur, decDur)
183+
} else {
184+
err = errDeepEqualNotMatch
185+
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v,\tencoded != decoded: %v", name, encLen, encDur, decDur, err)
186+
}
187+
} else {
188+
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v", name, encLen, encDur, decDur)
189+
}
190+
}
191+
192+
func benchOnePassLogf(format string, args ...interface{}) {
193+
fmt.Printf(format+"\n", args...)
194+
}
195+
152196
var vBenchTs = TestStruc{}
153197

154198
func fnBenchNewTs() interface{} {
@@ -185,45 +229,6 @@ func benchRecoverPanicT(t *testing.T) {
185229
}
186230
}
187231

188-
func doBenchCheck(t *testing.T, name string, encfn benchEncFn, decfn benchDecFn) {
189-
// if benchUnscientificRes {
190-
// t.Logf("-------------- %s ----------------", name)
191-
// }
192-
defer benchRecoverPanicT(t)
193-
runtime.GC()
194-
tnow := time.Now()
195-
buf, err := encfn(benchTs, nil)
196-
if err != nil {
197-
t.Logf("\t%10s: **** Error encoding benchTs: %v", name, err)
198-
return
199-
}
200-
encDur := time.Since(tnow)
201-
encLen := len(buf)
202-
runtime.GC()
203-
if !benchUnscientificRes {
204-
t.Logf("\t%10s: len: %d bytes\n", name, encLen)
205-
return
206-
}
207-
tnow = time.Now()
208-
var ts2 TestStruc
209-
if err = decfn(buf, &ts2); err != nil {
210-
t.Logf("\t%10s: **** Error decoding into new TestStruc: %v", name, err)
211-
return
212-
}
213-
decDur := time.Since(tnow)
214-
// if benchCheckDoDeepEqual {
215-
if benchVerify {
216-
if reflect.DeepEqual(benchTs, &ts2) {
217-
t.Logf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v,\tencoded == decoded", name, encLen, encDur, decDur)
218-
} else {
219-
err = errDeepEqualNotMatch
220-
t.Logf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v,\tencoded != decoded: %v", name, encLen, encDur, decDur, err)
221-
}
222-
} else {
223-
t.Logf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v", name, encLen, encDur, decDur)
224-
}
225-
}
226-
227232
func fnBenchmarkEncode(b *testing.B, encName string, ts interface{}, encfn benchEncFn) {
228233
defer benchRecoverPanic(b)
229234
// testOnce.Do(testInitAll)

0 commit comments

Comments
 (0)