deep_copy_test.go 4.7 KB

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