gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/harness/harness.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 harness holds utility code for running benchmarks on Docker.
    16  package harness
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"os"
    22  
    23  	"gvisor.dev/gvisor/pkg/test/dockerutil"
    24  )
    25  
    26  var (
    27  	help  = flag.Bool("help", false, "print this usage message")
    28  	debug = flag.Bool("debug", false, "turns on debug messages for individual benchmarks")
    29  )
    30  
    31  // Init performs any harness initilialization before runs.
    32  func Init() error {
    33  	flag.Usage = func() {
    34  		fmt.Fprintf(os.Stderr, "Usage: %s -- --test.bench=<regex>\n", os.Args[0])
    35  		flag.PrintDefaults()
    36  	}
    37  	flag.Parse()
    38  	if *help {
    39  		flag.Usage()
    40  		os.Exit(0)
    41  	}
    42  	dockerutil.EnsureSupportedDockerVersion()
    43  	return nil
    44  }
    45  
    46  // SetFixedBenchmarks causes all benchmarks to run once.
    47  //
    48  // This must be set if they cannot scale with N. Note that this uses 1ns
    49  // instead of 1x due to https://github.com/golang/go/issues/32051.
    50  func SetFixedBenchmarks() {
    51  	flag.Set("test.benchtime", "1ns")
    52  }
    53  
    54  // GetMachine returns this run's implementation of machine.
    55  func GetMachine() (Machine, error) {
    56  	return &localMachine{}, nil
    57  }