Skip to content

Instantly share code, notes, and snippets.

@ugorji
Created October 13, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugorji/0972bb21444609fec896 to your computer and use it in GitHub Desktop.
Save ugorji/0972bb21444609fec896 to your computer and use it in GitHub Desktop.
package github_107
import (
"encoding/json"
"io/ioutil"
"reflect"
"runtime"
"testing"
"github.com/ugorji/go/codec"
)
var h = new(codec.JsonHandle)
func init() {
h.MapType = reflect.TypeOf(map[string]interface{}(nil)) // specify that you want JSON mode of interface{} decoding
h.InterfaceReset = true // decode into "blank" interface{}. Do not try to decode into what is currently in the interface.
h.MapValueReset = true // decode into "blank" map value. Do not get old mapping value first. Parity with encoding/json
}
func testGoCodec(filename string, useCodec bool, t *testing.B) {
// log.Printf("Reading file %s", filename)
b, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
runtime.GC()
t.ResetTimer()
if useCodec {
for i := 0; i < t.N; i++ {
var v interface{}
codec.NewDecoderBytes(b, h).MustDecode(&v)
}
} else {
for i := 0; i < t.N; i++ {
var v interface{}
if err = json.Unmarshal(b, &v); err != nil {
panic(err)
}
}
}
}
func Benchmark__GoCodec1(t *testing.B) { testGoCodec("test1.json", true, t) }
func Benchmark__StdJson1(t *testing.B) { testGoCodec("test1.json", false, t) }
func Benchmark__GoCodec2(t *testing.B) { testGoCodec("test2.json", true, t) }
func Benchmark__StdJson2(t *testing.B) { testGoCodec("test2.json", false, t) }
func Test__Noop(t *testing.T) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment