gcsbench_test.go raw
1 package gcs_test
2
3 import (
4 "encoding/binary"
5 "math/rand"
6 "testing"
7
8 "github.com/p9c/p9/pkg/gcs"
9 )
10
11 func genRandFilterElements(numElements uint) ([][]byte, error) {
12 testContents := make([][]byte, numElements)
13 for i := range contents {
14 randElem := make([]byte, 32)
15 if _, e = rand.Read(randElem); E.Chk(e) {
16 return nil, e
17 }
18 testContents[i] = randElem
19 }
20 return testContents, nil
21 }
22
23 var (
24 generatedFilter *gcs.Filter
25 // filterErr error
26 )
27
28 // BenchmarkGCSFilterBuild benchmarks building a filter.
29 func BenchmarkGCSFilterBuild50000(b *testing.B) {
30 b.StopTimer()
31 var testKey [gcs.KeySize]byte
32 for i := 0; i < gcs.KeySize; i += 4 {
33 binary.BigEndian.PutUint32(testKey[i:], rand.Uint32())
34 }
35 randFilterElems, genErr := genRandFilterElements(50000)
36 if e != nil {
37 b.Fatalf("unable to generate random item: %v", genErr)
38 }
39 b.StartTimer()
40 var localFilter *gcs.Filter
41 for i := 0; i < b.N; i++ {
42 localFilter, e = gcs.BuildGCSFilter(
43 P, M, key, randFilterElems,
44 )
45 if e != nil {
46 b.Fatalf("unable to generate filter: %v", e)
47 }
48 }
49 generatedFilter = localFilter
50 }
51
52 // BenchmarkGCSFilterBuild benchmarks building a filter.
53 func BenchmarkGCSFilterBuild100000(b *testing.B) {
54 b.StopTimer()
55 var testKey [gcs.KeySize]byte
56 for i := 0; i < gcs.KeySize; i += 4 {
57 binary.BigEndian.PutUint32(testKey[i:], rand.Uint32())
58 }
59 randFilterElems, genErr := genRandFilterElements(100000)
60 if e != nil {
61 b.Fatalf("unable to generate random item: %v", genErr)
62 }
63 b.StartTimer()
64 var localFilter *gcs.Filter
65 for i := 0; i < b.N; i++ {
66 localFilter, e = gcs.BuildGCSFilter(
67 P, M, key, randFilterElems,
68 )
69 if e != nil {
70 b.Fatalf("unable to generate filter: %v", e)
71 }
72 }
73 generatedFilter = localFilter
74 }
75
76 var (
77 match bool
78 )
79
80 // BenchmarkGCSFilterMatch benchmarks querying a filter for a single value.
81 func BenchmarkGCSFilterMatch(b *testing.B) {
82 b.StopTimer()
83 filter, e := gcs.BuildGCSFilter(P, M, key, contents)
84 if e != nil {
85 b.Fatalf("Failed to podbuild filter")
86 }
87 b.StartTimer()
88 var (
89 localMatch bool
90 )
91 for i := 0; i < b.N; i++ {
92 _, e = filter.Match(key, []byte("Nate"))
93 if e != nil {
94 b.Fatalf("unable to match filter: %v", e)
95 }
96 localMatch, e = filter.Match(key, []byte("Nates"))
97 if e != nil {
98 b.Fatalf("unable to match filter: %v", e)
99 }
100 }
101 match = localMatch
102 }
103
104 // BenchmarkGCSFilterMatchAny benchmarks querying a filter for a list of values.
105 func BenchmarkGCSFilterMatchAny(b *testing.B) {
106 b.StopTimer()
107 filter, e := gcs.BuildGCSFilter(P, M, key, contents)
108 if e != nil {
109 b.Fatalf("Failed to podbuild filter")
110 }
111 b.StartTimer()
112 var (
113 localMatch bool
114 )
115 for i := 0; i < b.N; i++ {
116 localMatch, e = filter.MatchAny(key, contents2)
117 if e != nil {
118 b.Fatalf("unable to match filter: %v", e)
119 }
120 }
121 match = localMatch
122 }
123