github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/network/iperf_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  	"testing"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    22  	"github.com/SagerNet/gvisor/pkg/test/testutil"
    23  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    24  	"github.com/SagerNet/gvisor/test/benchmarks/tools"
    25  )
    26  
    27  func BenchmarkIperf(b *testing.B) {
    28  	clientMachine, err := harness.GetMachine()
    29  	if err != nil {
    30  		b.Fatalf("failed to get machine: %v", err)
    31  	}
    32  	defer clientMachine.CleanUp()
    33  
    34  	serverMachine, err := harness.GetMachine()
    35  	if err != nil {
    36  		b.Fatalf("failed to get machine: %v", err)
    37  	}
    38  	defer serverMachine.CleanUp()
    39  	ctx := context.Background()
    40  	for _, bm := range []struct {
    41  		name       string
    42  		clientFunc func(context.Context, testutil.Logger) *dockerutil.Container
    43  		serverFunc func(context.Context, testutil.Logger) *dockerutil.Container
    44  	}{
    45  		// We are either measuring the server or the client. The other should be
    46  		// runc. e.g. Upload sees how fast the runtime under test uploads to a native
    47  		// server.
    48  		{
    49  			name:       "Upload",
    50  			clientFunc: clientMachine.GetContainer,
    51  			serverFunc: serverMachine.GetNativeContainer,
    52  		},
    53  		{
    54  			name:       "Download",
    55  			clientFunc: clientMachine.GetNativeContainer,
    56  			serverFunc: serverMachine.GetContainer,
    57  		},
    58  	} {
    59  		name, err := tools.ParametersToName(tools.Parameter{
    60  			Name:  "operation",
    61  			Value: bm.name,
    62  		})
    63  		if err != nil {
    64  			b.Fatalf("Failed to parse parameters: %v", err)
    65  		}
    66  		b.Run(name, func(b *testing.B) {
    67  			// Set up the containers.
    68  			server := bm.serverFunc(ctx, b)
    69  			defer server.CleanUp(ctx)
    70  			client := bm.clientFunc(ctx, b)
    71  			defer client.CleanUp(ctx)
    72  
    73  			// iperf serves on port 5001 by default.
    74  			port := 5001
    75  
    76  			// Start the server.
    77  			if err := server.Spawn(ctx, dockerutil.RunOpts{
    78  				Image: "benchmarks/iperf",
    79  				Ports: []int{port},
    80  			}, "iperf", "-s"); err != nil {
    81  				b.Fatalf("failed to start server with: %v", err)
    82  			}
    83  
    84  			ip, err := serverMachine.IPAddress()
    85  			if err != nil {
    86  				b.Fatalf("failed to find server ip: %v", err)
    87  			}
    88  
    89  			servingPort, err := server.FindPort(ctx, port)
    90  			if err != nil {
    91  				b.Fatalf("failed to find port %d: %v", port, err)
    92  			}
    93  
    94  			// Make sure the server is up and serving before we run.
    95  			if err := harness.WaitUntilServing(ctx, clientMachine, ip, servingPort); err != nil {
    96  				b.Fatalf("failed to wait for server: %v", err)
    97  			}
    98  
    99  			iperf := tools.Iperf{
   100  				Num: b.N, // KB for the client to send.
   101  			}
   102  
   103  			// Run the client.
   104  			b.ResetTimer()
   105  			out, err := client.Run(ctx, dockerutil.RunOpts{
   106  				Image: "benchmarks/iperf",
   107  			}, iperf.MakeCmd(ip, servingPort)...)
   108  			if err != nil {
   109  				b.Fatalf("failed to run client: %v", err)
   110  			}
   111  			b.StopTimer()
   112  			iperf.Report(b, out)
   113  			b.StartTimer()
   114  		})
   115  	}
   116  }
   117  
   118  func TestMain(m *testing.M) {
   119  	harness.Init()
   120  	os.Exit(m.Run())
   121  }