@@ -37,6 +37,7 @@ package codec
37
37
38
38
import (
39
39
"bytes"
40
+ "fmt"
40
41
"reflect"
41
42
"runtime"
42
43
"runtime/metrics"
@@ -123,32 +124,75 @@ func benchmarkDivider() {
123
124
// testOnce.Do(testInitAll)
124
125
// }
125
126
126
- func TestBenchOnePass (t * testing.T ) {
127
+ func TestBenchOnePassCheck (t * testing.T ) {
127
128
// 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 ("\t Struct 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 ("\t Struct recursive Depth: %d" , testv .Depth )
133
134
if approxSize > 0 {
134
- t . Logf ("\t ApproxDeepSize Of benchmark Struct: %d bytes" , approxSize )
135
+ benchOnePassLogf ("\t ApproxDeepSize Of benchmark Struct: %d bytes" , approxSize )
135
136
}
136
137
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): " )
138
139
} else {
139
- t . Logf ("Benchmark One-Pass Run:" )
140
+ benchOnePassLogf ("Benchmark One-Pass Run:" )
140
141
}
141
142
for _ , bc := range benchCheckers {
142
- doBenchCheck (t , bc .name , bc .encodefn , bc .decodefn )
143
+ benchOnePassCheck (t , bc .name , bc .encodefn , bc .decodefn )
143
144
}
144
145
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 )
147
148
}
148
149
runtime .GC ()
149
150
time .Sleep (100 * time .Millisecond )
150
151
}
151
152
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,\t encoded == decoded" , name , encLen , encDur , decDur )
183
+ } else {
184
+ err = errDeepEqualNotMatch
185
+ benchOnePassLogf ("\t %10s: len: %d bytes,\t encode: %v,\t decode: %v,\t encoded != 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
+
152
196
var vBenchTs = TestStruc {}
153
197
154
198
func fnBenchNewTs () interface {} {
@@ -185,45 +229,6 @@ func benchRecoverPanicT(t *testing.T) {
185
229
}
186
230
}
187
231
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,\t encoded == decoded" , name , encLen , encDur , decDur )
218
- } else {
219
- err = errDeepEqualNotMatch
220
- t .Logf ("\t %10s: len: %d bytes,\t encode: %v,\t decode: %v,\t encoded != 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
-
227
232
func fnBenchmarkEncode (b * testing.B , encName string , ts interface {}, encfn benchEncFn ) {
228
233
defer benchRecoverPanic (b )
229
234
// testOnce.Do(testInitAll)
0 commit comments