2020-07-13 20:23:50 +00:00
|
|
|
// Copyright 2020 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-27 05:01:16 +00:00
|
|
|
"fmt"
|
2020-07-13 20:23:50 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gvisor.dev/gvisor/pkg/test/dockerutil"
|
2020-07-29 17:05:46 +00:00
|
|
|
"gvisor.dev/gvisor/test/benchmarks/harness"
|
2020-07-13 20:23:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Note: CleanCache versions of this test require running with root permissions.
|
2020-07-30 20:31:05 +00:00
|
|
|
func BenchmarkBuildABSL(b *testing.B) {
|
|
|
|
runBuildBenchmark(b, "benchmarks/absl", "/abseil-cpp", "absl/base/...")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: CleanCache versions of this test require running with root permissions.
|
|
|
|
// Note: This test takes on the order of 10m per permutation for runsc on kvm.
|
|
|
|
func BenchmarkBuildRunsc(b *testing.B) {
|
|
|
|
runBuildBenchmark(b, "benchmarks/runsc", "/gvisor", "runsc:runsc")
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBuildBenchmark(b *testing.B, image, workdir, target string) {
|
|
|
|
b.Helper()
|
2020-07-13 20:23:50 +00:00
|
|
|
// Get a machine from the Harness on which to run.
|
|
|
|
machine, err := h.GetMachine()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("failed to get machine: %v", err)
|
|
|
|
}
|
2020-07-16 01:19:52 +00:00
|
|
|
defer machine.CleanUp()
|
2020-07-13 20:23:50 +00:00
|
|
|
|
|
|
|
// Dimensions here are clean/dirty cache (do or don't drop caches)
|
|
|
|
// and if the mount on which we are compiling is a tmpfs/bind mount.
|
|
|
|
benchmarks := []struct {
|
|
|
|
name string
|
|
|
|
clearCache bool // clearCache drops caches before running.
|
|
|
|
tmpfs bool // tmpfs will run compilation on a tmpfs.
|
|
|
|
}{
|
|
|
|
{name: "CleanCache", clearCache: true, tmpfs: false},
|
|
|
|
{name: "DirtyCache", clearCache: false, tmpfs: false},
|
|
|
|
{name: "CleanCacheTmpfs", clearCache: true, tmpfs: true},
|
|
|
|
{name: "DirtyCacheTmpfs", clearCache: false, tmpfs: true},
|
|
|
|
}
|
|
|
|
for _, bm := range benchmarks {
|
|
|
|
b.Run(bm.name, func(b *testing.B) {
|
|
|
|
// Grab a container.
|
|
|
|
ctx := context.Background()
|
|
|
|
container := machine.GetContainer(ctx, b)
|
|
|
|
defer container.CleanUp(ctx)
|
|
|
|
|
2020-09-01 00:15:14 +00:00
|
|
|
// Start a container and sleep.
|
2020-07-13 20:23:50 +00:00
|
|
|
if err := container.Spawn(ctx, dockerutil.RunOpts{
|
2020-07-30 20:31:05 +00:00
|
|
|
Image: image,
|
2020-07-27 05:01:16 +00:00
|
|
|
}, "sleep", fmt.Sprintf("%d", 1000000)); err != nil {
|
2020-07-13 20:23:50 +00:00
|
|
|
b.Fatalf("run failed with: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are running on a tmpfs, copy to /tmp which is a tmpfs.
|
2020-09-01 00:15:14 +00:00
|
|
|
prefix := ""
|
2020-07-13 20:23:50 +00:00
|
|
|
if bm.tmpfs {
|
2020-07-30 20:31:05 +00:00
|
|
|
if out, err := container.Exec(ctx, dockerutil.ExecOpts{},
|
|
|
|
"cp", "-r", workdir, "/tmp/."); err != nil {
|
2020-08-25 19:16:31 +00:00
|
|
|
b.Fatalf("failed to copy directory: %v (%s)", err, out)
|
2020-07-13 20:23:50 +00:00
|
|
|
}
|
2020-09-01 00:15:14 +00:00
|
|
|
prefix = "/tmp"
|
2020-07-13 20:23:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 05:01:16 +00:00
|
|
|
// Restart profiles after the copy.
|
|
|
|
container.RestartProfiles()
|
2020-07-13 20:23:50 +00:00
|
|
|
b.ResetTimer()
|
2020-07-27 05:01:16 +00:00
|
|
|
// Drop Caches and bazel clean should happen inside the loop as we may use
|
|
|
|
// time options with b.N. (e.g. Run for an hour.)
|
2020-07-13 20:23:50 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-07-27 05:01:16 +00:00
|
|
|
b.StopTimer()
|
|
|
|
// Drop Caches for clear cache runs.
|
|
|
|
if bm.clearCache {
|
2020-07-29 17:05:46 +00:00
|
|
|
if err := harness.DropCaches(machine); err != nil {
|
|
|
|
b.Skipf("failed to drop caches: %v. You probably need root.", err)
|
2020-07-27 05:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
2020-07-13 20:23:50 +00:00
|
|
|
got, err := container.Exec(ctx, dockerutil.ExecOpts{
|
2020-09-01 00:15:14 +00:00
|
|
|
WorkDir: prefix + workdir,
|
2020-07-30 20:31:05 +00:00
|
|
|
}, "bazel", "build", "-c", "opt", target)
|
2020-07-13 20:23:50 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("build failed with: %v", err)
|
|
|
|
}
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
want := "Build completed successfully"
|
|
|
|
if !strings.Contains(got, want) {
|
|
|
|
b.Fatalf("string %s not in: %s", want, got)
|
|
|
|
}
|
2020-07-27 05:01:16 +00:00
|
|
|
// Clean bazel in case we use b.N.
|
|
|
|
_, err = container.Exec(ctx, dockerutil.ExecOpts{
|
2020-09-01 00:15:14 +00:00
|
|
|
WorkDir: prefix + workdir,
|
2020-07-27 05:01:16 +00:00
|
|
|
}, "bazel", "clean")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("build failed with: %v", err)
|
|
|
|
}
|
2020-07-13 20:23:50 +00:00
|
|
|
b.StartTimer()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|