Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create microbenchmarks for ToUnstructured/FromUnstructured. #84082

Merged
merged 1 commit into from
Oct 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"reflect"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -466,3 +467,76 @@ func TestUnstructuredToUnstructuredConversion(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, inUnstructured, outUnstructured)
}

func benchmarkCarp() *testapigroupv1.Carp {
t := metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC)
return &testapigroupv1.Carp{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: testapigroupv1.CarpSpec{
RestartPolicy: "restart",
NodeSelector: map[string]string{
"label1": "value1",
"label2": "value2",
},
ServiceAccountName: "service-account",
HostNetwork: false,
HostPID: true,
Subdomain: "hostname.subdomain.namespace.svc.domain",
},
Status: testapigroupv1.CarpStatus{
Phase: "phase",
Conditions: []testapigroupv1.CarpCondition{
{
Type: "condition1",
Status: "true",
LastProbeTime: t,
LastTransitionTime: t,
Reason: "reason",
Message: "message",
},
},
Message: "message",
Reason: "reason",
HostIP: "1.2.3.4",
},
}
}

func BenchmarkToUnstructured(b *testing.B) {
carp := benchmarkCarp()
converter := runtime.DefaultUnstructuredConverter
b.ResetTimer()

for i := 0; i < b.N; i++ {
result, err := converter.ToUnstructured(carp)
if err != nil {
b.Fatalf("Unexpected conversion error: %v", err)
}
if len(result) != 3 {
b.Errorf("Unexpected conversion result: %#v", result)
}
}
}

func BenchmarkFromUnstructured(b *testing.B) {
carp := benchmarkCarp()
converter := runtime.DefaultUnstructuredConverter
unstr, err := converter.ToUnstructured(carp)
if err != nil {
b.Fatalf("Unexpected conversion error: %v", err)
}
b.ResetTimer()

for i := 0; i < b.N; i++ {
result := testapigroupv1.Carp{}
if err := converter.FromUnstructured(unstr, &result); err != nil {
b.Fatalf("Unexpected conversion error: %v", err)
}
if result.Status.Phase != "phase" {
b.Errorf("Unexpected conversion result: %#v", result)
}
}
}