github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_api_service_bench_test.go (about) 1 // Copyright 2015 The rkt Authors 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build host coreos src kvm 16 17 package main 18 19 import ( 20 "fmt" 21 "os" 22 "sync" 23 "testing" 24 25 "github.com/coreos/gexpect" 26 "github.com/rkt/rkt/api/v1alpha" 27 "github.com/rkt/rkt/tests/testutils" 28 "golang.org/x/net/context" 29 "google.golang.org/grpc" 30 ) 31 32 func setup() (*testutils.RktRunCtx, *gexpect.ExpectSubprocess, v1alpha.PublicAPIClient, *grpc.ClientConn, string) { 33 t := new(testing.T) // Print no messages. 34 ctx := testutils.NewRktRunCtx() 35 svc := startAPIService(t, ctx) 36 c, conn := newAPIClientOrFail(t, "localhost:15441") 37 imagePath := patchTestACI("rkt-inspect-print.aci", "--exec=/inspect --print-msg=HELLO_API") 38 39 return ctx, svc, c, conn, imagePath 40 } 41 42 func cleanup(ctx *testutils.RktRunCtx, svc *gexpect.ExpectSubprocess, conn *grpc.ClientConn, imagePath string) { 43 t := new(testing.T) // Print no messages. 44 os.Remove(imagePath) 45 conn.Close() 46 stopAPIService(t, svc) 47 ctx.Cleanup() 48 } 49 50 func launchPods(ctx *testutils.RktRunCtx, numOfPods int, imagePath string) { 51 t := new(testing.T) // Print no messages. 52 cmd := fmt.Sprintf("%s --insecure-options=all run %s", ctx.Cmd(), imagePath) 53 54 var wg sync.WaitGroup 55 wg.Add(numOfPods) 56 for i := 0; i < numOfPods; i++ { 57 go func() { 58 spawnAndWaitOrFail(t, cmd, 0) 59 wg.Done() 60 }() 61 } 62 wg.Wait() 63 } 64 65 func benchListPods(b *testing.B, c v1alpha.PublicAPIClient, detail bool) { 66 b.ResetTimer() 67 for i := 0; i < b.N; i++ { 68 _, err := c.ListPods(context.Background(), &v1alpha.ListPodsRequest{Detail: detail}) 69 if err != nil { 70 b.Error(err) 71 } 72 } 73 b.StopTimer() 74 } 75 76 func benchInspectPod(b *testing.B, c v1alpha.PublicAPIClient) { 77 resp, err := c.ListPods(context.Background(), &v1alpha.ListPodsRequest{}) 78 if err != nil { 79 b.Fatalf("Unexpected error: %v", err) 80 } 81 82 b.ResetTimer() 83 for i := 0; i < b.N; i++ { 84 _, err := c.InspectPod(context.Background(), &v1alpha.InspectPodRequest{Id: resp.Pods[0].Id}) 85 if err != nil { 86 b.Error(err) 87 } 88 } 89 b.StopTimer() 90 } 91 92 func fetchImages(ctx *testutils.RktRunCtx, numOfImages int) { 93 t := new(testing.T) // Print no messages. 94 95 var wg sync.WaitGroup 96 wg.Add(numOfImages) 97 for i := 0; i < numOfImages; i++ { 98 go func(i int) { 99 _, err := patchImportAndFetchHash(fmt.Sprintf("rkt-inspect-sleep-%d.aci", i), []string{"--exec=/inspect"}, t, ctx) 100 if err != nil { 101 t.Fatalf("%v", err) 102 } 103 wg.Done() 104 }(i) 105 } 106 wg.Wait() 107 } 108 109 func benchListImages(b *testing.B, c v1alpha.PublicAPIClient, detail bool) { 110 b.ResetTimer() 111 for i := 0; i < b.N; i++ { 112 _, err := c.ListImages(context.Background(), &v1alpha.ListImagesRequest{Detail: detail}) 113 if err != nil { 114 b.Error(err) 115 } 116 } 117 b.StopTimer() 118 } 119 120 func benchInspectImage(b *testing.B, c v1alpha.PublicAPIClient) { 121 resp, err := c.ListImages(context.Background(), &v1alpha.ListImagesRequest{}) 122 if err != nil { 123 b.Fatalf("Unexpected error: %v", err) 124 } 125 126 b.ResetTimer() 127 for i := 0; i < b.N; i++ { 128 _, err := c.InspectImage(context.Background(), &v1alpha.InspectImageRequest{Id: resp.Images[0].Id}) 129 if err != nil { 130 b.Error(err) 131 } 132 } 133 b.StopTimer() 134 } 135 136 func BenchmarkList1PodNoDetail(b *testing.B) { 137 ctx, svc, client, conn, imagePath := setup() 138 defer cleanup(ctx, svc, conn, imagePath) 139 140 launchPods(ctx, 1, imagePath) 141 benchListPods(b, client, false) 142 } 143 144 func BenchmarkList10PodsNoDetail(b *testing.B) { 145 ctx, svc, client, conn, imagePath := setup() 146 defer cleanup(ctx, svc, conn, imagePath) 147 148 launchPods(ctx, 10, imagePath) 149 benchListPods(b, client, false) 150 } 151 152 func BenchmarkList100PodsNoDetail(b *testing.B) { 153 ctx, svc, client, conn, imagePath := setup() 154 defer cleanup(ctx, svc, conn, imagePath) 155 156 launchPods(ctx, 100, imagePath) 157 benchListPods(b, client, false) 158 } 159 160 func BenchmarkList1PodDetail(b *testing.B) { 161 ctx, svc, client, conn, imagePath := setup() 162 defer cleanup(ctx, svc, conn, imagePath) 163 164 launchPods(ctx, 1, imagePath) 165 benchListPods(b, client, true) 166 } 167 168 func BenchmarkList10PodsDetail(b *testing.B) { 169 ctx, svc, client, conn, imagePath := setup() 170 defer cleanup(ctx, svc, conn, imagePath) 171 172 launchPods(ctx, 10, imagePath) 173 benchListPods(b, client, true) 174 } 175 176 func BenchmarkList100PodsDetail(b *testing.B) { 177 ctx, svc, client, conn, imagePath := setup() 178 defer cleanup(ctx, svc, conn, imagePath) 179 180 launchPods(ctx, 100, imagePath) 181 benchListPods(b, client, true) 182 } 183 184 func BenchmarkInspectPodIn10Pods(b *testing.B) { 185 ctx, svc, client, conn, imagePath := setup() 186 defer cleanup(ctx, svc, conn, imagePath) 187 188 launchPods(ctx, 10, imagePath) 189 benchInspectPod(b, client) 190 } 191 192 func BenchmarkInspectPodIn100Pods(b *testing.B) { 193 ctx, svc, client, conn, imagePath := setup() 194 defer cleanup(ctx, svc, conn, imagePath) 195 196 launchPods(ctx, 100, imagePath) 197 benchInspectPod(b, client) 198 } 199 200 func BenchmarkList1ImageNoDetail(b *testing.B) { 201 ctx, svc, client, conn, imagePath := setup() 202 defer cleanup(ctx, svc, conn, imagePath) 203 204 fetchImages(ctx, 1) 205 benchListImages(b, client, false) 206 } 207 208 func BenchmarkList10ImagesNoDetail(b *testing.B) { 209 ctx, svc, client, conn, imagePath := setup() 210 defer cleanup(ctx, svc, conn, imagePath) 211 212 fetchImages(ctx, 10) 213 benchListImages(b, client, false) 214 } 215 216 func BenchmarkList100ImagesNoDetail(b *testing.B) { 217 ctx, svc, client, conn, imagePath := setup() 218 defer cleanup(ctx, svc, conn, imagePath) 219 220 fetchImages(ctx, 100) 221 benchListImages(b, client, false) 222 } 223 224 func BenchmarkList1ImageDetail(b *testing.B) { 225 ctx, svc, client, conn, imagePath := setup() 226 defer cleanup(ctx, svc, conn, imagePath) 227 228 fetchImages(ctx, 1) 229 benchListImages(b, client, true) 230 } 231 232 func BenchmarkList10ImagesDetail(b *testing.B) { 233 ctx, svc, client, conn, imagePath := setup() 234 defer cleanup(ctx, svc, conn, imagePath) 235 236 fetchImages(ctx, 10) 237 benchListImages(b, client, true) 238 } 239 240 func BenchmarkList100ImagesDetail(b *testing.B) { 241 ctx, svc, client, conn, imagePath := setup() 242 defer cleanup(ctx, svc, conn, imagePath) 243 244 fetchImages(ctx, 100) 245 benchListImages(b, client, true) 246 } 247 248 func BenchmarkInspectImageIn10Images(b *testing.B) { 249 ctx, svc, client, conn, imagePath := setup() 250 defer cleanup(ctx, svc, conn, imagePath) 251 252 fetchImages(ctx, 10) 253 benchInspectImage(b, client) 254 } 255 256 func BenchmarkInspectImageIn100Images(b *testing.B) { 257 ctx, svc, client, conn, imagePath := setup() 258 defer cleanup(ctx, svc, conn, imagePath) 259 260 fetchImages(ctx, 100) 261 benchInspectImage(b, client) 262 }