github.com/rumpl/bof@v23.0.0-rc.2+incompatible/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  	"time"
    11  
    12  	"gotest.tools/v3/assert"
    13  )
    14  
    15  type DockerBenchmarkSuite struct {
    16  	ds *DockerSuite
    17  }
    18  
    19  func (s *DockerBenchmarkSuite) TearDownTest(c *testing.T) {
    20  	s.ds.TearDownTest(c)
    21  }
    22  
    23  func (s *DockerBenchmarkSuite) OnTimeout(c *testing.T) {
    24  	s.ds.OnTimeout(c)
    25  }
    26  
    27  func (s *DockerBenchmarkSuite) BenchmarkConcurrentContainerActions(c *testing.B) {
    28  	maxConcurrency := runtime.GOMAXPROCS(0)
    29  	numIterations := c.N
    30  	outerGroup := &sync.WaitGroup{}
    31  	outerGroup.Add(maxConcurrency)
    32  	chErr := make(chan error, numIterations*2*maxConcurrency)
    33  
    34  	for i := 0; i < maxConcurrency; i++ {
    35  		go func() {
    36  			defer outerGroup.Done()
    37  			innerGroup := &sync.WaitGroup{}
    38  			innerGroup.Add(2)
    39  
    40  			go func() {
    41  				defer innerGroup.Done()
    42  				for i := 0; i < numIterations; i++ {
    43  					args := []string{"run", "-d", "busybox"}
    44  					args = append(args, sleepCommandForDaemonPlatform()...)
    45  					out, _, err := dockerCmdWithError(args...)
    46  					if err != nil {
    47  						chErr <- fmt.Errorf(out)
    48  						return
    49  					}
    50  
    51  					id := strings.TrimSpace(out)
    52  					tmpDir, err := os.MkdirTemp("", "docker-concurrent-test-"+id)
    53  					if err != nil {
    54  						chErr <- err
    55  						return
    56  					}
    57  					defer os.RemoveAll(tmpDir)
    58  					out, _, err = dockerCmdWithError("cp", id+":/tmp", tmpDir)
    59  					if err != nil {
    60  						chErr <- fmt.Errorf(out)
    61  						return
    62  					}
    63  
    64  					out, _, err = dockerCmdWithError("kill", id)
    65  					if err != nil {
    66  						chErr <- fmt.Errorf(out)
    67  					}
    68  
    69  					out, _, err = dockerCmdWithError("start", id)
    70  					if err != nil {
    71  						chErr <- fmt.Errorf(out)
    72  					}
    73  
    74  					out, _, err = dockerCmdWithError("kill", id)
    75  					if err != nil {
    76  						chErr <- fmt.Errorf(out)
    77  					}
    78  
    79  					// don't do an rm -f here since it can potentially ignore errors from the graphdriver
    80  					out, _, err = dockerCmdWithError("rm", id)
    81  					if err != nil {
    82  						chErr <- fmt.Errorf(out)
    83  					}
    84  				}
    85  			}()
    86  
    87  			go func() {
    88  				defer innerGroup.Done()
    89  				for i := 0; i < numIterations; i++ {
    90  					out, _, err := dockerCmdWithError("ps")
    91  					if err != nil {
    92  						chErr <- fmt.Errorf(out)
    93  					}
    94  				}
    95  			}()
    96  
    97  			innerGroup.Wait()
    98  		}()
    99  	}
   100  
   101  	outerGroup.Wait()
   102  	close(chErr)
   103  
   104  	for err := range chErr {
   105  		assert.NilError(c, err)
   106  	}
   107  }
   108  
   109  func (s *DockerBenchmarkSuite) BenchmarkLogsCLIRotateFollow(c *testing.B) {
   110  	out, _ := dockerCmd(c, "run", "-d", "--log-opt", "max-size=1b", "--log-opt", "max-file=10", "busybox", "sh", "-c", "while true; do usleep 50000; echo hello; done")
   111  	id := strings.TrimSpace(out)
   112  	ch := make(chan error, 1)
   113  	go func() {
   114  		ch <- nil
   115  		out, _, _ := dockerCmdWithError("logs", "-f", id)
   116  		// if this returns at all, it's an error
   117  		ch <- fmt.Errorf(out)
   118  	}()
   119  
   120  	<-ch
   121  	select {
   122  	case <-time.After(30 * time.Second):
   123  		// ran for 30 seconds with no problem
   124  		return
   125  	case err := <-ch:
   126  		if err != nil {
   127  			c.Fatal(err)
   128  		}
   129  	}
   130  }