github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/network/node_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  package network
    15  
    16  import (
    17  	"context"
    18  	"os"
    19  	"strconv"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    24  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    25  	"github.com/SagerNet/gvisor/test/benchmarks/tools"
    26  )
    27  
    28  // BenchmarkNode runs requests using 'hey' against a Node server run on
    29  // 'runtime'. The server responds to requests by grabbing some data in a
    30  // redis instance and returns the data in its reponse. The test loops through
    31  // increasing amounts of concurency for requests.
    32  func BenchmarkNode(b *testing.B) {
    33  	concurrency := []int{1, 5, 10, 25}
    34  	for _, c := range concurrency {
    35  		param := tools.Parameter{
    36  			Name:  "concurrency",
    37  			Value: strconv.Itoa(c),
    38  		}
    39  		name, err := tools.ParametersToName(param)
    40  		if err != nil {
    41  			b.Fatalf("Failed to parse parameters: %v", err)
    42  		}
    43  		b.Run(name, func(b *testing.B) {
    44  			hey := &tools.Hey{
    45  				Requests:    b.N,
    46  				Concurrency: c,
    47  			}
    48  			runNode(b, hey)
    49  		})
    50  	}
    51  }
    52  
    53  // runNode runs the test for a given # of requests and concurrency.
    54  func runNode(b *testing.B, hey *tools.Hey) {
    55  	b.Helper()
    56  
    57  	// The machine to hold Redis and the Node Server.
    58  	serverMachine, err := harness.GetMachine()
    59  	if err != nil {
    60  		b.Fatalf("failed to get machine with: %v", err)
    61  	}
    62  	defer serverMachine.CleanUp()
    63  
    64  	// The machine to run 'hey'.
    65  	clientMachine, err := harness.GetMachine()
    66  	if err != nil {
    67  		b.Fatalf("failed to get machine with: %v", err)
    68  	}
    69  	defer clientMachine.CleanUp()
    70  
    71  	ctx := context.Background()
    72  
    73  	// Spawn a redis instance for the app to use.
    74  	redis := serverMachine.GetNativeContainer(ctx, b)
    75  	if err := redis.Spawn(ctx, dockerutil.RunOpts{
    76  		Image: "benchmarks/redis",
    77  	}); err != nil {
    78  		b.Fatalf("failed to spwan redis instance: %v", err)
    79  	}
    80  	defer redis.CleanUp(ctx)
    81  
    82  	if out, err := redis.WaitForOutput(ctx, "Ready to accept connections", 3*time.Second); err != nil {
    83  		b.Fatalf("failed to start redis server: %v %s", err, out)
    84  	}
    85  	redisIP, err := redis.FindIP(ctx, false)
    86  	if err != nil {
    87  		b.Fatalf("failed to get IP from redis instance: %v", err)
    88  	}
    89  
    90  	// Node runs on port 8080.
    91  	port := 8080
    92  
    93  	// Start-up the Node server.
    94  	nodeApp := serverMachine.GetContainer(ctx, b)
    95  	if err := nodeApp.Spawn(ctx, dockerutil.RunOpts{
    96  		Image:   "benchmarks/node",
    97  		WorkDir: "/usr/src/app",
    98  		Links:   []string{redis.MakeLink("redis")},
    99  		Ports:   []int{port},
   100  	}, "node", "index.js", redisIP.String()); err != nil {
   101  		b.Fatalf("failed to spawn node instance: %v", err)
   102  	}
   103  	defer nodeApp.CleanUp(ctx)
   104  
   105  	servingIP, err := serverMachine.IPAddress()
   106  	if err != nil {
   107  		b.Fatalf("failed to get ip from server: %v", err)
   108  	}
   109  
   110  	servingPort, err := nodeApp.FindPort(ctx, port)
   111  	if err != nil {
   112  		b.Fatalf("failed to port from node instance: %v", err)
   113  	}
   114  
   115  	// Wait until the Client sees the server as up.
   116  	harness.WaitUntilServing(ctx, clientMachine, servingIP, servingPort)
   117  
   118  	heyCmd := hey.MakeCmd(servingIP, servingPort)
   119  
   120  	// the client should run on Native.
   121  	b.ResetTimer()
   122  	client := clientMachine.GetNativeContainer(ctx, b)
   123  	out, err := client.Run(ctx, dockerutil.RunOpts{
   124  		Image: "benchmarks/hey",
   125  	}, heyCmd...)
   126  	if err != nil {
   127  		b.Fatalf("hey container failed: %v logs: %s", err, out)
   128  	}
   129  
   130  	// Stop the timer to parse the data and report stats.
   131  	hey.Report(b, out)
   132  }
   133  
   134  func TestMain(m *testing.M) {
   135  	harness.Init()
   136  	os.Exit(m.Run())
   137  }