...
1
16
17 package typed_test
18
19 import (
20 "io/ioutil"
21 "path/filepath"
22 "strings"
23 "testing"
24
25 yaml "gopkg.in/yaml.v2"
26 "sigs.k8s.io/structured-merge-diff/v4/typed"
27 )
28
29 func testdata(file string) string {
30 return filepath.Join("..", "internal", "testdata", file)
31 }
32
33 func read(file string) []byte {
34 obj, err := ioutil.ReadFile(file)
35 if err != nil {
36 panic(err)
37 }
38 return obj
39 }
40
41 func lastPart(s string) string {
42 return s[strings.LastIndex(s, ".")+1:]
43 }
44
45 func BenchmarkConvertUnstructured(b *testing.B) {
46 tests := []struct {
47 typename string
48 obj []byte
49 }{
50 {
51 typename: "io.k8s.api.core.v1.Pod",
52 obj: read(testdata("pod.yaml")),
53 },
54 {
55 typename: "io.k8s.api.core.v1.Node",
56 obj: read(testdata("node.yaml")),
57 },
58 {
59 typename: "io.k8s.api.core.v1.Endpoints",
60 obj: read(testdata("endpoints.yaml")),
61 },
62 {
63 typename: "io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.CustomResourceDefinition",
64 obj: read(testdata("prometheus-crd.yaml")),
65 },
66 }
67
68 s, err := ioutil.ReadFile(testdata("k8s-schema.yaml"))
69 if err != nil {
70 b.Fatal(err)
71 }
72 parser, err := typed.NewParser(typed.YAMLObject(s))
73 if err != nil {
74 b.Fatal(err)
75 }
76
77 for _, test := range tests {
78 pt := parser.Type(test.typename)
79
80 obj := map[string]interface{}{}
81 if err := yaml.Unmarshal(test.obj, &obj); err != nil {
82 b.Fatal(err)
83 }
84
85 b.Run(lastPart(test.typename), func(b *testing.B) {
86 b.Run("From", func(b *testing.B) {
87 b.ReportAllocs()
88 b.ResetTimer()
89 for n := 0; n < b.N; n++ {
90 if _, err := pt.FromUnstructured(obj); err != nil {
91 b.Fatal(err)
92 }
93 }
94 })
95 b.Run("To", func(b *testing.B) {
96 b.ReportAllocs()
97 b.ResetTimer()
98
99
100
101
102 })
103 })
104 }
105 }
106
View as plain text