-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_bench.go
More file actions
390 lines (334 loc) · 12 KB
/
Copy pathrun_bench.go
File metadata and controls
390 lines (334 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package shimtest
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
taskAPI "github.com/containerd/containerd/api/runtime/task/v3"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/ttrpc"
)
// Bench runs every benchmark in the RunSuite as a sub-benchmark of
// b. Each benchmark times one phase of a container's lifecycle so
// regressions can be localized.
func (s *RunSuite) Bench(b *testing.B) {
b.Helper()
b.Run("Lifecycle", s.benchLifecycle)
b.Run("Startup", s.benchStartup)
b.Run("StartupPhases", s.benchStartupPhases)
b.Run("Start", s.benchStart)
}
// benchLifecycle measures the full create-start-kill-wait-delete
// lifecycle of a container through the shim, including the shim
// start. Per-phase durations are reported as custom metrics so
// regressions can be localized to a specific RPC.
func (s *RunSuite) benchLifecycle(b *testing.B) {
// Build the read-only erofs images once; per-iteration setup only
// creates the writable ext4 scratch image (or overlay upper/work).
shimBin, err := exec.LookPath(s.cfg.ShimBinary)
if err != nil {
b.Fatalf("shim binary %q not found in PATH: %v", s.cfg.ShimBinary, err)
}
if shimDir := filepath.Dir(shimBin); !strings.Contains(os.Getenv("PATH"), shimDir) {
os.Setenv("PATH", shimDir+":"+os.Getenv("PATH"))
}
imgs := buildShimImages(b, s.cfg)
base := containerID(b)
ns := shimtestNamespace
var sumShim, sumCreate, sumStart, sumKill, sumWait, sumDelete time.Duration
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
cid := fmt.Sprintf("%s-%d", base, i)
bundleDir := b.TempDir()
bundleDir, err = filepath.EvalSymlinks(bundleDir)
if err != nil {
b.Fatal("resolve bundle dir:", err)
}
rootfsDir := filepath.Join(bundleDir, "rootfs")
if err := os.MkdirAll(rootfsDir, 0755); err != nil {
b.Fatal("mkdir rootfs:", err)
}
b.Cleanup(func() { mount.Unmount(rootfsDir, 0) })
rootfsMounts := buildRootfsMountsFromImages(b, s.cfg, imgs, rootfsDir)
createOCISpec(b, bundleDir, []string{"/bin/forever", "hello"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(b, bundleDir)
ctx := namespaces.WithNamespace(b.Context(), ns)
var stdoutBuf bytes.Buffer
var stdoutMu sync.Mutex
drainFifoInto(b, ctx, stdoutPath, &stdoutBuf, &stdoutMu)
drainFifo(b, ctx, stderrPath)
b.StartTimer()
t := time.Now()
params := startShim(b, shimBin, bundleDir, cid, ns, s.cfg)
conn := connectShim(b, params.Address)
client := ttrpc.NewClient(conn)
tc := newTaskClient(client, params.Version)
sumShim += time.Since(t)
t = time.Now()
if _, err := tc.Create(ctx, newCreateTaskRequest(b, cid, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
b.Fatal("create failed:", err)
}
sumCreate += time.Since(t)
t = time.Now()
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: cid}); err != nil {
b.Fatal("start failed:", err)
}
sumStart += time.Since(t)
t = time.Now()
if _, err := tc.Kill(ctx, &taskAPI.KillRequest{ID: cid, Signal: uint32(syscall.SIGKILL), All: true}); err != nil {
b.Fatal("kill failed:", err)
}
sumKill += time.Since(t)
t = time.Now()
if _, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: cid}); err != nil {
b.Fatal("wait failed:", err)
}
sumWait += time.Since(t)
t = time.Now()
if _, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: cid}); err != nil {
b.Fatal("delete failed:", err)
}
sumDelete += time.Since(t)
b.StopTimer()
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: cid})
client.Close()
}
n := float64(b.N)
reportMs := func(d time.Duration, name string) {
b.ReportMetric(float64(d.Microseconds())/n/1000.0, name)
}
reportMs(sumShim, "ms/shim-start")
reportMs(sumCreate, "ms/create")
reportMs(sumStart, "ms/start")
reportMs(sumKill, "ms/kill")
reportMs(sumWait, "ms/wait")
reportMs(sumDelete, "ms/delete")
}
// benchStartup measures the time from shim start through the first
// output being produced by the container process.
func (s *RunSuite) benchStartup(b *testing.B) {
shimBin, err := exec.LookPath(s.cfg.ShimBinary)
if err != nil {
b.Fatalf("shim binary %q not found in PATH: %v", s.cfg.ShimBinary, err)
}
if shimDir := filepath.Dir(shimBin); !strings.Contains(os.Getenv("PATH"), shimDir) {
os.Setenv("PATH", shimDir+":"+os.Getenv("PATH"))
}
imgs := buildShimImages(b, s.cfg)
base := containerID(b)
ns := shimtestNamespace
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
cid := fmt.Sprintf("%s-%d", base, i)
bundleDir := b.TempDir()
bundleDir, err = filepath.EvalSymlinks(bundleDir)
if err != nil {
b.Fatal("resolve bundle dir:", err)
}
rootfsDir := filepath.Join(bundleDir, "rootfs")
if err := os.MkdirAll(rootfsDir, 0755); err != nil {
b.Fatal("mkdir rootfs:", err)
}
b.Cleanup(func() { mount.Unmount(rootfsDir, 0) })
rootfsMounts := buildRootfsMountsFromImages(b, s.cfg, imgs, rootfsDir)
createOCISpec(b, bundleDir, []string{"/bin/forever", "started"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(b, bundleDir)
ctx := namespaces.WithNamespace(b.Context(), ns)
var stdoutBuf bytes.Buffer
var stdoutMu sync.Mutex
drainFifoInto(b, ctx, stdoutPath, &stdoutBuf, &stdoutMu)
drainFifo(b, ctx, stderrPath)
b.StartTimer()
params := startShim(b, shimBin, bundleDir, cid, ns, s.cfg)
conn := connectShim(b, params.Address)
client := ttrpc.NewClient(conn)
tc := newTaskClient(client, params.Version)
if _, err := tc.Create(ctx, newCreateTaskRequest(b, cid, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
b.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: cid}); err != nil {
b.Fatal("start failed:", err)
}
deadline := time.After(30 * time.Second)
for {
stdoutMu.Lock()
got := stdoutBuf.String()
stdoutMu.Unlock()
if strings.Contains(got, "started") {
break
}
select {
case <-deadline:
b.Fatal("timed out waiting for output")
case <-time.After(1 * time.Millisecond):
}
}
b.StopTimer()
tc.Kill(ctx, &taskAPI.KillRequest{ID: cid, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: cid})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: cid})
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: cid})
client.Close()
}
}
// benchStartupPhases reports per-phase timings for the startup
// sequence. Custom metrics are averaged over b.N iterations.
func (s *RunSuite) benchStartupPhases(b *testing.B) {
shimBin, err := exec.LookPath(s.cfg.ShimBinary)
if err != nil {
b.Fatalf("shim binary %q not found in PATH: %v", s.cfg.ShimBinary, err)
}
if shimDir := filepath.Dir(shimBin); !strings.Contains(os.Getenv("PATH"), shimDir) {
os.Setenv("PATH", shimDir+":"+os.Getenv("PATH"))
}
imgs := buildShimImages(b, s.cfg)
base := containerID(b)
ns := shimtestNamespace
var sumShimStart, sumConnect, sumCreate, sumTaskStart, sumOutput, sumTotal time.Duration
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
cid := fmt.Sprintf("%s-%d", base, i)
bundleDir := b.TempDir()
bundleDir, err = filepath.EvalSymlinks(bundleDir)
if err != nil {
b.Fatal("resolve bundle dir:", err)
}
rootfsDir := filepath.Join(bundleDir, "rootfs")
if err := os.MkdirAll(rootfsDir, 0755); err != nil {
b.Fatal("mkdir rootfs:", err)
}
b.Cleanup(func() { mount.Unmount(rootfsDir, 0) })
rootfsMounts := buildRootfsMountsFromImages(b, s.cfg, imgs, rootfsDir)
createOCISpec(b, bundleDir, []string{"/bin/forever", "started"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(b, bundleDir)
ctx := namespaces.WithNamespace(b.Context(), ns)
var stdoutBuf bytes.Buffer
var stdoutMu sync.Mutex
drainFifoInto(b, ctx, stdoutPath, &stdoutBuf, &stdoutMu)
drainFifo(b, ctx, stderrPath)
b.StartTimer()
t0 := time.Now()
params := startShim(b, shimBin, bundleDir, cid, ns, s.cfg)
tShimStart := time.Since(t0)
t1 := time.Now()
conn := connectShim(b, params.Address)
client := ttrpc.NewClient(conn)
tc := newTaskClient(client, params.Version)
tConnect := time.Since(t1)
t2 := time.Now()
if _, err := tc.Create(ctx, newCreateTaskRequest(b, cid, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
b.Fatal("create failed:", err)
}
tCreate := time.Since(t2)
t3 := time.Now()
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: cid}); err != nil {
b.Fatal("start failed:", err)
}
tTaskStart := time.Since(t3)
t4 := time.Now()
deadline := time.After(30 * time.Second)
for {
stdoutMu.Lock()
got := stdoutBuf.String()
stdoutMu.Unlock()
if strings.Contains(got, "started") {
break
}
select {
case <-deadline:
b.Fatal("timed out waiting for output")
case <-time.After(1 * time.Millisecond):
}
}
tOutput := time.Since(t4)
tTotal := time.Since(t0)
b.StopTimer()
sumShimStart += tShimStart
sumConnect += tConnect
sumCreate += tCreate
sumTaskStart += tTaskStart
sumOutput += tOutput
sumTotal += tTotal
tc.Kill(ctx, &taskAPI.KillRequest{ID: cid, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: cid})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: cid})
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: cid})
client.Close()
}
n := float64(b.N)
b.ReportMetric(float64(sumShimStart.Milliseconds())/n, "ms/shim-start")
b.ReportMetric(float64(sumConnect.Milliseconds())/n, "ms/connect")
b.ReportMetric(float64(sumCreate.Milliseconds())/n, "ms/create")
b.ReportMetric(float64(sumTaskStart.Milliseconds())/n, "ms/task-start")
b.ReportMetric(float64(sumOutput.Milliseconds())/n, "ms/output")
b.ReportMetric(float64(sumTotal.Milliseconds())/n, "ms/total")
}
// benchStart measures just the shim "start" subcommand: the time
// from fork/exec of the shim binary until it returns its bootstrap
// parameters.
//
// The shim daemon is started and immediately shut down. No container
// task is created within it — the TTRPC Create method is never called
// — so the shim never receives or mounts rootfsMounts. Because the
// rootfs images are unused, they are not built at all: each iteration
// needs only a minimal bundle directory (config.json + log FIFO),
// which avoids O(b.N) erofs accumulation on the tmpfs at the high
// iteration counts this fast benchmark produces.
func (s *RunSuite) benchStart(b *testing.B) {
// Resolve the shim binary once; a fresh minimal bundle dir is
// created per iteration (tiny: only config.json + log fifo).
shimBin, err := exec.LookPath(s.cfg.ShimBinary)
if err != nil {
b.Fatalf("shim binary %q not found in PATH: %v", s.cfg.ShimBinary, err)
}
if shimDir := filepath.Dir(shimBin); !strings.Contains(os.Getenv("PATH"), shimDir) {
os.Setenv("PATH", shimDir+":"+os.Getenv("PATH"))
}
base := containerID(b)
ns := shimtestNamespace
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
cid := fmt.Sprintf("%s-%d", base, i)
bundleDir := b.TempDir()
bundleDir, err = filepath.EvalSymlinks(bundleDir)
if err != nil {
b.Fatal("resolve bundle dir:", err)
}
if err := os.MkdirAll(filepath.Join(bundleDir, "rootfs"), 0755); err != nil {
b.Fatal("mkdir rootfs:", err)
}
createOCISpec(b, bundleDir, []string{"/bin/forever"}, s.cfg)
ctx := namespaces.WithNamespace(b.Context(), ns)
b.StartTimer()
params := startShim(b, shimBin, bundleDir, cid, ns, s.cfg)
b.StopTimer()
conn := connectShim(b, params.Address)
client := ttrpc.NewClient(conn)
tc := newTaskClient(client, params.Version)
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: cid})
client.Close()
}
}