Skip to content

Commit 03f010c

Browse files
committed
Add a micro benchmark for fieldpath.
1 parent 8b1ef26 commit 03f010c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

fieldpath/set_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,109 @@ limitations under the License.
1717
package fieldpath
1818

1919
import (
20+
"fmt"
21+
"math/rand"
2022
"testing"
2123

2224
"sigs.k8s.io/structured-merge-diff/value"
2325
)
2426

27+
type randomPathAlphabet []PathElement
28+
29+
func (a randomPathAlphabet) makePath(minLen, maxLen int) Path {
30+
n := minLen
31+
if minLen < maxLen {
32+
n += rand.Intn(maxLen - minLen)
33+
}
34+
var p Path
35+
for i := 0; i < n; i++ {
36+
p = append(p, a[rand.Intn(len(a))])
37+
}
38+
return p
39+
}
40+
41+
var randomPathMaker = randomPathAlphabet(MakePathOrDie(
42+
"aaa",
43+
"aab",
44+
"aac",
45+
"aad",
46+
"aae",
47+
"aaf",
48+
KeyByFields("name", value.StringValue("first")),
49+
KeyByFields("name", value.StringValue("second")),
50+
KeyByFields("port", value.IntValue(443), "protocol", value.StringValue("tcp")),
51+
KeyByFields("port", value.IntValue(443), "protocol", value.StringValue("udp")),
52+
value.IntValue(1),
53+
value.IntValue(2),
54+
value.IntValue(3),
55+
value.StringValue("aa"),
56+
value.StringValue("ab"),
57+
value.BooleanValue(true),
58+
1, 2, 3, 4,
59+
))
60+
61+
func BenchmarkFieldSet(b *testing.B) {
62+
cases := []struct {
63+
size int
64+
minPathLen int
65+
maxPathLen int
66+
}{
67+
//{10, 1, 2},
68+
{20, 2, 3},
69+
{50, 2, 4},
70+
{100, 3, 6},
71+
{500, 3, 7},
72+
{1000, 3, 8},
73+
}
74+
for i := range cases {
75+
here := cases[i]
76+
makeSet := func() *Set {
77+
x := NewSet()
78+
for j := 0; j < here.size; j++ {
79+
x.Insert(randomPathMaker.makePath(here.minPathLen, here.maxPathLen))
80+
}
81+
return x
82+
}
83+
operands := make([]*Set, 500)
84+
for i := range operands {
85+
operands[i] = makeSet()
86+
}
87+
randOperand := func() *Set { return operands[rand.Intn(len(operands))] }
88+
89+
b.Run(fmt.Sprintf("insert-%v", here.size), func(b *testing.B) {
90+
b.ReportAllocs()
91+
for i := 0; i < b.N; i++ {
92+
makeSet()
93+
}
94+
})
95+
b.Run(fmt.Sprintf("has-%v", here.size), func(b *testing.B) {
96+
b.ReportAllocs()
97+
for i := 0; i < b.N; i++ {
98+
randOperand().Has(randomPathMaker.makePath(here.minPathLen, here.maxPathLen))
99+
}
100+
})
101+
102+
b.Run(fmt.Sprintf("union-%v", here.size), func(b *testing.B) {
103+
b.ReportAllocs()
104+
for i := 0; i < b.N; i++ {
105+
randOperand().Union(randOperand())
106+
}
107+
})
108+
b.Run(fmt.Sprintf("intersection-%v", here.size), func(b *testing.B) {
109+
b.ReportAllocs()
110+
for i := 0; i < b.N; i++ {
111+
randOperand().Intersection(randOperand())
112+
}
113+
})
114+
b.Run(fmt.Sprintf("difference-%v", here.size), func(b *testing.B) {
115+
b.ReportAllocs()
116+
for i := 0; i < b.N; i++ {
117+
randOperand().Difference(randOperand())
118+
}
119+
})
120+
}
121+
}
122+
25123
func TestSetInsertHas(t *testing.T) {
26124
s1 := NewSet(
27125
MakePathOrDie("foo", 0, "bar", "baz"),

0 commit comments

Comments
 (0)