github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/base/sysbench_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  
    15  package sysbench_test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    22  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    23  	"github.com/SagerNet/gvisor/test/benchmarks/tools"
    24  )
    25  
    26  type testCase struct {
    27  	name string
    28  	test tools.Sysbench
    29  }
    30  
    31  // BenchmarSysbench runs sysbench on the runtime.
    32  func BenchmarkSysbench(b *testing.B) {
    33  	testCases := []testCase{
    34  		{
    35  			name: "CPU",
    36  			test: &tools.SysbenchCPU{
    37  				SysbenchBase: tools.SysbenchBase{
    38  					Threads: 1,
    39  				},
    40  			},
    41  		},
    42  		{
    43  			name: "Memory",
    44  			test: &tools.SysbenchMemory{
    45  				SysbenchBase: tools.SysbenchBase{
    46  					Threads: 1,
    47  				},
    48  			},
    49  		},
    50  		{
    51  			name: "Mutex",
    52  			test: &tools.SysbenchMutex{
    53  				SysbenchBase: tools.SysbenchBase{
    54  					Threads: 8,
    55  				},
    56  			},
    57  		},
    58  	}
    59  
    60  	machine, err := harness.GetMachine()
    61  	if err != nil {
    62  		b.Fatalf("failed to get machine: %v", err)
    63  	}
    64  	defer machine.CleanUp()
    65  
    66  	for _, tc := range testCases {
    67  		param := tools.Parameter{
    68  			Name:  "testname",
    69  			Value: tc.name,
    70  		}
    71  		name, err := tools.ParametersToName(param)
    72  		if err != nil {
    73  			b.Fatalf("Failed to parse params: %v", err)
    74  		}
    75  		b.Run(name, func(b *testing.B) {
    76  			ctx := context.Background()
    77  			sysbench := machine.GetContainer(ctx, b)
    78  			defer sysbench.CleanUp(ctx)
    79  
    80  			cmd := tc.test.MakeCmd(b)
    81  			b.ResetTimer()
    82  			out, err := sysbench.Run(ctx, dockerutil.RunOpts{
    83  				Image: "benchmarks/sysbench",
    84  			}, cmd...)
    85  			if err != nil {
    86  				b.Fatalf("failed to run sysbench: %v: logs:%s", err, out)
    87  			}
    88  			b.StopTimer()
    89  			tc.test.Report(b, out)
    90  		})
    91  	}
    92  }