github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/integration-cli/benchmark_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"sync"
     9  	"testing"
    10  
    11  	"gotest.tools/v3/assert"
    12  )
    13  
    14  func (s *DockerSuite) BenchmarkConcurrentContainerActions(c *testing.B) {
    15  	maxConcurrency := runtime.GOMAXPROCS(0)
    16  	numIterations := c.N
    17  	outerGroup := &sync.WaitGroup{}
    18  	outerGroup.Add(maxConcurrency)
    19  	chErr := make(chan error, numIterations*2*maxConcurrency)
    20  
    21  	for i := 0; i < maxConcurrency; i++ {
    22  		go func() {
    23  			defer outerGroup.Done()
    24  			innerGroup := &sync.WaitGroup{}
    25  			innerGroup.Add(2)
    26  
    27  			go func() {
    28  				defer innerGroup.Done()
    29  				for i := 0; i < numIterations; i++ {
    30  					args := []string{"run", "-d", "busybox"}
    31  					args = append(args, sleepCommandForDaemonPlatform()...)
    32  					out, _, err := dockerCmdWithError(args...)
    33  					if err != nil {
    34  						chErr <- fmt.Errorf(out)
    35  						return
    36  					}
    37  
    38  					id := strings.TrimSpace(out)
    39  					tmpDir, err := os.MkdirTemp("", "docker-concurrent-test-"+id)
    40  					if err != nil {
    41  						chErr <- err
    42  						return
    43  					}
    44  					defer os.RemoveAll(tmpDir)
    45  					out, _, err = dockerCmdWithError("cp", id+":/tmp", tmpDir)
    46  					if err != nil {
    47  						chErr <- fmt.Errorf(out)
    48  						return
    49  					}
    50  
    51  					out, _, err = dockerCmdWithError("kill", id)
    52  					if err != nil {
    53  						chErr <- fmt.Errorf(out)
    54  					}
    55  
    56  					out, _, err = dockerCmdWithError("start", id)
    57  					if err != nil {
    58  						chErr <- fmt.Errorf(out)
    59  					}
    60  
    61  					out, _, err = dockerCmdWithError("kill", id)
    62  					if err != nil {
    63  						chErr <- fmt.Errorf(out)
    64  					}
    65  
    66  					// don't do an rm -f here since it can potentially ignore errors from the graphdriver
    67  					out, _, err = dockerCmdWithError("rm", id)
    68  					if err != nil {
    69  						chErr <- fmt.Errorf(out)
    70  					}
    71  				}
    72  			}()
    73  
    74  			go func() {
    75  				defer innerGroup.Done()
    76  				for i := 0; i < numIterations; i++ {
    77  					out, _, err := dockerCmdWithError("ps")
    78  					if err != nil {
    79  						chErr <- fmt.Errorf(out)
    80  					}
    81  				}
    82  			}()
    83  
    84  			innerGroup.Wait()
    85  		}()
    86  	}
    87  
    88  	outerGroup.Wait()
    89  	close(chErr)
    90  
    91  	for err := range chErr {
    92  		assert.NilError(c, err)
    93  	}
    94  }