gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"fmt"
    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  type testCase struct {
    29  	name    string
    30  	threads int
    31  	test    tools.Sysbench
    32  }
    33  
    34  // BenchmarkSysbench runs sysbench on the runtime.
    35  func BenchmarkSysbench(b *testing.B) {
    36  	testCases := []testCase{
    37  		{
    38  			name: "CPU",
    39  			test: &tools.SysbenchCPU{
    40  				SysbenchBase: tools.SysbenchBase{
    41  					Threads: 1,
    42  				},
    43  			},
    44  		},
    45  		{
    46  			name:    "CPU",
    47  			threads: 16,
    48  			test: &tools.SysbenchCPU{
    49  				SysbenchBase: tools.SysbenchBase{
    50  					Threads: 16,
    51  				},
    52  			},
    53  		},
    54  		{
    55  			name: "Memory",
    56  			test: &tools.SysbenchMemory{
    57  				SysbenchBase: tools.SysbenchBase{
    58  					Threads: 1,
    59  				},
    60  			},
    61  		},
    62  		{
    63  			name:    "Memory",
    64  			threads: 16,
    65  			test: &tools.SysbenchMemory{
    66  				SysbenchBase: tools.SysbenchBase{
    67  					Threads: 16,
    68  				},
    69  			},
    70  		},
    71  		{
    72  			name: "Mutex",
    73  			test: &tools.SysbenchMutex{
    74  				SysbenchBase: tools.SysbenchBase{
    75  					Threads: 8,
    76  				},
    77  			},
    78  		},
    79  	}
    80  
    81  	machine, err := harness.GetMachine()
    82  	if err != nil {
    83  		b.Fatalf("failed to get machine: %v", err)
    84  	}
    85  	defer machine.CleanUp()
    86  
    87  	for _, tc := range testCases {
    88  		param := tools.Parameter{
    89  			Name:  "testname",
    90  			Value: tc.name,
    91  		}
    92  		var name string
    93  		if tc.threads != 0 {
    94  			threads := tools.Parameter{
    95  				Name:  "threads",
    96  				Value: fmt.Sprintf("%d", tc.threads),
    97  			}
    98  			name, err = tools.ParametersToName(param, threads)
    99  			if err != nil {
   100  				b.Fatalf("Failed to parse params: %v", err)
   101  			}
   102  		} else {
   103  			name, err = tools.ParametersToName(param)
   104  			if err != nil {
   105  				b.Fatalf("Failed to parse params: %v", err)
   106  			}
   107  		}
   108  		b.Run(name, func(b *testing.B) {
   109  			ctx := context.Background()
   110  			sysbench := machine.GetContainer(ctx, b)
   111  			defer sysbench.CleanUp(ctx)
   112  			defer metricsviz.FromContainerLogs(ctx, b, sysbench)
   113  
   114  			cmd := tc.test.MakeCmd(b)
   115  			b.ResetTimer()
   116  			out, err := sysbench.Run(ctx, dockerutil.RunOpts{
   117  				Image: "benchmarks/sysbench",
   118  			}, cmd...)
   119  			if err != nil {
   120  				b.Fatalf("failed to run sysbench: %v: logs:%s", err, out)
   121  			}
   122  			b.StopTimer()
   123  			tc.test.Report(b, out)
   124  		})
   125  	}
   126  }