k8s.io/kubernetes@v1.29.3/pkg/api/testing/deep_copy_test.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testing 18 19 import ( 20 "os" 21 "testing" 22 "time" 23 24 v1 "k8s.io/api/core/v1" 25 apiequality "k8s.io/apimachinery/pkg/api/equality" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/apimachinery/pkg/types" 29 "k8s.io/kubernetes/pkg/api/legacyscheme" 30 api "k8s.io/kubernetes/pkg/apis/core" 31 ) 32 33 func parseTimeOrDie(ts string) metav1.Time { 34 t, err := time.Parse(time.RFC3339, ts) 35 if err != nil { 36 panic(err) 37 } 38 return metav1.Time{Time: t} 39 } 40 41 var benchmarkPod = api.Pod{ 42 TypeMeta: metav1.TypeMeta{ 43 Kind: "Pod", 44 APIVersion: "v1", 45 }, 46 ObjectMeta: metav1.ObjectMeta{ 47 Name: "etcd-server-e2e-test-wojtekt-master", 48 Namespace: "default", 49 UID: types.UID("a671734a-e8e5-11e4-8fde-42010af09327"), 50 ResourceVersion: "22", 51 CreationTimestamp: parseTimeOrDie("2015-04-22T11:49:36Z"), 52 }, 53 Spec: api.PodSpec{ 54 Volumes: []api.Volume{ 55 { 56 Name: "varetcd", 57 VolumeSource: api.VolumeSource{ 58 HostPath: &api.HostPathVolumeSource{ 59 Path: "/mnt/master-pd/var/etcd", 60 }, 61 }, 62 }, 63 }, 64 Containers: []api.Container{ 65 { 66 Name: "etcd-container", 67 Image: "registry.k8s.io/etcd:2.0.9", 68 Command: []string{ 69 "/usr/local/bin/etcd", 70 "--addr", 71 "127.0.0.1:2379", 72 "--bind-addr", 73 "127.0.0.1:2379", 74 "--data-dir", 75 "/var/etcd/data", 76 }, 77 Ports: []api.ContainerPort{ 78 { 79 Name: "serverport", 80 HostPort: 2380, 81 ContainerPort: 2380, 82 Protocol: "TCP", 83 }, 84 { 85 Name: "clientport", 86 HostPort: 2379, 87 ContainerPort: 2379, 88 Protocol: "TCP", 89 }, 90 }, 91 VolumeMounts: []api.VolumeMount{ 92 { 93 Name: "varetcd", 94 MountPath: "/var/etcd", 95 }, 96 }, 97 TerminationMessagePath: "/dev/termination-log", 98 ImagePullPolicy: api.PullIfNotPresent, 99 }, 100 }, 101 RestartPolicy: api.RestartPolicyAlways, 102 DNSPolicy: api.DNSClusterFirst, 103 NodeName: "e2e-test-wojtekt-master", 104 }, 105 Status: api.PodStatus{ 106 Phase: api.PodRunning, 107 Conditions: []api.PodCondition{ 108 { 109 Type: api.PodReady, 110 Status: api.ConditionTrue, 111 }, 112 }, 113 ContainerStatuses: []api.ContainerStatus{ 114 { 115 Name: "etcd-container", 116 State: api.ContainerState{ 117 Running: &api.ContainerStateRunning{ 118 StartedAt: parseTimeOrDie("2015-04-22T11:49:32Z"), 119 }, 120 }, 121 Ready: true, 122 RestartCount: 0, 123 Image: "registry.k8s.io/etcd:2.0.9", 124 ImageID: "docker://b6b9a86dc06aa1361357ca1b105feba961f6a4145adca6c54e142c0be0fe87b0", 125 ContainerID: "docker://3cbbf818f1addfc252957b4504f56ef2907a313fe6afc47fc75373674255d46d", 126 }, 127 }, 128 }, 129 } 130 131 func BenchmarkPodCopy(b *testing.B) { 132 var result *api.Pod 133 for i := 0; i < b.N; i++ { 134 result = benchmarkPod.DeepCopy() 135 } 136 if !apiequality.Semantic.DeepEqual(benchmarkPod, *result) { 137 b.Fatalf("Incorrect copy: expected %v, got %v", benchmarkPod, *result) 138 } 139 } 140 141 func BenchmarkNodeCopy(b *testing.B) { 142 data, err := os.ReadFile("node_example.json") 143 if err != nil { 144 b.Fatalf("Unexpected error while reading file: %v", err) 145 } 146 var node api.Node 147 if err := runtime.DecodeInto(legacyscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), data, &node); err != nil { 148 b.Fatalf("Unexpected error decoding node: %v", err) 149 } 150 151 var result *api.Node 152 for i := 0; i < b.N; i++ { 153 result = node.DeepCopy() 154 } 155 if !apiequality.Semantic.DeepEqual(node, *result) { 156 b.Fatalf("Incorrect copy: expected %v, got %v", node, *result) 157 } 158 } 159 160 func BenchmarkReplicationControllerCopy(b *testing.B) { 161 data, err := os.ReadFile("replication_controller_example.json") 162 if err != nil { 163 b.Fatalf("Unexpected error while reading file: %v", err) 164 } 165 var replicationController api.ReplicationController 166 if err := runtime.DecodeInto(legacyscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), data, &replicationController); err != nil { 167 b.Fatalf("Unexpected error decoding node: %v", err) 168 } 169 170 var result *api.ReplicationController 171 for i := 0; i < b.N; i++ { 172 result = replicationController.DeepCopy() 173 } 174 if !apiequality.Semantic.DeepEqual(replicationController, *result) { 175 b.Fatalf("Incorrect copy: expected %v, got %v", replicationController, *result) 176 } 177 }