-
Notifications
You must be signed in to change notification settings - Fork 21
/
bson_benchmark_test.go
133 lines (128 loc) · 3.09 KB
/
bson_benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ftdc
import (
"context"
"testing"
"github.com/evergreen-ci/birch"
"github.com/mongodb/ftdc/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type metricHashFunc func(*birch.Document) (string, int)
func BenchmarkHashBSON(b *testing.B) {
for _, impl := range []struct {
Name string
HashFunc metricHashFunc
}{
{
Name: "FNVChecksum",
HashFunc: metricKeyHash,
},
} {
b.Run(impl.Name, func(b *testing.B) {
for _, test := range []struct {
Name string
Doc *birch.Document
}{
{
Name: "FlatSmall",
Doc: testutil.RandFlatDocument(10),
},
{
Name: "FlatLarge",
Doc: testutil.RandFlatDocument(100),
},
{
Name: "ComplexSmall",
Doc: testutil.RandComplexDocument(10, 5),
},
{
Name: "ComplexLarge",
Doc: testutil.RandComplexDocument(100, 5),
},
{
Name: "MoreComplexSmall",
Doc: testutil.RandComplexDocument(10, 2),
},
{
Name: "MoreComplexLarge",
Doc: testutil.RandComplexDocument(100, 2),
},
{
Name: "EventMock",
Doc: testutil.CreateEventRecord(2, 2, 2, 2),
},
} {
b.Run(test.Name, func(b *testing.B) {
var (
h string
num int
)
for n := 0; n < b.N; n++ {
h, num = impl.HashFunc(test.Doc)
}
b.StopTimer()
assert.NotZero(b, num)
assert.NotZero(b, h)
})
}
})
}
}
func BenchmarkDocumentCreation(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, test := range []struct {
Name string
Samples int
Length int
Reference *birch.Document
Metrics []Metric
}{
{
Name: "Flat",
Samples: 1000,
Length: 15,
Reference: testutil.RandFlatDocument(15),
Metrics: produceMockMetrics(ctx, 1000, func() *birch.Document { return testutil.RandFlatDocument(15) }),
},
{
Name: "SmallFlat",
Samples: 1000,
Length: 5,
Reference: testutil.RandFlatDocument(5),
Metrics: produceMockMetrics(ctx, 1000, func() *birch.Document { return testutil.RandFlatDocument(5) }),
},
{
Name: "LargeFlat",
Samples: 1000,
Length: 15,
Reference: testutil.RandFlatDocument(15),
Metrics: produceMockMetrics(ctx, 1000, func() *birch.Document { return testutil.RandFlatDocument(100) }),
},
{
Name: "Complex",
Samples: 1000,
Length: 60,
Reference: testutil.RandComplexDocument(20, 3),
Metrics: produceMockMetrics(ctx, 1000, func() *birch.Document { return testutil.RandComplexDocument(20, 3) }),
},
{
Name: "SmallComplex",
Samples: 1000,
Length: 10,
Reference: testutil.RandComplexDocument(5, 1),
Metrics: produceMockMetrics(ctx, 1000, func() *birch.Document { return testutil.RandComplexDocument(5, 1) }),
},
} {
var doc *birch.Document
b.Run(test.Name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
for i := 0; i < test.Samples; i++ {
doc, _ = restoreDocument(test.Reference, i, test.Metrics, 0)
require.NotNil(b, doc)
require.Equal(b, test.Length, doc.Len())
}
}
})
}
}