github.com/runcom/containerd@v0.0.0-20160708090337-9bff9f934c0d/hack/benchmark.go (about)

     1  // single app that will run containers in containerd and output
     2  // the total time in seconds that it took for the execution.
     3  // go run benchmark.go -count 1000 -bundle /containers/redis
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"net"
     9  	"strconv"
    10  	"sync"
    11  	"time"
    12  
    13  	"github.com/Sirupsen/logrus"
    14  	"github.com/docker/containerd/api/grpc/types"
    15  	netcontext "golang.org/x/net/context"
    16  	"google.golang.org/grpc"
    17  )
    18  
    19  func init() {
    20  	flag.StringVar(&bundle, "bundle", "/containers/redis", "the bundle path")
    21  	flag.StringVar(&addr, "addr", "/run/containerd/containerd.sock", "address to the container d instance")
    22  	flag.IntVar(&count, "count", 1000, "number of containers to run")
    23  	flag.Parse()
    24  }
    25  
    26  var (
    27  	count        int
    28  	bundle, addr string
    29  	group        = sync.WaitGroup{}
    30  	jobs         = make(chan string, 20)
    31  )
    32  
    33  func getClient() types.APIClient {
    34  	dialOpts := []grpc.DialOption{grpc.WithInsecure()}
    35  	dialOpts = append(dialOpts,
    36  		grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
    37  			return net.DialTimeout("unix", addr, timeout)
    38  		},
    39  		))
    40  	conn, err := grpc.Dial(addr, dialOpts...)
    41  	if err != nil {
    42  		logrus.Fatal(err)
    43  	}
    44  	return types.NewAPIClient(conn)
    45  }
    46  
    47  func main() {
    48  	client := getClient()
    49  	for i := 0; i < 100; i++ {
    50  		group.Add(1)
    51  		go worker(client)
    52  	}
    53  	start := time.Now()
    54  	for i := 0; i < count; i++ {
    55  		id := strconv.Itoa(i)
    56  		jobs <- id
    57  	}
    58  	close(jobs)
    59  	group.Wait()
    60  	end := time.Now()
    61  	duration := end.Sub(start).Seconds()
    62  	logrus.Info(duration)
    63  }
    64  
    65  func worker(client types.APIClient) {
    66  	defer group.Done()
    67  	for id := range jobs {
    68  		if _, err := client.CreateContainer(netcontext.Background(), &types.CreateContainerRequest{
    69  			Id:         id,
    70  			BundlePath: bundle,
    71  		}); err != nil {
    72  			logrus.Error(err)
    73  		}
    74  	}
    75  }