From d0e00d2bdefaf25107dfc679cf1a336a85a2c904 Mon Sep 17 00:00:00 2001 From: Eric Chlebek Date: Tue, 21 Nov 2017 20:48:26 -0800 Subject: [PATCH] Add benchmarks. Refs #586 goos: linux goarch: amd64 pkg: github.com/sensu/sensu-go/types/dynamic BenchmarkQueryGovaluateSimple-4 500000 2069 ns/op 992 B/op 25 allocs/op BenchmarkQueryGovaluateComplex-4 200000 6212 ns/op 2544 B/op 75 allocs/op BenchmarkUnmarshal-4 200000 6492 ns/op 6568 B/op 57 allocs/op BenchmarkMarshal-4 300000 4556 ns/op 6512 B/op 40 allocs/op PASS ok github.com/sensu/sensu-go/types/dynamic 6.172s --- types/dynamic/dynamic_test.go | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/types/dynamic/dynamic_test.go b/types/dynamic/dynamic_test.go index f2511d2bb7..71a5f0345d 100644 --- a/types/dynamic/dynamic_test.go +++ b/types/dynamic/dynamic_test.go @@ -238,6 +238,21 @@ func TestQueryGovaluateSimple(t *testing.T) { require.Equal(t, true, result) } +func BenchmarkQueryGovaluateSimple(b *testing.B) { + m := MyType{ + extended: []byte(`{"hello":5}`), + } + + expr, err := govaluate.NewEvaluableExpression("hello == 5") + require.Nil(b, err) + require.NotNil(b, expr) + b.ResetTimer() + + for i := 0; i < b.N; i++ { + expr.Eval(m) + } +} + func TestQueryGovaluateComplex(t *testing.T) { m := MyType{ extended: []byte(`{"hello":{"foo":5,"bar":6.0}}`), @@ -268,6 +283,21 @@ func TestQueryGovaluateComplex(t *testing.T) { require.Equal(t, true, result) } +func BenchmarkQueryGovaluateComplex(b *testing.B) { + m := MyType{ + extended: []byte(`{"hello":{"foo":5,"bar":6.0}}`), + } + + expr, err := govaluate.NewEvaluableExpression("hello.Foo == 5") + require.Nil(b, err) + require.NotNil(b, expr) + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, _ = expr.Eval(m) + } +} + func TestMarshalUnmarshal(t *testing.T) { data := []byte(`{"bar":null,"foo":"hello","a":10,"b":"c"}`) var m MyType @@ -278,3 +308,21 @@ func TestMarshalUnmarshal(t *testing.T) { require.Nil(t, err) assert.Equal(t, data, b) } + +func BenchmarkUnmarshal(b *testing.B) { + data := []byte(`{"bar":null,"foo":"hello","a":10,"b":"c"}`) + var m MyType + for i := 0; i < b.N; i++ { + _ = json.Unmarshal(data, &m) + } +} + +func BenchmarkMarshal(b *testing.B) { + data := []byte(`{"bar":null,"foo":"hello","a":10,"b":"c"}`) + var m MyType + json.Unmarshal(data, &m) + b.ResetTimer() + for i := 0; i < b.N; i++ { + json.Marshal(m) + } +}