From ffc140e76eb2cfa0326161a403d715d7eb8f895a Mon Sep 17 00:00:00 2001 From: Eric Myhre Date: Wed, 22 Jan 2020 16:04:25 +0100 Subject: [PATCH] Entersen some test and benchmark setup. I'm wary as heck at introducing any amount of abstraction in benchmarks, because sometimes, it starts to foment lies in the most unexpected ways. However, I did several fairly long runs with and without this contraction, and they seem to vary on the order of single nanoseconds -- while the noise between test runs varies by a few dozen. So, this appears to be safe to move on. --- _rsrch/nodesolution/impls/bench_test.go | 77 +++++++------------------ _rsrch/nodesolution/impls/map_test.go | 37 ++++-------- must/must.go | 9 +++ 3 files changed, 40 insertions(+), 83 deletions(-) diff --git a/_rsrch/nodesolution/impls/bench_test.go b/_rsrch/nodesolution/impls/bench_test.go index 305430a..9bd9457 100644 --- a/_rsrch/nodesolution/impls/bench_test.go +++ b/_rsrch/nodesolution/impls/bench_test.go @@ -5,6 +5,7 @@ import ( "testing" ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution" + "github.com/ipld/go-ipld-prime/must" ) var sink interface{} @@ -22,9 +23,7 @@ func BenchmarkMap3nBaselineNativeMapAssignSimpleKeys(b *testing.B) { func BenchmarkMap3nBaselineJsonUnmarshalMapSimpleKeys(b *testing.B) { for i := 0; i < b.N; i++ { var x = make(map[string]int, 3) - if err := json.Unmarshal([]byte(`{"whee":1,"woot":2,"waga":3}`), &x); err != nil { - panic(err) - } + must.NotError(json.Unmarshal([]byte(`{"whee":1,"woot":2,"waga":3}`), &x)) sink = x } } @@ -37,27 +36,13 @@ func BenchmarkMap3nFeedGenericMapSimpleKeys(b *testing.B) { if err != nil { panic(err) } - if err := ma.AssembleKey().AssignString("whee"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(1); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("woot"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(2); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("waga"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(3); err != nil { - panic(err) - } - if err := ma.Done(); err != nil { - panic(err) - } + must.NotError(ma.AssembleKey().AssignString("whee")) + must.NotError(ma.AssembleValue().AssignInt(1)) + must.NotError(ma.AssembleKey().AssignString("woot")) + must.NotError(ma.AssembleValue().AssignInt(2)) + must.NotError(ma.AssembleKey().AssignString("waga")) + must.NotError(ma.AssembleValue().AssignInt(3)) + must.NotError(ma.Done()) if n, err := nb.Build(); err != nil { panic(err) } else { @@ -74,27 +59,13 @@ func BenchmarkMap3nFeedGennedMapSimpleKeys(b *testing.B) { if err != nil { panic(err) } - if err := ma.AssembleKey().AssignString("whee"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(1); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("woot"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(2); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("waga"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(3); err != nil { - panic(err) - } - if err := ma.Done(); err != nil { - panic(err) - } + must.NotError(ma.AssembleKey().AssignString("whee")) + must.NotError(ma.AssembleValue().AssignInt(1)) + must.NotError(ma.AssembleKey().AssignString("woot")) + must.NotError(ma.AssembleValue().AssignInt(2)) + must.NotError(ma.AssembleKey().AssignString("waga")) + must.NotError(ma.AssembleValue().AssignInt(3)) + must.NotError(ma.Done()) if n, err := nb.Build(); err != nil { panic(err) } else { @@ -114,27 +85,19 @@ func BenchmarkMap3nFeedGennedMapSimpleKeysDirectly(b *testing.B) { if va, err := ma.AssembleDirectly("whee"); err != nil { panic(err) } else { - if err := va.AssignInt(1); err != nil { - panic(err) - } + must.NotError(va.AssignInt(1)) } if va, err := ma.AssembleDirectly("woot"); err != nil { panic(err) } else { - if err := va.AssignInt(2); err != nil { - panic(err) - } + must.NotError(va.AssignInt(2)) } if va, err := ma.AssembleDirectly("waga"); err != nil { panic(err) } else { - if err := va.AssignInt(3); err != nil { - panic(err) - } - } - if err := ma.Done(); err != nil { - panic(err) + must.NotError(va.AssignInt(3)) } + must.NotError(ma.Done()) if n, err := nb.Build(); err != nil { panic(err) } else { diff --git a/_rsrch/nodesolution/impls/map_test.go b/_rsrch/nodesolution/impls/map_test.go index 577194d..4189aab 100644 --- a/_rsrch/nodesolution/impls/map_test.go +++ b/_rsrch/nodesolution/impls/map_test.go @@ -3,9 +3,10 @@ package impls import ( "testing" - ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution" - wish "github.com/warpfork/go-wish" + + ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution" + "github.com/ipld/go-ipld-prime/must" ) func TestGennedMapIntValues(t *testing.T) { @@ -19,30 +20,14 @@ func CheckMaps(t *testing.T, ns ipld.NodeStyle) { t.Run("map node, str:int, 3 entries", func(t *testing.T) { nb := ns.NewBuilder() ma, err := nb.BeginMap(3) - if err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("whee"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(1); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("woot"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(2); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("waga"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(3); err != nil { - panic(err) - } - if err := ma.Done(); err != nil { - panic(err) - } + must.NotError(err) + must.NotError(ma.AssembleKey().AssignString("whee")) + must.NotError(ma.AssembleValue().AssignInt(1)) + must.NotError(ma.AssembleKey().AssignString("woot")) + must.NotError(ma.AssembleValue().AssignInt(2)) + must.NotError(ma.AssembleKey().AssignString("waga")) + must.NotError(ma.AssembleValue().AssignInt(3)) + must.NotError(ma.Done()) n, err := nb.Build() if err != nil { wish.Require(t, err, wish.ShouldEqual, nil) diff --git a/must/must.go b/must/must.go index 134c543..c15a0b6 100644 --- a/must/must.go +++ b/must/must.go @@ -21,6 +21,15 @@ import ( "github.com/ipld/go-ipld-prime/impl/typed" ) +// must.NotError simply panics if given an error. +// It helps turn multi-line code into one-liner code in situations where +// you simply don't care. +func NotError(e error) { + if e != nil { + panic(e) + } +} + // must.Node helps write pointfree/chainable-style code // by taking a Node and an error and transforming any error into a panic. // -- GitLab