deep_copy_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 api_test
  14. import (
  15. "io/ioutil"
  16. "testing"
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/types"
  23. )
  24. func parseTimeOrDie(ts string) unversioned.Time {
  25. t, err := time.Parse(time.RFC3339, ts)
  26. if err != nil {
  27. panic(err)
  28. }
  29. return unversioned.Time{Time: t}
  30. }
  31. var benchmarkPod api.Pod = api.Pod{
  32. TypeMeta: unversioned.TypeMeta{
  33. Kind: "Pod",
  34. APIVersion: "v1",
  35. },
  36. ObjectMeta: api.ObjectMeta{
  37. Name: "etcd-server-e2e-test-wojtekt-master",
  38. Namespace: "default",
  39. SelfLink: "/api/v1/namespaces/default/pods/etcd-server-e2e-test-wojtekt-master",
  40. UID: types.UID("a671734a-e8e5-11e4-8fde-42010af09327"),
  41. ResourceVersion: "22",
  42. CreationTimestamp: parseTimeOrDie("2015-04-22T11:49:36Z"),
  43. Annotations: map[string]string{
  44. "kubernetes.io/config.mirror": "mirror",
  45. "kubernetes.io/config.source": "file",
  46. },
  47. },
  48. Spec: api.PodSpec{
  49. Volumes: []api.Volume{
  50. {
  51. Name: "varetcd",
  52. VolumeSource: api.VolumeSource{
  53. HostPath: &api.HostPathVolumeSource{
  54. Path: "/mnt/master-pd/var/etcd",
  55. },
  56. },
  57. },
  58. },
  59. Containers: []api.Container{
  60. {
  61. Name: "etcd-container",
  62. Image: "gcr.io/google_containers/etcd:2.0.9",
  63. Command: []string{
  64. "/usr/local/bin/etcd",
  65. "--addr",
  66. "127.0.0.1:2379",
  67. "--bind-addr",
  68. "127.0.0.1:2379",
  69. "--data-dir",
  70. "/var/etcd/data",
  71. },
  72. Ports: []api.ContainerPort{
  73. {
  74. Name: "serverport",
  75. HostPort: 2380,
  76. ContainerPort: 2380,
  77. Protocol: "TCP",
  78. },
  79. {
  80. Name: "clientport",
  81. HostPort: 2379,
  82. ContainerPort: 2379,
  83. Protocol: "TCP",
  84. },
  85. },
  86. VolumeMounts: []api.VolumeMount{
  87. {
  88. Name: "varetcd",
  89. MountPath: "/var/etcd",
  90. },
  91. },
  92. TerminationMessagePath: "/dev/termination-log",
  93. ImagePullPolicy: api.PullIfNotPresent,
  94. },
  95. },
  96. RestartPolicy: api.RestartPolicyAlways,
  97. DNSPolicy: api.DNSClusterFirst,
  98. NodeName: "e2e-test-wojtekt-master",
  99. },
  100. Status: api.PodStatus{
  101. Phase: api.PodRunning,
  102. Conditions: []api.PodCondition{
  103. {
  104. Type: api.PodReady,
  105. Status: api.ConditionTrue,
  106. },
  107. },
  108. ContainerStatuses: []api.ContainerStatus{
  109. {
  110. Name: "etcd-container",
  111. State: api.ContainerState{
  112. Running: &api.ContainerStateRunning{
  113. StartedAt: parseTimeOrDie("2015-04-22T11:49:32Z"),
  114. },
  115. },
  116. Ready: true,
  117. RestartCount: 0,
  118. Image: "gcr.io/google_containers/etcd:2.0.9",
  119. ImageID: "docker://b6b9a86dc06aa1361357ca1b105feba961f6a4145adca6c54e142c0be0fe87b0",
  120. ContainerID: "docker://3cbbf818f1addfc252957b4504f56ef2907a313fe6afc47fc75373674255d46d",
  121. },
  122. },
  123. },
  124. }
  125. func BenchmarkPodCopy(b *testing.B) {
  126. var result *api.Pod
  127. for i := 0; i < b.N; i++ {
  128. obj, err := api.Scheme.DeepCopy(&benchmarkPod)
  129. if err != nil {
  130. b.Fatalf("Unexpected error copying pod: %v", err)
  131. }
  132. result = obj.(*api.Pod)
  133. }
  134. if !api.Semantic.DeepEqual(benchmarkPod, *result) {
  135. b.Fatalf("Incorrect copy: expected %v, got %v", benchmarkPod, *result)
  136. }
  137. }
  138. func BenchmarkNodeCopy(b *testing.B) {
  139. data, err := ioutil.ReadFile("node_example.json")
  140. if err != nil {
  141. b.Fatalf("Unexpected error while reading file: %v", err)
  142. }
  143. var node api.Node
  144. if err := runtime.DecodeInto(testapi.Default.Codec(), data, &node); err != nil {
  145. b.Fatalf("Unexpected error decoding node: %v", err)
  146. }
  147. var result *api.Node
  148. for i := 0; i < b.N; i++ {
  149. obj, err := api.Scheme.DeepCopy(&node)
  150. if err != nil {
  151. b.Fatalf("Unexpected error copying node: %v", err)
  152. }
  153. result = obj.(*api.Node)
  154. }
  155. if !api.Semantic.DeepEqual(node, *result) {
  156. b.Fatalf("Incorrect copy: expected %v, got %v", node, *result)
  157. }
  158. }
  159. func BenchmarkReplicationControllerCopy(b *testing.B) {
  160. data, err := ioutil.ReadFile("replication_controller_example.json")
  161. if err != nil {
  162. b.Fatalf("Unexpected error while reading file: %v", err)
  163. }
  164. var replicationController api.ReplicationController
  165. if err := runtime.DecodeInto(testapi.Default.Codec(), data, &replicationController); err != nil {
  166. b.Fatalf("Unexpected error decoding node: %v", err)
  167. }
  168. var result *api.ReplicationController
  169. for i := 0; i < b.N; i++ {
  170. obj, err := api.Scheme.DeepCopy(&replicationController)
  171. if err != nil {
  172. b.Fatalf("Unexpected error copying replication controller: %v", err)
  173. }
  174. result = obj.(*api.ReplicationController)
  175. }
  176. if !api.Semantic.DeepEqual(replicationController, *result) {
  177. b.Fatalf("Incorrect copy: expected %v, got %v", replicationController, *result)
  178. }
  179. }