Skip to content

Commit 4ad9f7f

Browse files
authored
Merge pull request #4432 from kolyshkin/exec-bench
libct/int: add exec benchmark
2 parents 22106a4 + 1e67409 commit 4ad9f7f

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package integration
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/opencontainers/runc/libcontainer"
8+
)
9+
10+
func BenchmarkExecTrue(b *testing.B) {
11+
config := newTemplateConfig(b, nil)
12+
container, err := newContainer(b, config)
13+
ok(b, err)
14+
defer destroyContainer(container)
15+
16+
// Execute a first process in the container
17+
stdinR, stdinW, err := os.Pipe()
18+
ok(b, err)
19+
process := &libcontainer.Process{
20+
Cwd: "/",
21+
Args: []string{"cat"},
22+
Env: standardEnvironment,
23+
Stdin: stdinR,
24+
Init: true,
25+
}
26+
err = container.Run(process)
27+
_ = stdinR.Close()
28+
defer func() {
29+
_ = stdinW.Close()
30+
if _, err := process.Wait(); err != nil {
31+
b.Log(err)
32+
}
33+
}()
34+
ok(b, err)
35+
36+
b.ResetTimer()
37+
for i := 0; i < b.N; i++ {
38+
exec := &libcontainer.Process{
39+
Cwd: "/",
40+
Args: []string{"/bin/true"},
41+
Env: standardEnvironment,
42+
LogLevel: "0", // Minimize forwardChildLogs involvement.
43+
}
44+
err := container.Run(exec)
45+
if err != nil {
46+
b.Fatal("exec failed:", err)
47+
}
48+
waitProcess(exec, b)
49+
}
50+
b.StopTimer()
51+
}

libcontainer/integration/template_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type tParam struct {
3232
// and the default setup for devices.
3333
//
3434
// If p is nil, a default container is created.
35-
func newTemplateConfig(t *testing.T, p *tParam) *configs.Config {
35+
func newTemplateConfig(t testing.TB, p *tParam) *configs.Config {
3636
var allowedDevices []*devices.Rule
3737
for _, device := range specconv.AllowedDevices {
3838
allowedDevices = append(allowedDevices, &device.Rule)

libcontainer/integration/utils_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func ok(t testing.TB, err error) {
8585
}
8686
}
8787

88-
func waitProcess(p *libcontainer.Process, t *testing.T) {
88+
func waitProcess(p *libcontainer.Process, t testing.TB) {
8989
t.Helper()
9090
status, err := p.Wait()
9191
if err != nil {
@@ -99,7 +99,7 @@ func waitProcess(p *libcontainer.Process, t *testing.T) {
9999

100100
// newRootfs creates a new tmp directory and copies the busybox root
101101
// filesystem to it.
102-
func newRootfs(t *testing.T) string {
102+
func newRootfs(t testing.TB) string {
103103
t.Helper()
104104
dir := t.TempDir()
105105
if err := copyBusybox(dir); err != nil {
@@ -165,7 +165,7 @@ func copyBusybox(dest string) error {
165165
return nil
166166
}
167167

168-
func newContainer(t *testing.T, config *configs.Config) (*libcontainer.Container, error) {
168+
func newContainer(t testing.TB, config *configs.Config) (*libcontainer.Container, error) {
169169
name := strings.ReplaceAll(t.Name(), "/", "_") + strconv.FormatInt(-int64(time.Now().Nanosecond()), 35)
170170
root := t.TempDir()
171171

@@ -176,7 +176,7 @@ func newContainer(t *testing.T, config *configs.Config) (*libcontainer.Container
176176
//
177177
// buffers are returned containing the STDOUT and STDERR output for the run
178178
// along with the exit code and any go error
179-
func runContainer(t *testing.T, config *configs.Config, args ...string) (buffers *stdBuffers, exitCode int, err error) {
179+
func runContainer(t testing.TB, config *configs.Config, args ...string) (buffers *stdBuffers, exitCode int, err error) {
180180
container, err := newContainer(t, config)
181181
if err != nil {
182182
return nil, -1, err
@@ -214,7 +214,7 @@ func runContainer(t *testing.T, config *configs.Config, args ...string) (buffers
214214

215215
// runContainerOk is a wrapper for runContainer, simplifying its use for cases
216216
// when the run is expected to succeed and return exit code of 0.
217-
func runContainerOk(t *testing.T, config *configs.Config, args ...string) *stdBuffers {
217+
func runContainerOk(t testing.TB, config *configs.Config, args ...string) *stdBuffers {
218218
buffers, exitCode, err := runContainer(t, config, args...)
219219

220220
t.Helper()

0 commit comments

Comments
 (0)