gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/fs/rubydev_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 rubydev_test benchmarks Ruby CI/CD-type workloads.
    16  package rubydev_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"gvisor.dev/gvisor/test/benchmarks/fs/fsbench"
    26  	"gvisor.dev/gvisor/test/benchmarks/harness"
    27  	"gvisor.dev/gvisor/test/benchmarks/tools"
    28  )
    29  
    30  func runRubyBenchmark(b *testing.B, bm fsbench.FSBenchmark, cleanupDirPatterns []string) {
    31  	b.Helper()
    32  	ctx := context.Background()
    33  	machine, err := harness.GetMachine()
    34  	if err != nil {
    35  		b.Fatalf("failed to get machine: %v", err)
    36  	}
    37  	defer machine.CleanUp()
    38  	bm.Image = "benchmarks/rubydev"
    39  	cleanupDirPatterns = append(cleanupDirPatterns, "$HOME/.bundle/cache", "/tmp/rspec_failed_tests.txt")
    40  	bm.CleanCmd = []string{"bash", "-c", fmt.Sprintf("rm -rf %s", strings.Join(cleanupDirPatterns, " "))}
    41  	fsbench.RunWithDifferentFilesystems(ctx, b, machine, bm)
    42  }
    43  
    44  // BenchmarkRubyNoOpTest runs a no-op Ruby test.
    45  // This is the test case that Stripe used to benchmark gVisor:
    46  // https://stripe.com/blog/fast-secure-builds-choose-two
    47  func BenchmarkRubyNoOpTest(b *testing.B) {
    48  	runRubyBenchmark(b, fsbench.FSBenchmark{
    49  		Image:      "benchmarks/rubydev",
    50  		WorkDir:    "/files",
    51  		RunCmd:     []string{"ruby", "tc_no_op.rb"},
    52  		WantOutput: "100% passed",
    53  	}, nil)
    54  }
    55  
    56  // BenchmarkRubySpecTest runs a complex test suite from the Fastlane project:
    57  // https://github.com/fastlane/fastlane
    58  func BenchmarkRubySpecTest(b *testing.B) {
    59  	runRubyBenchmark(b, fsbench.FSBenchmark{
    60  		Image:      "benchmarks/rubydev",
    61  		WorkDir:    "/fastlane",
    62  		RunCmd:     []string{"bash", "/files/run_fastlane_tests.sh"},
    63  		WantOutput: "3613 examples, 0 failures",
    64  		Callback: func(b *testing.B, output string) {
    65  			loadTime, err := tools.ExtractRubyLoadTime(output)
    66  			if err != nil {
    67  				b.Errorf("ExtractRubyLoadTime failed: %v", err)
    68  				return
    69  			}
    70  			tools.ReportCustomMetric(b, float64(loadTime.Nanoseconds()), "load", "ns")
    71  		},
    72  	}, []string{
    73  		// Fastlane tests pollute the filesystem a lot.
    74  		// To find out, run `find / -exec stat  -c "%n %y" {} \; | sort` before and after running tests
    75  		// for the first time, and diff them.
    76  		"$HOME/Library", // Yes, even on Linux.
    77  		"$HOME/.fastlane",
    78  		"/tmp/fastlane*",
    79  		"/tmp/spaceship*",
    80  		"/tmp/profile_download*",
    81  		"/tmp/d*-*-*/*.mobileprovision",
    82  	})
    83  }
    84  
    85  // TestMain is the main method for this package.
    86  func TestMain(m *testing.M) {
    87  	harness.Init()
    88  	harness.SetFixedBenchmarks()
    89  	os.Exit(m.Run())
    90  }