From 8e57ee91114c30ee73b65653e306d1c1226af470 Mon Sep 17 00:00:00 2001 From: Justin SB Date: Wed, 3 Mar 2021 08:53:06 -0500 Subject: [PATCH] Add benchmarks to measure impact of swagger parsing Example results: BenchmarkSwaggerParse-72 2 882910241 ns/op BenchmarkAsssetUnpack-72 62 19654866 ns/op --- kyaml/openapi/openapi_benchmark_test.go | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 kyaml/openapi/openapi_benchmark_test.go diff --git a/kyaml/openapi/openapi_benchmark_test.go b/kyaml/openapi/openapi_benchmark_test.go new file mode 100644 index 0000000000..4c8b86957f --- /dev/null +++ b/kyaml/openapi/openapi_benchmark_test.go @@ -0,0 +1,47 @@ +// Copyright 2021 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package openapi + +import ( + "path/filepath" + "testing" + + "github.com/go-openapi/spec" + "sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi" +) + +// Benchmark for swagger parsing (UnmarshalJSON) +func BenchmarkSwaggerUnmarshalJSON(t *testing.B) { + version := kubernetesOpenAPIDefaultVersion + + // parse the swagger, this should never fail + assetName := filepath.Join( + "kubernetesapi", + version, + "swagger.json") + + b := kubernetesapi.OpenAPIMustAsset[version](assetName) + + for i := 0; i < t.N; i++ { + var swagger spec.Swagger + if err := swagger.UnmarshalJSON(b); err != nil { + t.Fatalf("swagger.UnmarshalJSON failed: %v", err) + } + } +} + +// Benchmark for loading assets packed into the binary +func BenchmarkAssetRead(t *testing.B) { + for i := 0; i < t.N; i++ { + version := kubernetesOpenAPIDefaultVersion + + // parse the swagger, this should never fail + assetName := filepath.Join( + "kubernetesapi", + version, + "swagger.json") + + kubernetesapi.OpenAPIMustAsset[version](assetName) + } +}