benchmark_test.go 2.0 KB

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