github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/network/network.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 network holds benchmarks around raw network performance.
    16  package network
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    23  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    24  	"github.com/SagerNet/gvisor/test/benchmarks/tools"
    25  )
    26  
    27  // runStaticServer runs static serving workloads (httpd, nginx).
    28  func runStaticServer(b *testing.B, serverOpts dockerutil.RunOpts, serverCmd []string, port int, hey *tools.Hey) {
    29  	ctx := context.Background()
    30  
    31  	// Get two machines: a client and server.
    32  	clientMachine, err := harness.GetMachine()
    33  	if err != nil {
    34  		b.Fatalf("failed to get machine: %v", err)
    35  	}
    36  	defer clientMachine.CleanUp()
    37  
    38  	serverMachine, err := harness.GetMachine()
    39  	if err != nil {
    40  		b.Fatalf("failed to get machine: %v", err)
    41  	}
    42  	defer serverMachine.CleanUp()
    43  
    44  	// Make the containers.
    45  	client := clientMachine.GetNativeContainer(ctx, b)
    46  	defer client.CleanUp(ctx)
    47  	server := serverMachine.GetContainer(ctx, b)
    48  	defer server.CleanUp(ctx)
    49  
    50  	// Start the server.
    51  	if err := server.Spawn(ctx, serverOpts, serverCmd...); err != nil {
    52  		b.Fatalf("failed to start server: %v", err)
    53  	}
    54  
    55  	// Get its IP.
    56  	ip, err := serverMachine.IPAddress()
    57  	if err != nil {
    58  		b.Fatalf("failed to find server ip: %v", err)
    59  	}
    60  
    61  	// Get the published port.
    62  	servingPort, err := server.FindPort(ctx, port)
    63  	if err != nil {
    64  		b.Fatalf("failed to find server port %d: %v", port, err)
    65  	}
    66  
    67  	// Make sure the server is serving.
    68  	harness.WaitUntilServing(ctx, clientMachine, ip, servingPort)
    69  
    70  	// Run the client.
    71  	b.ResetTimer()
    72  	out, err := client.Run(ctx, dockerutil.RunOpts{
    73  		Image: "benchmarks/hey",
    74  	}, hey.MakeCmd(ip, servingPort)...)
    75  	if err != nil {
    76  		b.Fatalf("run failed with: %v", err)
    77  	}
    78  	b.StopTimer()
    79  	hey.Report(b, out)
    80  }