2016-03-09 16:23:04 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2016-03-09 16:23:04 -05:00
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-03-09 16:23:04 -05:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) BenchmarkConcurrentContainerActions(c *testing.B) {
|
2016-03-09 16:23:04 -05:00
|
|
|
maxConcurrency := runtime.GOMAXPROCS(0)
|
|
|
|
numIterations := c.N
|
|
|
|
outerGroup := &sync.WaitGroup{}
|
|
|
|
outerGroup.Add(maxConcurrency)
|
|
|
|
chErr := make(chan error, numIterations*2*maxConcurrency)
|
|
|
|
|
|
|
|
for i := 0; i < maxConcurrency; i++ {
|
|
|
|
go func() {
|
|
|
|
defer outerGroup.Done()
|
|
|
|
innerGroup := &sync.WaitGroup{}
|
|
|
|
innerGroup.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer innerGroup.Done()
|
|
|
|
for i := 0; i < numIterations; i++ {
|
2019-07-08 12:42:08 -04:00
|
|
|
args := []string{"run", "-d", "busybox"}
|
2016-08-17 18:46:28 -04:00
|
|
|
args = append(args, sleepCommandForDaemonPlatform()...)
|
2016-03-09 16:23:04 -05:00
|
|
|
out, _, err := dockerCmdWithError(args...)
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
2021-08-24 06:10:50 -04:00
|
|
|
tmpDir, err := os.MkdirTemp("", "docker-concurrent-test-"+id)
|
2016-03-09 16:23:04 -05:00
|
|
|
if err != nil {
|
|
|
|
chErr <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
out, _, err = dockerCmdWithError("cp", id+":/tmp", tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err = dockerCmdWithError("kill", id)
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err = dockerCmdWithError("start", id)
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err = dockerCmdWithError("kill", id)
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't do an rm -f here since it can potentially ignore errors from the graphdriver
|
|
|
|
out, _, err = dockerCmdWithError("rm", id)
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer innerGroup.Done()
|
|
|
|
for i := 0; i < numIterations; i++ {
|
|
|
|
out, _, err := dockerCmdWithError("ps")
|
|
|
|
if err != nil {
|
|
|
|
chErr <- fmt.Errorf(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
innerGroup.Wait()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
outerGroup.Wait()
|
|
|
|
close(chErr)
|
|
|
|
|
|
|
|
for err := range chErr {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-09 16:23:04 -05:00
|
|
|
}
|
|
|
|
}
|