gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/base/hackbench_test.go (about)

     1  // Copyright 2022 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 hackbench_test
    16  
    17  import (
    18  	"context"
    19  	"os"
    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  // BenchmarHackbench runs hackbench on the runtime.
    29  func BenchmarkHackbench(b *testing.B) {
    30  	testCases := []tools.Hackbench{
    31  		{
    32  			IpcMode:     "pipe",
    33  			ProcessMode: "thread",
    34  		},
    35  		{
    36  			IpcMode:     "socket",
    37  			ProcessMode: "process",
    38  		},
    39  	}
    40  
    41  	machine, err := harness.GetMachine()
    42  	if err != nil {
    43  		b.Fatalf("failed to get machine: %v", err)
    44  	}
    45  	defer machine.CleanUp()
    46  
    47  	for _, tc := range testCases {
    48  		ipcMode := tools.Parameter{
    49  			Name:  "ipcMode",
    50  			Value: tc.IpcMode,
    51  		}
    52  		processMode := tools.Parameter{
    53  			Name:  "processMode",
    54  			Value: tc.ProcessMode,
    55  		}
    56  		name, err := tools.ParametersToName(ipcMode, processMode)
    57  		if err != nil {
    58  			b.Fatalf("Failed to parse params: %v", err)
    59  		}
    60  		b.Run(name, func(b *testing.B) {
    61  			ctx := context.Background()
    62  			container := machine.GetContainer(ctx, b)
    63  			defer container.CleanUp(ctx)
    64  
    65  			if err := container.Spawn(
    66  				ctx, dockerutil.RunOpts{
    67  					Image: "benchmarks/hackbench",
    68  				},
    69  				"sleep", "24h",
    70  			); err != nil {
    71  				b.Fatalf("run failed with: %v", err)
    72  			}
    73  			defer metricsviz.FromContainerLogs(ctx, b, container)
    74  
    75  			cmd := tc.MakeCmd(b)
    76  			b.ResetTimer()
    77  			out, err := container.Exec(ctx, dockerutil.ExecOpts{}, cmd...)
    78  			if err != nil {
    79  				b.Fatalf("failed to run hackbench: %v, logs:%s", err, out)
    80  			}
    81  			tc.Report(b, out)
    82  		})
    83  	}
    84  }
    85  
    86  // TestMain is the main method for this package.
    87  func TestMain(m *testing.M) {
    88  	harness.Init()
    89  	os.Exit(m.Run())
    90  }