github.com/portworx/docker@v1.12.1/integration-cli/benchmark_test.go (about)

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