github.com/containerd/Containerd@v1.4.13/benchmark_test.go (about) 1 /* 2 Copyright The containerd 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 containerd 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/containerd/containerd/containers" 24 "github.com/containerd/containerd/oci" 25 ) 26 27 func BenchmarkContainerCreate(b *testing.B) { 28 client, err := newClient(b, address) 29 if err != nil { 30 b.Fatal(err) 31 } 32 defer client.Close() 33 34 ctx, cancel := testContext(b) 35 defer cancel() 36 37 image, err := client.GetImage(ctx, testImage) 38 if err != nil { 39 b.Error(err) 40 return 41 } 42 spec, err := oci.GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, oci.WithImageConfig(image), withTrue()) 43 if err != nil { 44 b.Error(err) 45 return 46 } 47 var containers []Container 48 defer func() { 49 for _, c := range containers { 50 if err := c.Delete(ctx, WithSnapshotCleanup); err != nil { 51 b.Error(err) 52 } 53 } 54 }() 55 56 // reset the timer before creating containers 57 b.ResetTimer() 58 for i := 0; i < b.N; i++ { 59 id := fmt.Sprintf("%s-%d", b.Name(), i) 60 container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithSpec(spec)) 61 if err != nil { 62 b.Error(err) 63 return 64 } 65 containers = append(containers, container) 66 } 67 b.StopTimer() 68 } 69 70 func BenchmarkContainerStart(b *testing.B) { 71 client, err := newClient(b, address) 72 if err != nil { 73 b.Fatal(err) 74 } 75 defer client.Close() 76 77 ctx, cancel := testContext(b) 78 defer cancel() 79 80 image, err := client.GetImage(ctx, testImage) 81 if err != nil { 82 b.Error(err) 83 return 84 } 85 spec, err := oci.GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, oci.WithImageConfig(image), withTrue()) 86 if err != nil { 87 b.Error(err) 88 return 89 } 90 var containers []Container 91 defer func() { 92 for _, c := range containers { 93 if err := c.Delete(ctx, WithSnapshotCleanup); err != nil { 94 b.Error(err) 95 } 96 } 97 }() 98 99 for i := 0; i < b.N; i++ { 100 id := fmt.Sprintf("%s-%d", b.Name(), i) 101 container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithSpec(spec)) 102 if err != nil { 103 b.Error(err) 104 return 105 } 106 containers = append(containers, container) 107 108 } 109 // reset the timer before starting tasks 110 b.ResetTimer() 111 for _, c := range containers { 112 task, err := c.NewTask(ctx, empty()) 113 if err != nil { 114 b.Error(err) 115 return 116 } 117 defer task.Delete(ctx) 118 if err := task.Start(ctx); err != nil { 119 b.Error(err) 120 return 121 } 122 } 123 b.StopTimer() 124 }