gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/base/syscallbench_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 syscallbench_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  // BenchmarSyscallbench runs a syscall b.N times on the runtime.
    29  func BenchmarkSyscallbench(b *testing.B) {
    30  	ctx := context.Background()
    31  	machine, err := harness.GetMachine()
    32  	if err != nil {
    33  		b.Fatalf("failed to get machine: %v", err)
    34  	}
    35  	defer machine.CleanUp()
    36  
    37  	for _, tc := range []struct {
    38  		param      tools.Parameter
    39  		syscallArg int
    40  	}{
    41  		{
    42  			param: tools.Parameter{
    43  				Name:  "syscall",
    44  				Value: "getpid",
    45  			},
    46  		},
    47  		{
    48  			param: tools.Parameter{
    49  				Name:  "syscall",
    50  				Value: "getpidopt",
    51  			},
    52  			syscallArg: 1,
    53  		},
    54  	} {
    55  		name, err := tools.ParametersToName(tc.param)
    56  		if err != nil {
    57  			b.Fatalf("Failed to parse params: %v", err)
    58  		}
    59  
    60  		func() {
    61  			container := machine.GetContainer(ctx, b)
    62  			defer container.CleanUp(ctx)
    63  			if err := container.Spawn(
    64  				ctx, dockerutil.RunOpts{
    65  					Image: "benchmarks/syscallbench",
    66  				},
    67  				"sleep", "24h",
    68  			); err != nil {
    69  				b.Fatalf("run failed with: %v", err)
    70  			}
    71  			defer metricsviz.FromContainerLogs(ctx, b, container)
    72  			b.Run(name, func(b *testing.B) {
    73  				cmd := []string{"syscallbench", fmt.Sprintf("--loops=%d", b.N), fmt.Sprintf("--syscall=%d", tc.syscallArg)}
    74  				b.ResetTimer()
    75  				out, err := container.Exec(ctx, dockerutil.ExecOpts{}, cmd...)
    76  				if err != nil {
    77  					b.Fatalf("failed to run syscallbench: %v, logs:%s", err, out)
    78  				}
    79  			})
    80  		}()
    81  	}
    82  }
    83  
    84  // BenchmarkSyscallUnderSeccomp runs a syscall b.N times with a seccomp filter
    85  // enabled for it.
    86  func BenchmarkSyscallUnderSeccomp(b *testing.B) {
    87  	ctx := context.Background()
    88  	machine, err := harness.GetMachine()
    89  	if err != nil {
    90  		b.Fatalf("failed to get machine: %v", err)
    91  	}
    92  	defer machine.CleanUp()
    93  
    94  	for _, tc := range []tools.Parameter{
    95  		{
    96  			Name:  "cacheable",
    97  			Value: "false",
    98  		},
    99  		{
   100  			Name:  "cacheable",
   101  			Value: "true",
   102  		},
   103  	} {
   104  		name, err := tools.ParametersToName(tc)
   105  		if err != nil {
   106  			b.Fatalf("Failed to parse params: %v", err)
   107  		}
   108  		func() {
   109  			container := machine.GetContainer(ctx, b)
   110  			defer container.CleanUp(ctx)
   111  			if err := container.Spawn(
   112  				ctx, dockerutil.RunOpts{
   113  					Image: "benchmarks/syscallbench",
   114  				},
   115  				"sleep", "24h",
   116  			); err != nil {
   117  				b.Fatalf("run failed with: %v", err)
   118  			}
   119  			defer metricsviz.FromContainerLogs(ctx, b, container)
   120  			b.Run(name, func(b *testing.B) {
   121  				cmd := []string{"syscallbench", "--syscall=1", fmt.Sprintf("--loops=%d", b.N)}
   122  				if tc.Value == "true" {
   123  					cmd = append(cmd, "--seccomp_cacheable")
   124  				} else {
   125  					cmd = append(cmd, "--seccomp_notcacheable")
   126  				}
   127  				b.ResetTimer()
   128  				out, err := container.Exec(ctx, dockerutil.ExecOpts{}, cmd...)
   129  				if err != nil {
   130  					b.Fatalf("failed to run syscallbench: %v, logs:%s", err, out)
   131  				}
   132  			})
   133  		}()
   134  	}
   135  }