Skip to content

Commit 3c70f93

Browse files
committed
codec: testing cleanup
- bundle test variables within a single struct - only store values in 1 place (not multiple places with a sync scheme)
1 parent 1c88f85 commit 3c70f93

File tree

9 files changed

+308
-266
lines changed

9 files changed

+308
-266
lines changed

codec/0_init_test.go

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ package codec
6060
import (
6161
"bytes"
6262
"flag"
63+
"strconv"
6364
"testing"
6465
)
6566

6667
func init() {
6768
// log.SetOutput(io.Discard) // don't allow things log to standard out/err
6869
testPreInitFns = append(testPreInitFns, testInitFlags, benchInitFlags, testParseFlags)
69-
testPostInitFns = append(testPostInitFns, testUpdateOptionsFromFlags)
70-
testReInitFns = append(testReInitFns, testUpdateOptionsFromFlags)
70+
// testPostInitFns = append(testPostInitFns, testUpdateOptionsFromFlags)
71+
// testReInitFns = append(testReInitFns, testUpdateOptionsFromFlags)
7172
}
7273

7374
func TestMain(m *testing.M) {
@@ -83,64 +84,92 @@ var (
8384
)
8485

8586
// flag variables used by tests (and bench)
86-
var (
87-
testVerbose bool
88-
87+
type testVars struct {
88+
Verbose bool
8989
//depth of 0 maps to ~400bytes json-encoded string, 1 maps to ~1400 bytes, etc
9090
//For depth>1, we likely trigger stack growth for encoders, making benchmarking unreliable.
91-
testDepth int
91+
Depth int
9292

93-
testMaxInitLen int
93+
UseDiff bool
9494

95-
testUseDiff bool
95+
UseReset bool
96+
UseParallel bool
9697

97-
testUseReset bool
98-
testUseParallel bool
98+
SkipIntf bool
99+
SkipRPCTests bool
99100

100-
testSkipIntf bool
101+
UseIoWrapper bool
101102

102-
testUseIoEncDec int
103-
testUseIoWrapper bool
103+
NumRepeatString int
104104

105-
testNumRepeatString int
105+
RpcBufsize int
106+
MapStringKeyOnly bool
106107

107-
testRpcBufsize int // Deprecated: no-op
108-
testMapStringKeyOnly bool
109-
testZeroCopy bool
108+
BenchmarkNoConfig bool
110109

111-
testBenchmarkNoConfig bool
110+
BenchmarkWithRuntimeMetrics bool
112111

113-
testBenchmarkWithRuntimeMetrics bool
114-
)
112+
bufsize testBufioSizeFlag
113+
114+
// variables that are not flags, but which can configure the handles
115+
E EncodeOptions
116+
D DecodeOptions
117+
R RPCOptions
118+
119+
// MaxInitLen int
120+
// ZeroCopy bool
121+
// UseIoEncDec int
122+
}
123+
124+
func (x *testVars) setBufsize(v int) {
125+
x.E.WriterBufferSize = v
126+
x.D.ReaderBufferSize = v
127+
}
128+
129+
type testBufioSizeFlag int
130+
131+
func (x *testBufioSizeFlag) String() string { return strconv.Itoa(int(*x)) }
132+
func (x *testBufioSizeFlag) Set(s string) (err error) {
133+
v, err := strconv.ParseInt(s, 0, strconv.IntSize)
134+
if err != nil {
135+
v = -1
136+
}
137+
*x = testBufioSizeFlag(v)
138+
testv.setBufsize((int)(v))
139+
return
140+
}
141+
func (x *testBufioSizeFlag) Get() interface{} { return int(*x) }
142+
143+
var testv testVars
115144

116145
func testInitFlags() {
117146
var bIgnore bool
118147
// delete(testDecOpts.ExtFuncs, timeTyp)
119-
flag.BoolVar(&testVerbose, "tv", false, "Text Extra Verbose Logging if -v if set")
120-
flag.IntVar(&testUseIoEncDec, "ti", -1, "Use IO Reader/Writer for Marshal/Unmarshal ie >= 0")
121-
flag.BoolVar(&testUseIoWrapper, "tiw", false, "Wrap the IO Reader/Writer with a base pass-through reader/writer")
148+
flag.Var(&testv.bufsize, "ti", "Use IO Reader/Writer for Marshal/Unmarshal ie >= 0")
149+
flag.BoolVar(&testv.Verbose, "tv", false, "Text Extra Verbose Logging if -v if set")
150+
flag.BoolVar(&testv.UseIoWrapper, "tiw", false, "Wrap the IO Reader/Writer with a base pass-through reader/writer")
122151

123-
flag.BoolVar(&testSkipIntf, "tf", false, "Skip Interfaces")
124-
flag.BoolVar(&testUseReset, "tr", false, "Use Reset")
125-
flag.BoolVar(&testUseParallel, "tp", false, "Run tests in parallel")
126-
flag.IntVar(&testNumRepeatString, "trs", 8, "Create string variables by repeating a string N times")
127-
flag.BoolVar(&testUseDiff, "tdiff", false, "Use Diff")
128-
flag.BoolVar(&testZeroCopy, "tzc", false, "Use Zero copy mode")
152+
flag.BoolVar(&testv.SkipIntf, "tf", false, "Skip Interfaces")
153+
flag.BoolVar(&testv.UseReset, "tr", false, "Use Reset")
154+
flag.BoolVar(&testv.UseParallel, "tp", false, "Run tests in parallel")
155+
flag.IntVar(&testv.NumRepeatString, "trs", 8, "Create string variables by repeating a string N times")
156+
flag.BoolVar(&testv.UseDiff, "tdiff", false, "Use Diff")
157+
flag.BoolVar(&testv.D.ZeroCopy, "tzc", false, "Use Zero copy mode")
129158

130-
flag.BoolVar(&bIgnore, "tm", true, "(Deprecated) Use Must(En|De)code")
159+
flag.IntVar(&testv.D.MaxInitLen, "tx", 0, "Max Init Len")
131160

132-
flag.IntVar(&testMaxInitLen, "tx", 0, "Max Init Len")
161+
flag.IntVar(&testv.Depth, "tsd", 0, "Test Struc Depth")
162+
flag.BoolVar(&testv.MapStringKeyOnly, "tsk", false, "use maps with string keys only")
133163

134-
flag.IntVar(&testDepth, "tsd", 0, "Test Struc Depth")
135-
flag.BoolVar(&testMapStringKeyOnly, "tsk", false, "use maps with string keys only")
164+
flag.BoolVar(&bIgnore, "tm", true, "(Deprecated) Use Must(En|De)code")
136165
}
137166

138167
func benchInitFlags() {
139-
flag.BoolVar(&testBenchmarkNoConfig, "bnc", false, "benchmarks: do not make configuration changes for fair benchmarking")
140-
flag.BoolVar(&testBenchmarkWithRuntimeMetrics, "brm", false, "benchmarks: include runtime metrics")
168+
flag.BoolVar(&testv.BenchmarkNoConfig, "bnc", false, "benchmarks: do not make configuration changes for fair benchmarking")
169+
flag.BoolVar(&testv.BenchmarkWithRuntimeMetrics, "brm", false, "benchmarks: include runtime metrics")
141170
// flags reproduced here for compatibility (duplicate some in testInitFlags)
142-
flag.BoolVar(&testMapStringKeyOnly, "bs", false, "benchmarks: use maps with string keys only")
143-
flag.IntVar(&testDepth, "bd", 1, "Benchmarks: Test Struc Depth")
171+
flag.BoolVar(&testv.MapStringKeyOnly, "bs", false, "benchmarks: use maps with string keys only")
172+
flag.IntVar(&testv.Depth, "bd", 1, "Benchmarks: Test Struc Depth")
144173
}
145174

146175
func testParseFlags() {
@@ -150,12 +179,12 @@ func testParseFlags() {
150179
}
151180
}
152181

153-
func testUpdateOptionsFromFlags() {
154-
testEncodeOptions.WriterBufferSize = testUseIoEncDec
155-
testDecodeOptions.ReaderBufferSize = testUseIoEncDec
156-
testDecodeOptions.MaxInitLen = testMaxInitLen
157-
testDecodeOptions.ZeroCopy = testZeroCopy
158-
}
182+
// func testUpdateOptionsFromFlags() {
183+
// testv.E.WriterBufferSize = testv.UseIoEncDec
184+
// testv.D.ReaderBufferSize = testv.UseIoEncDec
185+
// testv.D.MaxInitLen = testv.MaxInitLen
186+
// testv.D.ZeroCopy = testv.ZeroCopy
187+
// }
159188

160189
func testReinit() {
161190
// testOnce = sync.Once{}

codec/1_init_run_test.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import (
1010
// . "github.com/ugorji/go/codec"
1111
)
1212

13-
// variables that are not flags, but which can configure the handles
14-
var (
15-
testEncodeOptions EncodeOptions
16-
testDecodeOptions DecodeOptions
17-
testRPCOptions RPCOptions
18-
)
19-
2013
type testHED struct {
2114
H Handle
2215
Eb *Encoder
@@ -133,8 +126,8 @@ func testSharedCodecEncode(ts interface{}, bsIn []byte,
133126
// bs = make([]byte, 0, approxSize)
134127
var e *Encoder
135128
var buf *bytes.Buffer
136-
useIO := testUseIoEncDec >= 0
137-
if testUseReset && !testUseParallel {
129+
useIO := testv.E.WriterBufferSize >= 0
130+
if testv.UseReset && !testv.UseParallel {
138131
hed := testHEDGet(h)
139132
if useIO {
140133
e = hed.Eio
@@ -150,7 +143,7 @@ func testSharedCodecEncode(ts interface{}, bsIn []byte,
150143
// var oldWriteBufferSize int
151144
if useIO {
152145
buf = fn(bsIn)
153-
if testUseIoWrapper {
146+
if testv.UseIoWrapper {
154147
e.Reset(ioWriterWrapper{buf})
155148
} else {
156149
e.Reset(buf)
@@ -164,16 +157,16 @@ func testSharedCodecEncode(ts interface{}, bsIn []byte,
164157
} else {
165158
err = e.Encode(ts)
166159
}
167-
if testUseIoEncDec >= 0 {
160+
if testv.E.WriterBufferSize >= 0 {
168161
bs = buf.Bytes()
169162
}
170163
return
171164
}
172165

173166
func testSharedCodecDecoder(bs []byte, h Handle) (d *Decoder) {
174167
// var buf *bytes.Reader
175-
useIO := testUseIoEncDec >= 0
176-
if testUseReset && !testUseParallel {
168+
useIO := testv.D.ReaderBufferSize >= 0
169+
if testv.UseReset && !testv.UseParallel {
177170
hed := testHEDGet(h)
178171
if useIO {
179172
d = hed.Dio
@@ -187,7 +180,7 @@ func testSharedCodecDecoder(bs []byte, h Handle) (d *Decoder) {
187180
}
188181
if useIO {
189182
buf := bytes.NewReader(bs)
190-
if testUseIoWrapper {
183+
if testv.UseIoWrapper {
191184
d.Reset(ioReaderWrapper{buf})
192185
} else {
193186
d.Reset(buf)
@@ -212,9 +205,9 @@ func testUpdateBasicHandleOptions(bh *BasicHandle) {
212205
// cleanInited() not needed, as we re-create the Handles on each reinit
213206
// bh.clearInited() // so it is reinitialized next time around
214207
// pre-fill them first
215-
bh.EncodeOptions = testEncodeOptions
216-
bh.DecodeOptions = testDecodeOptions
217-
bh.RPCOptions = testRPCOptions
208+
bh.EncodeOptions = testv.E
209+
bh.DecodeOptions = testv.D
210+
bh.RPCOptions = testv.R
218211
// bh.InterfaceReset = true
219212
// bh.PreferArrayOverSlice = true
220213
// modify from flag'ish things

codec/2_init_bench_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func init() {
7878
}
7979

8080
func benchInit() {
81-
benchTs = newTestStruc(testDepth, testNumRepeatString, true, !testSkipIntf, testMapStringKeyOnly)
81+
benchTs = newTestStruc(testv.Depth, testv.NumRepeatString, true, !testv.SkipIntf, testv.MapStringKeyOnly)
8282
approxSize = approxDataSize(reflect.ValueOf(benchTs)) * 2 // multiply by 1.5 or 2 to appease msgp, and prevent alloc
83-
// bytesLen := 1024 * 4 * (testDepth + 1) * (testDepth + 1)
83+
// bytesLen := 1024 * 4 * (testv.Depth + 1) * (testv.Depth + 1)
8484
// if bytesLen < approxSize {
8585
// bytesLen = approxSize
8686
// }
@@ -93,7 +93,7 @@ func benchReinit() {
9393

9494
func benchUpdateHandles() {
9595
// benchCheckers = nil
96-
if testBenchmarkNoConfig {
96+
if testv.BenchmarkNoConfig {
9797
return
9898
}
9999

@@ -128,7 +128,7 @@ func TestBenchOnePass(t *testing.T) {
128128
t.Logf("BENCHMARK INIT: %v", time.Now())
129129
// t.Logf("To run full benchmark comparing encodings, use: \"go test -bench=.\"")
130130
t.Logf("Benchmark: ")
131-
t.Logf("\tStruct recursive Depth: %d", testDepth)
131+
t.Logf("\tStruct recursive Depth: %d", testv.Depth)
132132
if approxSize > 0 {
133133
t.Logf("\tApproxDeepSize Of benchmark Struct: %d bytes", approxSize)
134134
}
@@ -140,9 +140,9 @@ func TestBenchOnePass(t *testing.T) {
140140
for _, bc := range benchCheckers {
141141
doBenchCheck(t, bc.name, bc.encodefn, bc.decodefn)
142142
}
143-
if testVerbose {
143+
if testv.Verbose {
144144
t.Logf("..............................................")
145-
t.Logf("<<<<====>>>> depth: %v, ts: %#v\n", testDepth, benchTs)
145+
t.Logf("<<<<====>>>> depth: %v, ts: %#v\n", testv.Depth, benchTs)
146146
}
147147
runtime.GC()
148148
time.Sleep(100 * time.Millisecond)
@@ -294,7 +294,7 @@ func fnBenchmarkDecode(b *testing.B, encName string, ts interface{},
294294

295295
func fnBenchmarkRun(b *testing.B, fn func()) {
296296
fn() // run one time first - to init things
297-
if testBenchmarkWithRuntimeMetrics {
297+
if testv.BenchmarkWithRuntimeMetrics {
298298
fnBenchmarkRunWithMetrics(b, fn)
299299
} else {
300300
fnBenchmarkRunNoMetrics(b, fn)

0 commit comments

Comments
 (0)