From 529d636ede63d4e6386bbf2b059f63d96d75d704 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 1 Mar 2022 15:44:34 +0300 Subject: [PATCH] [#376] refs: Add benchmarks for ObjectID slice conversions Signed-off-by: Evgenii Stratonikov --- refs/bench_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 refs/bench_test.go diff --git a/refs/bench_test.go b/refs/bench_test.go new file mode 100644 index 00000000..dc5b3938 --- /dev/null +++ b/refs/bench_test.go @@ -0,0 +1,54 @@ +package refs + +import ( + "math/rand" + "strconv" + "testing" +) + +func BenchmarkObjectIDSlice(b *testing.B) { + for _, size := range []int{0, 1, 50} { + b.Run(strconv.Itoa(size)+" elements", func(b *testing.B) { + benchmarkObjectIDSlice(b, size) + }) + } +} + +func benchmarkObjectIDSlice(b *testing.B, size int) { + ids := make([]*ObjectID, size) + for i := range ids { + ids[i] = new(ObjectID) + ids[i].val = make([]byte, 32) + rand.Read(ids[i].val) + } + raw := ObjectIDListToGRPCMessage(ids) + + b.Run("to grpc message", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + raw := ObjectIDListToGRPCMessage(ids) + if len(raw) != len(ids) { + b.FailNow() + } + } + }) + b.Run("from grpc message", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + ids, err := ObjectIDListFromGRPCMessage(raw) + if err != nil || len(raw) != len(ids) { + b.FailNow() + } + } + }) + b.Run("marshal", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + buf := make([]byte, ObjectIDNestedListSize(1, ids)) + _, err := ObjectIDNestedListMarshal(1, buf, ids) + if err != nil { + b.FailNow() + } + } + }) +}