Add more Go benchmarks and tool for regenerating README.md

This commit is contained in:
2019-10-26 13:10:04 -07:00
parent 7934023f30
commit a6f38188ec
7 changed files with 342 additions and 2 deletions

18
src/README.md Normal file
View File

@@ -0,0 +1,18 @@
goos: linux
goarch: amd64
pkg: xinu.tv/rustperf/src
BenchmarkVec3ToJSONStdlib-8 2464 467698 ns/op
BenchmarkVec3ToJSONIterator-8 2583 477032 ns/op
BenchmarkVec3ToJSONGojay-8 1216 859554 ns/op
PASS
ok xinu.tv/rustperf/src 3.631s
```
goos: linux
goarch: amd64
pkg: xinu.tv/rustperf/src
BenchmarkVec3ToJSONStdlib-8 2288 475548 ns/op
BenchmarkVec3ToJSONIterator-8 2389 494459 ns/op
BenchmarkVec3ToJSONGojay-8 1483 837328 ns/op
PASS
ok xinu.tv/rustperf/src 3.702s
```

View File

@@ -4,6 +4,9 @@ import (
"encoding/json"
"reflect"
"testing"
"github.com/francoispqt/gojay"
jsoniter "github.com/json-iterator/go"
)
type Vec3 struct {
@@ -12,7 +15,29 @@ type Vec3 struct {
Z float32 `json:"z"`
}
func TestVec3ToJSON(t *testing.T) {
func (v *Vec3) MarshalJSONObject(enc *gojay.Encoder) {
enc.FloatKey("x", float64(v.X))
enc.FloatKey("y", float64(v.Y))
enc.FloatKey("z", float64(v.Z))
}
func (v *Vec3) IsNil() bool {
return v == nil
}
type Vec3Slice []*Vec3
func (vs *Vec3Slice) MarshalJSONArray(enc *gojay.Encoder) {
for _, e := range *vs {
enc.AddObject(e)
}
}
func (vs Vec3Slice) IsNil() bool {
return vs == nil
}
func TestVec3ToJSONStdlib(t *testing.T) {
vecs := []Vec3{{X: 1.0, Y: 2.0, Z: 3.0}}
got, err := json.Marshal(vecs)
@@ -25,7 +50,7 @@ func TestVec3ToJSON(t *testing.T) {
}
}
func BenchmarkVec3ToJSON(b *testing.B) {
func BenchmarkVec3ToJSONStdlib(b *testing.B) {
var vecs []Vec3
for i := 0; i < 1000; i++ {
@@ -35,9 +60,74 @@ func BenchmarkVec3ToJSON(b *testing.B) {
Z: 255. * float32(i) / 1000.,
})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(vecs); err != nil {
b.Fatalf("Failed to marshal: %v", err)
}
}
}
func TestVec3ToJSONIterator(t *testing.T) {
vecs := []Vec3{{X: 1.0, Y: 2.0, Z: 3.0}}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
got, err := json.Marshal(vecs)
if err != nil {
t.Fatalf("Failed to marshal: %v", err)
}
want := []byte(`[{"x":1,"y":2,"z":3}]`)
if !reflect.DeepEqual(want, got) {
t.Fatalf("got != want\nGot: %s\nWant: %s", got, want)
}
}
func BenchmarkVec3ToJSONIterator(b *testing.B) {
var vecs []Vec3
for i := 0; i < 1000; i++ {
vecs = append(vecs, Vec3{
X: 255. * float32(i) / 1000.,
Y: 255. * float32(i) / 1000.,
Z: 255. * float32(i) / 1000.,
})
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(vecs); err != nil {
b.Fatalf("Failed to marshal: %v", err)
}
}
}
func TestVec3ToJSONGojay(t *testing.T) {
vecs := Vec3Slice{{X: 1.0, Y: 2.0, Z: 3.0}}
got, err := gojay.Marshal(&vecs)
if err != nil {
t.Fatalf("Failed to marshal: %v", err)
}
want := []byte(`[{"x":1,"y":2,"z":3}]`)
if !reflect.DeepEqual(want, got) {
t.Fatalf("got != want\nGot: %s\nWant: %s", got, want)
}
}
func BenchmarkVec3ToJSONGojay(b *testing.B) {
vecs := Vec3Slice{}
for i := 0; i < 1000; i++ {
vecs = append(vecs, &Vec3{
X: 255. * float32(i) / 1000.,
Y: 255. * float32(i) / 1000.,
Z: 255. * float32(i) / 1000.,
})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := gojay.Marshal(&vecs); err != nil {
b.Fatalf("Failed to marshal: %v", err)
}
}
}