Skip to content

Commit 0ba2092

Browse files
committed
libct/int: add BenchmarkExecInBigEnv
Here's what it shows on my laptop (with -count 10 -benchtime 10s, summarized by benchstat): name time/op ExecInBigEnv-20 61.7ms ± 4% Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 1b06426 commit 0ba2092

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

libcontainer/integration/execin_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"math/rand"
78
"os"
89
"strconv"
910
"strings"
@@ -377,6 +378,80 @@ func TestExecInEnvironment(t *testing.T) {
377378
}
378379
}
379380

381+
func genBigEnv(count int) []string {
382+
randStr := func(length int) string {
383+
const charset = "abcdefghijklmnopqrstuvwxyz0123456789_"
384+
b := make([]byte, length)
385+
for i := range b {
386+
b[i] = charset[rand.Intn(len(charset))]
387+
}
388+
return string(b)
389+
}
390+
391+
envs := make([]string, count)
392+
for i := 0; i < count; i++ {
393+
key := strings.ToUpper(randStr(10))
394+
value := randStr(20)
395+
envs[i] = key + "=" + value
396+
}
397+
398+
return envs
399+
}
400+
401+
func BenchmarkExecInBigEnv(b *testing.B) {
402+
if testing.Short() {
403+
return
404+
}
405+
config := newTemplateConfig(b, nil)
406+
container, err := newContainer(b, config)
407+
ok(b, err)
408+
defer destroyContainer(container)
409+
410+
// Execute a first process in the container
411+
stdinR, stdinW, err := os.Pipe()
412+
ok(b, err)
413+
process := &libcontainer.Process{
414+
Cwd: "/",
415+
Args: []string{"cat"},
416+
Env: standardEnvironment,
417+
Stdin: stdinR,
418+
Init: true,
419+
}
420+
err = container.Run(process)
421+
_ = stdinR.Close()
422+
defer stdinW.Close() //nolint: errcheck
423+
ok(b, err)
424+
425+
const numEnv = 5000
426+
env := append(standardEnvironment, genBigEnv(numEnv)...)
427+
// Construct the expected output.
428+
var wantOut bytes.Buffer
429+
for _, e := range env {
430+
wantOut.WriteString(e + "\n")
431+
}
432+
433+
b.ResetTimer()
434+
for i := 0; i < b.N; i++ {
435+
buffers := newStdBuffers()
436+
process2 := &libcontainer.Process{
437+
Cwd: "/",
438+
Args: []string{"env"},
439+
Env: env,
440+
Stdin: buffers.Stdin,
441+
Stdout: buffers.Stdout,
442+
Stderr: buffers.Stderr,
443+
}
444+
err = container.Run(process2)
445+
ok(b, err)
446+
waitProcess(process2, b)
447+
if !bytes.Equal(buffers.Stdout.Bytes(), wantOut.Bytes()) {
448+
b.Fatalf("unexpected output: %s (stderr: %s)", buffers.Stdout, buffers.Stderr)
449+
}
450+
}
451+
_ = stdinW.Close()
452+
waitProcess(process, b)
453+
}
454+
380455
func TestExecinPassExtraFiles(t *testing.T) {
381456
if testing.Short() {
382457
return

0 commit comments

Comments
 (0)