Commit 5acd08dd authored by Eric Myhre's avatar Eric Myhre

Scalable marshal and unmarshal benchmarks.

Dear reader, I am so excited about this.

Also: added additional sanity checks to the end of benchmarks, after
a call to `b.StopTimer`.  Always important to make sure your benchmark
is actually doing what you think it is, and not being super fast
because it turns out to be a hyperspeed application-layer devnull!

(In the case of the unmarshal tests, asserting sanity by remarshalling
the whole dang thing *again* might look a little funny... but it's
certainly a terse way to get full coverage of the data!)
parent 11a9896f
...@@ -89,10 +89,16 @@ func BenchmarkMap25nGenericMapIterationSimpleKeys(b *testing.B) { ...@@ -89,10 +89,16 @@ func BenchmarkMap25nGenericMapIterationSimpleKeys(b *testing.B) {
func BenchmarkSpec_Marshal_Map3StrInt(b *testing.B) { func BenchmarkSpec_Marshal_Map3StrInt(b *testing.B) {
tests.BenchmarkSpec_Marshal_Map3StrInt(b, NodeBuilder()) tests.BenchmarkSpec_Marshal_Map3StrInt(b, NodeBuilder())
} }
func BenchmarkSpec_Marshal_MapNStrMap3StrInt(b *testing.B) {
tests.BenchmarkSpec_Marshal_MapNStrMap3StrInt(b, NodeBuilder())
}
func BenchmarkSpec_Unmarshal_Map3StrInt(b *testing.B) { func BenchmarkSpec_Unmarshal_Map3StrInt(b *testing.B) {
tests.BenchmarkSpec_Unmarshal_Map3StrInt(b, NodeBuilder()) tests.BenchmarkSpec_Unmarshal_Map3StrInt(b, NodeBuilder())
} }
func BenchmarkSpec_Unmarshal_MapNStrMap3StrInt(b *testing.B) {
tests.BenchmarkSpec_Unmarshal_MapNStrMap3StrInt(b, NodeBuilder())
}
// benchmarks covering traversal --> // benchmarks covering traversal -->
......
...@@ -2,6 +2,7 @@ package tests ...@@ -2,6 +2,7 @@ package tests
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
refmtjson "github.com/polydawn/refmt/json" refmtjson "github.com/polydawn/refmt/json"
...@@ -21,16 +22,47 @@ import ( ...@@ -21,16 +22,47 @@ import (
// and unmarshalling, thus having a back-of-the-envelope baseline to compare. // and unmarshalling, thus having a back-of-the-envelope baseline to compare.
func BenchmarkSpec_Marshal_Map3StrInt(b *testing.B, nb ipld.NodeBuilder) { func BenchmarkSpec_Marshal_Map3StrInt(b *testing.B, nb ipld.NodeBuilder) {
node := mustNodeFromJsonString(nb, corpus.Map3StrInt()) msg := corpus.Map3StrInt()
node := mustNodeFromJsonString(nb, msg)
b.ResetTimer() b.ResetTimer()
var buf bytes.Buffer
var err error var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var buf bytes.Buffer buf = bytes.Buffer{}
err = encoding.Marshal(node, refmtjson.NewEncoder(&buf, refmtjson.EncodeOptions{})) err = encoding.Marshal(node, refmtjson.NewEncoder(&buf, refmtjson.EncodeOptions{}))
sink = buf
} }
b.StopTimer()
if err != nil { if err != nil {
b.Fatalf("marshal errored: %s", err) b.Fatalf("marshal errored: %s", err)
} }
if buf.String() != msg {
b.Fatalf("marshal didn't match corpus")
}
}
func BenchmarkSpec_Marshal_MapNStrMap3StrInt(b *testing.B, nb ipld.NodeBuilder) {
for _, n := range []int{0, 1, 2, 4, 8, 16, 32} {
b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) {
msg := corpus.MapNStrMap3StrInt(n)
node := mustNodeFromJsonString(nb, msg)
b.ResetTimer()
var buf bytes.Buffer
var err error
for i := 0; i < b.N; i++ {
buf = bytes.Buffer{}
err = encoding.Marshal(node, refmtjson.NewEncoder(&buf, refmtjson.EncodeOptions{}))
}
b.StopTimer()
if err != nil {
b.Fatalf("marshal errored: %s", err)
}
if buf.String() != msg {
b.Fatalf("marshal didn't match corpus")
}
})
}
} }
...@@ -2,6 +2,7 @@ package tests ...@@ -2,6 +2,7 @@ package tests
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
refmtjson "github.com/polydawn/refmt/json" refmtjson "github.com/polydawn/refmt/json"
...@@ -20,17 +21,48 @@ import ( ...@@ -20,17 +21,48 @@ import (
// - we can make direct comparisons to the standard library json marshalling // - we can make direct comparisons to the standard library json marshalling
// and unmarshalling, thus having a back-of-the-envelope baseline to compare. // and unmarshalling, thus having a back-of-the-envelope baseline to compare.
var sink interface{}
func BenchmarkSpec_Unmarshal_Map3StrInt(b *testing.B, nb ipld.NodeBuilder) { func BenchmarkSpec_Unmarshal_Map3StrInt(b *testing.B, nb ipld.NodeBuilder) {
msg := corpus.Map3StrInt() msg := corpus.Map3StrInt()
b.ResetTimer() b.ResetTimer()
var node ipld.Node
var err error var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
sink, err = encoding.Unmarshal(nb, refmtjson.NewDecoder(bytes.NewBufferString(msg))) node, err = encoding.Unmarshal(nb, refmtjson.NewDecoder(bytes.NewBufferString(msg)))
} }
b.StopTimer()
if err != nil { if err != nil {
b.Fatalf("unmarshal errored: %s", err) b.Fatalf("unmarshal errored: %s", err)
} }
var buf bytes.Buffer
encoding.Marshal(node, refmtjson.NewEncoder(&buf, refmtjson.EncodeOptions{}))
if buf.String() != msg {
b.Fatalf("remarshal didn't match corpus")
}
}
func BenchmarkSpec_Unmarshal_MapNStrMap3StrInt(b *testing.B, nb ipld.NodeBuilder) {
for _, n := range []int{0, 1, 2, 4, 8, 16, 32} {
b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) {
msg := corpus.MapNStrMap3StrInt(n)
b.ResetTimer()
var node ipld.Node
var err error
for i := 0; i < b.N; i++ {
node, err = encoding.Unmarshal(nb, refmtjson.NewDecoder(bytes.NewBufferString(msg)))
}
b.StopTimer()
if err != nil {
b.Fatalf("unmarshal errored: %s", err)
}
var buf bytes.Buffer
encoding.Marshal(node, refmtjson.NewEncoder(&buf, refmtjson.EncodeOptions{}))
if buf.String() != msg {
b.Fatalf("remarshal didn't match corpus")
}
})
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment