gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"gvisor.dev/gvisor/pkg/test/dockerutil"
    23  	"gvisor.dev/gvisor/test/benchmarks/harness"
    24  	"gvisor.dev/gvisor/test/benchmarks/tools"
    25  	"gvisor.dev/gvisor/test/metricsviz"
    26  )
    27  
    28  // runStaticServer runs static serving workloads (httpd, nginx).
    29  func runStaticServer(b *testing.B, serverOpts dockerutil.RunOpts, serverCmd []string, port int, hey *tools.Hey) {
    30  	ctx := context.Background()
    31  
    32  	// Get two machines: a client and server.
    33  	clientMachine, err := harness.GetMachine()
    34  	if err != nil {
    35  		b.Fatalf("failed to get machine: %v", err)
    36  	}
    37  	defer clientMachine.CleanUp()
    38  
    39  	serverMachine, err := harness.GetMachine()
    40  	if err != nil {
    41  		b.Fatalf("failed to get machine: %v", err)
    42  	}
    43  	defer serverMachine.CleanUp()
    44  
    45  	// Make the containers.
    46  	client := clientMachine.GetNativeContainer(ctx, b)
    47  	defer client.CleanUp(ctx)
    48  	server := serverMachine.GetContainer(ctx, b)
    49  	defer server.CleanUp(ctx)
    50  	defer metricsviz.FromContainerLogs(ctx, b, server)
    51  
    52  	// Start the server.
    53  	if err := server.Spawn(ctx, serverOpts, serverCmd...); err != nil {
    54  		b.Fatalf("failed to start server: %v", err)
    55  	}
    56  
    57  	// Make sure the server is serving.
    58  	harness.WaitUntilContainerServing(ctx, clientMachine, server, port)
    59  
    60  	// Run the client.
    61  	b.ResetTimer()
    62  	out, err := client.Run(ctx, dockerutil.RunOpts{
    63  		Image: "benchmarks/hey",
    64  		Links: []string{server.MakeLink("server")},
    65  	}, hey.MakeCmd("server", port)...)
    66  	if err != nil {
    67  		b.Fatalf("run failed with: %v", err)
    68  	}
    69  	b.StopTimer()
    70  	hey.Report(b, out)
    71  }