Browse Source

codec: test: make values_test.go more fair for benchmarking purposes vs other libs

Currently, we use _struct with omitEmpty and use the codec tag.

Instead, use the json tag, so that json libs interprete it,
and this allows us to benchmark more apples-to-apples.

Move types that don't need to be used by benchmarks
into values_flex_test.go
Ugorji Nwoke 8 years ago
parent
commit
50329c0f55
2 changed files with 51 additions and 46 deletions
  1. 41 0
      codec/values_flex_test.go
  2. 10 46
      codec/values_test.go

+ 41 - 0
codec/values_flex_test.go

@@ -10,6 +10,47 @@ package codec
 // that other engines may not support or may barf upon
 // e.g. custom extensions for wrapped types, maps with non-string keys, etc.
 
+// Some unused types just stored here
+type Bbool bool
+type Sstring string
+type Sstructsmall struct {
+	A int
+}
+
+type Sstructbig struct {
+	A int
+	B bool
+	c string
+	// Sval Sstruct
+	Ssmallptr *Sstructsmall
+	Ssmall    *Sstructsmall
+	Sptr      *Sstructbig
+}
+
+type SstructbigMapBySlice struct {
+	_struct struct{} `codec:",toarray"`
+	A       int
+	B       bool
+	c       string
+	// Sval Sstruct
+	Ssmallptr *Sstructsmall
+	Ssmall    *Sstructsmall
+	Sptr      *Sstructbig
+}
+
+type Sinterface interface {
+	Noop()
+}
+
+// small struct for testing that codecgen works for unexported types
+type tLowerFirstLetter struct {
+	I int
+	u uint64
+	S string
+	b []byte
+}
+
+// Some used types
 type wrapInt64 int64
 type wrapUint8 uint8
 type wrapBytes []uint8

+ 10 - 46
codec/values_test.go

@@ -6,9 +6,14 @@
 package codec
 
 // This file contains values used by tests and benchmarks.
-// Consequently, we only use values that will parse well in all engines.
-// For example, JSON/BSON do not like maps with keys that are not strings,
-// so we only use maps with string keys here.
+// The benchmarks will test performance against other libraries (encoding/json, json-iterator, bson, gob, etc).
+// Consequently, we only use values that will parse well in all engines,
+// and only leverage features that work across multiple libraries for a truer comparison.
+// For example,
+// - JSON/BSON do not like maps with keys that are not strings,
+//   so we only use maps with string keys here.
+// - _struct options are not honored by other libraries,
+//   so we don't use them in this file.
 
 import (
 	"math"
@@ -156,7 +161,7 @@ type testStrucCommon struct {
 	// make this a ptr, so that it could be set or not.
 	// for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
 	// make this one omitempty (so it is excluded if nil).
-	*AnonInTestStrucIntf `codec:",omitempty"`
+	*AnonInTestStrucIntf `json:",omitempty"`
 
 	// R          Raw // Testing Raw must be explicitly turned on, so use standalone test
 	// Rext RawExt // Testing RawExt is tricky, so use standalone test
@@ -167,7 +172,7 @@ type testStrucCommon struct {
 }
 
 type TestStruc struct {
-	_struct struct{} `codec:",omitempty"` //set omitempty for every field
+	// _struct struct{} `json:",omitempty"` //set omitempty for every field
 
 	testStrucCommon
 
@@ -177,47 +182,6 @@ type TestStruc struct {
 	Nteststruc *TestStruc
 }
 
-// small struct for testing that codecgen works for unexported types
-type tLowerFirstLetter struct {
-	I int
-	u uint64
-	S string
-	b []byte
-}
-
-// Some other types
-
-type Sstring string
-type Bbool bool
-type Sstructsmall struct {
-	A int
-}
-
-type Sstructbig struct {
-	A int
-	B bool
-	c string
-	// Sval Sstruct
-	Ssmallptr *Sstructsmall
-	Ssmall    *Sstructsmall
-	Sptr      *Sstructbig
-}
-
-type SstructbigMapBySlice struct {
-	_struct struct{} `codec:",toarray"`
-	A       int
-	B       bool
-	c       string
-	// Sval Sstruct
-	Ssmallptr *Sstructsmall
-	Ssmall    *Sstructsmall
-	Sptr      *Sstructbig
-}
-
-type Sinterface interface {
-	Noop()
-}
-
 var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
 
 func populateTestStrucCommon(ts *testStrucCommon, n int, bench, useInterface, useStringKeyOnly bool) {