Skip to content

Commit 13c65a2

Browse files
author
Antoine Pelisse
committed
Benchmark FromUnstructured specifically
1 parent b768ed7 commit 13c65a2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

typed/parser_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2019 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 typed_test
18+
19+
import (
20+
"io/ioutil"
21+
"path/filepath"
22+
"testing"
23+
24+
yaml "gopkg.in/yaml.v2"
25+
"sigs.k8s.io/structured-merge-diff/typed"
26+
)
27+
28+
func testdata(file string) string {
29+
return filepath.Join("..", "internal", "testdata", file)
30+
}
31+
32+
func BenchmarkFromUnstructured(b *testing.B) {
33+
pod, err := ioutil.ReadFile(testdata("pod.yaml"))
34+
if err != nil {
35+
b.Fatal(err)
36+
}
37+
38+
s, err := ioutil.ReadFile(testdata("k8s-schema.yaml"))
39+
if err != nil {
40+
b.Fatal(err)
41+
}
42+
parser, err := typed.NewParser(typed.YAMLObject(s))
43+
if err != nil {
44+
b.Fatal(err)
45+
}
46+
pt := parser.Type("io.k8s.api.core.v1.Pod")
47+
48+
obj := map[string]interface{}{}
49+
if err := yaml.Unmarshal([]byte(pod), &obj); err != nil {
50+
b.Fatal(err)
51+
}
52+
53+
b.ReportAllocs()
54+
b.ResetTimer()
55+
for n := 0; n < b.N; n++ {
56+
if _, err := pt.FromUnstructured(obj); err != nil {
57+
b.Fatal(err)
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)