github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/base/base.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package base holds utility methods common to the base tests.
    16  package base
    17  
    18  import (
    19  	"context"
    20  	"net"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    25  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    26  )
    27  
    28  // ServerArgs wraps args for startServers and runServerWorkload.
    29  type ServerArgs struct {
    30  	Machine harness.Machine
    31  	Port    int
    32  	RunOpts dockerutil.RunOpts
    33  	Cmd     []string
    34  }
    35  
    36  // StartServers starts b.N containers defined by 'runOpts' and 'cmd' and uses
    37  // 'machine' to check that each is up.
    38  func StartServers(ctx context.Context, b *testing.B, args ServerArgs) []*dockerutil.Container {
    39  	b.Helper()
    40  	servers := make([]*dockerutil.Container, 0, b.N)
    41  
    42  	// Create N servers and wait until each of them is serving.
    43  	for i := 0; i < b.N; i++ {
    44  		server := args.Machine.GetContainer(ctx, b)
    45  		servers = append(servers, server)
    46  		if err := server.Spawn(ctx, args.RunOpts, args.Cmd...); err != nil {
    47  			CleanUpContainers(ctx, servers)
    48  			b.Fatalf("failed to spawn node instance: %v", err)
    49  		}
    50  
    51  		// Get the container IP.
    52  		servingIP, err := server.FindIP(ctx, false)
    53  		if err != nil {
    54  			CleanUpContainers(ctx, servers)
    55  			b.Fatalf("failed to get ip from server: %v", err)
    56  		}
    57  
    58  		// Wait until the server is up.
    59  		if err := harness.WaitUntilServing(ctx, args.Machine, servingIP, args.Port); err != nil {
    60  			CleanUpContainers(ctx, servers)
    61  			b.Fatalf("failed to wait for serving")
    62  		}
    63  	}
    64  	return servers
    65  }
    66  
    67  // CleanUpContainers cleans up a slice of containers.
    68  func CleanUpContainers(ctx context.Context, containers []*dockerutil.Container) {
    69  	for _, c := range containers {
    70  		if c != nil {
    71  			c.CleanUp(ctx)
    72  		}
    73  	}
    74  }
    75  
    76  // RedisInstance returns a Redis container and its reachable IP.
    77  func RedisInstance(ctx context.Context, b *testing.B, machine harness.Machine) (*dockerutil.Container, net.IP) {
    78  	b.Helper()
    79  	// Spawn a redis instance for the app to use.
    80  	redis := machine.GetNativeContainer(ctx, b)
    81  	if err := redis.Spawn(ctx, dockerutil.RunOpts{
    82  		Image: "benchmarks/redis",
    83  	}); err != nil {
    84  		redis.CleanUp(ctx)
    85  		b.Fatalf("failed to spwan redis instance: %v", err)
    86  	}
    87  
    88  	if out, err := redis.WaitForOutput(ctx, "Ready to accept connections", 3*time.Second); err != nil {
    89  		redis.CleanUp(ctx)
    90  		b.Fatalf("failed to start redis server: %v %s", err, out)
    91  	}
    92  	redisIP, err := redis.FindIP(ctx, false)
    93  	if err != nil {
    94  		redis.CleanUp(ctx)
    95  		b.Fatalf("failed to get IP from redis instance: %v", err)
    96  	}
    97  	return redis, redisIP
    98  }