Skip to content

Commit 758c177

Browse files
committed
value: Add benchmark for value.Equals
1 parent 1dca6fb commit 758c177

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

value/equals_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package value_test
18+
19+
import (
20+
"io/ioutil"
21+
"path/filepath"
22+
"testing"
23+
24+
"gopkg.in/yaml.v2"
25+
"sigs.k8s.io/structured-merge-diff/v4/value"
26+
)
27+
28+
func testdata(file string) string {
29+
return filepath.Join("..", "internal", "testdata", file)
30+
}
31+
32+
func read(file string) []byte {
33+
s, err := ioutil.ReadFile(file)
34+
if err != nil {
35+
panic(err)
36+
}
37+
return s
38+
}
39+
40+
func BenchmarkEquals(b *testing.B) {
41+
benches := []struct {
42+
filename string
43+
}{
44+
{
45+
filename: "pod.yaml",
46+
},
47+
{
48+
filename: "endpoints.yaml",
49+
},
50+
{
51+
filename: "list.yaml",
52+
},
53+
{
54+
filename: "node.yaml",
55+
},
56+
{
57+
filename: "prometheus-crd.yaml",
58+
},
59+
}
60+
61+
for _, bench := range benches {
62+
b.Run(bench.filename, func(b *testing.B) {
63+
var obj interface{}
64+
err := yaml.Unmarshal(read(testdata(bench.filename)), &obj)
65+
if err != nil {
66+
b.Fatalf("Failed to unmarshal object: %v", err)
67+
}
68+
v := value.NewValueInterface(obj)
69+
b.Run("Equals", func(b *testing.B) {
70+
b.ReportAllocs()
71+
for i := 0; i < b.N; i++ {
72+
if !value.Equals(v, v) {
73+
b.Fatalf("Object should be equal")
74+
}
75+
}
76+
})
77+
b.Run("EqualsUsingFreelist", func(b *testing.B) {
78+
b.ReportAllocs()
79+
a := value.NewFreelistAllocator()
80+
for i := 0; i < b.N; i++ {
81+
if !value.EqualsUsing(a, v, v) {
82+
b.Fatalf("Object should be equal")
83+
}
84+
}
85+
})
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)