mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Add benchmarks for yaml marshaling and unmarshaling
This commit is contained in:
		@@ -47,6 +47,7 @@ import (
 | 
			
		||||
	k8s_apps_v1 "k8s.io/kubernetes/pkg/apis/apps/v1"
 | 
			
		||||
	api "k8s.io/kubernetes/pkg/apis/core"
 | 
			
		||||
	k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
 | 
			
		||||
	"sigs.k8s.io/yaml"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate
 | 
			
		||||
@@ -615,3 +616,40 @@ func BenchmarkDecodeIntoJSONCodecGenConfigCompatibleWithStandardLibrary(b *testi
 | 
			
		||||
	}
 | 
			
		||||
	b.StopTimer()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BenchmarkEncodeYAMLMarshal provides a baseline for regular YAML encode performance
 | 
			
		||||
func BenchmarkEncodeYAMLMarshal(b *testing.B) {
 | 
			
		||||
	items := benchmarkItems(b)
 | 
			
		||||
	width := len(items)
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
	for i := 0; i < b.N; i++ {
 | 
			
		||||
		if _, err := yaml.Marshal(&items[i%width]); err != nil {
 | 
			
		||||
			b.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	b.StopTimer()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BenchmarkDecodeYAML provides a baseline for regular YAML decode performance
 | 
			
		||||
func BenchmarkDecodeIntoYAML(b *testing.B) {
 | 
			
		||||
	codec := testapi.Default.Codec()
 | 
			
		||||
	items := benchmarkItems(b)
 | 
			
		||||
	width := len(items)
 | 
			
		||||
	encoded := make([][]byte, width)
 | 
			
		||||
	for i := range items {
 | 
			
		||||
		data, err := runtime.Encode(codec, &items[i])
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			b.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
		encoded[i] = data
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
	for i := 0; i < b.N; i++ {
 | 
			
		||||
		obj := v1.Pod{}
 | 
			
		||||
		if err := yaml.Unmarshal(encoded[i%width], &obj); err != nil {
 | 
			
		||||
			b.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	b.StopTimer()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user