benchmark_test.go 2.0 KB

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