@@ -19,6 +19,7 @@ package typed_test
19
19
import (
20
20
"io/ioutil"
21
21
"path/filepath"
22
+ "strings"
22
23
"testing"
23
24
24
25
yaml "gopkg.in/yaml.v2"
@@ -29,10 +30,35 @@ func testdata(file string) string {
29
30
return filepath .Join (".." , "internal" , "testdata" , file )
30
31
}
31
32
32
- func BenchmarkFromUnstructured ( b * testing. B ) {
33
- pod , err := ioutil .ReadFile (testdata ( "pod.yaml" ) )
33
+ func read ( file string ) [] byte {
34
+ obj , err := ioutil .ReadFile (file )
34
35
if err != nil {
35
- b .Fatal (err )
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 BenchmarkFromUnstructured (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
+ },
36
62
}
37
63
38
64
s , err := ioutil .ReadFile (testdata ("k8s-schema.yaml" ))
@@ -43,19 +69,24 @@ func BenchmarkFromUnstructured(b *testing.B) {
43
69
if err != nil {
44
70
b .Fatal (err )
45
71
}
46
- pt := parser .Type ("io.k8s.api.core.v1.Pod" )
47
72
48
- obj := map [string ]interface {}{}
49
- if err := yaml .Unmarshal ([]byte (pod ), & obj ); err != nil {
50
- b .Fatal (err )
51
- }
73
+ for _ , test := range tests {
74
+ b .Run (lastPart (test .typename ), func (b * testing.B ) {
75
+ pt := parser .Type (test .typename )
76
+
77
+ obj := map [string ]interface {}{}
78
+ if err := yaml .Unmarshal (test .obj , & obj ); err != nil {
79
+ b .Fatal (err )
80
+ }
52
81
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
- }
82
+ b .ReportAllocs ()
83
+ b .ResetTimer ()
84
+ for n := 0 ; n < b .N ; n ++ {
85
+ if _ , err := pt .FromUnstructured (obj ); err != nil {
86
+ b .Fatal (err )
87
+ }
88
+ }
89
+ })
59
90
}
60
91
61
92
}
0 commit comments