github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/integration/performance/create_measure_test.go (about)

     1  package performance_test
     2  
     3  import (
     4  	"runtime"
     5  	"strconv"
     6  
     7  	"github.com/cloudfoundry-incubator/garden"
     8  	gclient "github.com/cloudfoundry-incubator/garden/client"
     9  	"github.com/cloudfoundry-incubator/garden/client/connection"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  const (
    15  	creates       = 40
    16  	createSamples = 5
    17  )
    18  
    19  var _ = Describe("Concurrent container creation", func() {
    20  
    21  	BeforeEach(func() {
    22  		runtime.GOMAXPROCS(runtime.NumCPU())
    23  	})
    24  
    25  	Measure("multiple concurrent creates", func(b Benchmarker) {
    26  		handles := []string{}
    27  
    28  		b.Time("concurrent creations", func() {
    29  			chans := []chan string{}
    30  			for i := 0; i < creates; i++ {
    31  				ch := make(chan string, 1)
    32  				go func(c chan string, index int) {
    33  					defer GinkgoRecover()
    34  					client := gclient.New(connection.New("tcp", "localhost:7777"))
    35  					b.Time("create-"+strconv.Itoa(index), func() {
    36  						ctr, err := client.Create(garden.ContainerSpec{})
    37  						Expect(err).ToNot(HaveOccurred())
    38  						c <- ctr.Handle()
    39  					})
    40  				}(ch, i)
    41  				chans = append(chans, ch)
    42  			}
    43  
    44  			for _, ch := range chans {
    45  				handle := <-ch
    46  				if handle != "" {
    47  					handles = append(handles, handle)
    48  
    49  				}
    50  			}
    51  		})
    52  
    53  		for _, handle := range handles {
    54  			client := gclient.New(connection.New("tcp", "localhost:7777"))
    55  			Expect(client.Destroy(handle)).To(Succeed())
    56  		}
    57  
    58  	}, createSamples)
    59  
    60  })