github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/base/size_test.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 size_test
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    24  	"github.com/SagerNet/gvisor/test/benchmarks/base"
    25  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    26  	"github.com/SagerNet/gvisor/test/benchmarks/tools"
    27  )
    28  
    29  // BenchmarkSizeEmpty creates N empty containers and reads memory usage from
    30  // /proc/meminfo.
    31  func BenchmarkSizeEmpty(b *testing.B) {
    32  	machine, err := harness.GetMachine()
    33  	if err != nil {
    34  		b.Fatalf("failed to get machine: %v", err)
    35  	}
    36  	defer machine.CleanUp()
    37  	meminfo := tools.Meminfo{}
    38  	ctx := context.Background()
    39  	containers := make([]*dockerutil.Container, 0, b.N)
    40  
    41  	// DropCaches before the test.
    42  	harness.DropCaches(machine)
    43  
    44  	// Check available memory on 'machine'.
    45  	cmd, args := meminfo.MakeCmd()
    46  	before, err := machine.RunCommand(cmd, args...)
    47  	if err != nil {
    48  		b.Fatalf("failed to get meminfo: %v", err)
    49  	}
    50  
    51  	// Make N containers.
    52  	for i := 0; i < b.N; i++ {
    53  		container := machine.GetContainer(ctx, b)
    54  		containers = append(containers, container)
    55  		if err := container.Spawn(ctx, dockerutil.RunOpts{
    56  			Image: "benchmarks/alpine",
    57  		}, "sh", "-c", "echo Hello && sleep 1000"); err != nil {
    58  			base.CleanUpContainers(ctx, containers)
    59  			b.Fatalf("failed to run container: %v", err)
    60  		}
    61  		if _, err := container.WaitForOutputSubmatch(ctx, "Hello", 5*time.Second); err != nil {
    62  			base.CleanUpContainers(ctx, containers)
    63  			b.Fatalf("failed to read container output: %v", err)
    64  		}
    65  	}
    66  
    67  	// Drop caches again before second measurement.
    68  	harness.DropCaches(machine)
    69  
    70  	// Check available memory after containers are up.
    71  	after, err := machine.RunCommand(cmd, args...)
    72  	base.CleanUpContainers(ctx, containers)
    73  	if err != nil {
    74  		b.Fatalf("failed to get meminfo: %v", err)
    75  	}
    76  	meminfo.Report(b, before, after)
    77  }
    78  
    79  // BenchmarkSizeNginx starts N containers running Nginx, checks that they're
    80  // serving, and checks memory used based on /proc/meminfo.
    81  func BenchmarkSizeNginx(b *testing.B) {
    82  	machine, err := harness.GetMachine()
    83  	if err != nil {
    84  		b.Fatalf("failed to get machine with: %v", err)
    85  	}
    86  	defer machine.CleanUp()
    87  
    88  	// DropCaches for the first measurement.
    89  	harness.DropCaches(machine)
    90  
    91  	// Measure MemAvailable before creating containers.
    92  	meminfo := tools.Meminfo{}
    93  	cmd, args := meminfo.MakeCmd()
    94  	before, err := machine.RunCommand(cmd, args...)
    95  	if err != nil {
    96  		b.Fatalf("failed to run meminfo command: %v", err)
    97  	}
    98  
    99  	// Make N Nginx containers.
   100  	ctx := context.Background()
   101  	runOpts := dockerutil.RunOpts{
   102  		Image: "benchmarks/nginx",
   103  	}
   104  	const port = 80
   105  	servers := base.StartServers(ctx, b,
   106  		base.ServerArgs{
   107  			Machine: machine,
   108  			Port:    port,
   109  			RunOpts: runOpts,
   110  			Cmd:     []string{"nginx", "-c", "/etc/nginx/nginx_gofer.conf"},
   111  		})
   112  	defer base.CleanUpContainers(ctx, servers)
   113  
   114  	// DropCaches after servers are created.
   115  	harness.DropCaches(machine)
   116  	// Take after measurement.
   117  	after, err := machine.RunCommand(cmd, args...)
   118  	if err != nil {
   119  		b.Fatalf("failed to run meminfo command: %v", err)
   120  	}
   121  	meminfo.Report(b, before, after)
   122  }
   123  
   124  // BenchmarkSizeNode starts N containers running a Node app, checks that
   125  // they're serving, and checks memory used based on /proc/meminfo.
   126  func BenchmarkSizeNode(b *testing.B) {
   127  	machine, err := harness.GetMachine()
   128  	if err != nil {
   129  		b.Fatalf("failed to get machine with: %v", err)
   130  	}
   131  	defer machine.CleanUp()
   132  
   133  	// Make a redis instance for Node to connect.
   134  	ctx := context.Background()
   135  	redis, redisIP := base.RedisInstance(ctx, b, machine)
   136  	defer redis.CleanUp(ctx)
   137  
   138  	// DropCaches after redis is created.
   139  	harness.DropCaches(machine)
   140  
   141  	// Take before measurement.
   142  	meminfo := tools.Meminfo{}
   143  	cmd, args := meminfo.MakeCmd()
   144  	before, err := machine.RunCommand(cmd, args...)
   145  	if err != nil {
   146  		b.Fatalf("failed to run meminfo commend: %v", err)
   147  	}
   148  
   149  	// Create N Node servers.
   150  	runOpts := dockerutil.RunOpts{
   151  		Image:   "benchmarks/node",
   152  		WorkDir: "/usr/src/app",
   153  		Links:   []string{redis.MakeLink("redis")},
   154  	}
   155  	nodeCmd := []string{"node", "index.js", redisIP.String()}
   156  	const port = 8080
   157  	servers := base.StartServers(ctx, b,
   158  		base.ServerArgs{
   159  			Machine: machine,
   160  			Port:    port,
   161  			RunOpts: runOpts,
   162  			Cmd:     nodeCmd,
   163  		})
   164  	defer base.CleanUpContainers(ctx, servers)
   165  
   166  	// DropCaches after servers are created.
   167  	harness.DropCaches(machine)
   168  	// Take after measurement.
   169  	cmd, args = meminfo.MakeCmd()
   170  	after, err := machine.RunCommand(cmd, args...)
   171  	if err != nil {
   172  		b.Fatalf("failed to run meminfo command: %v", err)
   173  	}
   174  	meminfo.Report(b, before, after)
   175  }
   176  
   177  // TestMain is the main method for package network.
   178  func TestMain(m *testing.M) {
   179  	harness.Init()
   180  	os.Exit(m.Run())
   181  }