Skip to content

Commit

Permalink
dev: teach dev how to stress
Browse files Browse the repository at this point in the history
The previous version of this code would assume that you had `stress`
installed globally -- this doesn't work in the long term, so instead if
you're stress testing pre-build the `stress` binary and pass it in to
`bazel test` invocation.

Closes #67165.

Release note: None
  • Loading branch information
rickystewart committed Jul 15, 2021
1 parent dc4b834 commit fce4c8d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
18 changes: 15 additions & 3 deletions pkg/cmd/dev/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,19 @@ func (d *dev) getPathToBin(ctx context.Context, target string) (string, error) {
return "", err
}
bazelBin := strings.TrimSpace(string(out))
target = strings.TrimPrefix(target, "//")
_, filename := filepath.Split(target)
return filepath.Join(bazelBin, target, filename+"_", filename), nil
var head string
if strings.HasPrefix(target, "@") {
doubleSlash := strings.Index(target, "//")
head = filepath.Join("external", target[1:doubleSlash])
} else {
head = strings.TrimPrefix(target, "//")
}
var bin string
colon := strings.Index(target, ":")
if colon >= 0 {
bin = target[colon+1:]
} else {
bin = target[strings.LastIndex(target, "/")+1:]
}
return filepath.Join(bazelBin, head, bin+"_", bin), nil
}
36 changes: 25 additions & 11 deletions pkg/cmd/dev/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"github.com/spf13/cobra"
)

var (
const (
stressTarget = "@com_github_cockroachdb_stress//:stress"

// General testing flags.
filterFlag = "filter"
timeoutFlag = "timeout"
showLogsFlag = "show-logs"
vFlag = "verbose"
stressFlag = "stress"
raceFlag = "race"
Expand All @@ -46,7 +47,7 @@ func makeTestCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.Comm
Long: `Run the specified tests.`,
Example: `
dev test
dev test pkg/kv/kvserver --filter=TestReplicaGC* -v -show-logs --timeout=1m
dev test pkg/kv/kvserver --filter=TestReplicaGC* -v --timeout=1m
dev test --stress --race ...
dev test --logic --files=prepare|fk --subtests=20042 --config=local
dev test --fuzz sql/sem/tree --filter=Decimal`,
Expand All @@ -55,7 +56,6 @@ func makeTestCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.Comm
}
// Attach flags for the test sub-command.
addCommonTestFlags(testCmd)
testCmd.Flags().Bool(showLogsFlag, false, "print logs instead of saving them in files")
testCmd.Flags().BoolP(vFlag, "v", false, "enable logging during test runs")
testCmd.Flags().Bool(stressFlag, false, "run tests under stress")
testCmd.Flags().Bool(raceFlag, false, "run tests using race builds")
Expand Down Expand Up @@ -95,15 +95,27 @@ func (d *dev) runUnitTest(cmd *cobra.Command, pkgs []string) error {
timeout := mustGetFlagDuration(cmd, timeoutFlag)
ignoreCache := mustGetFlagBool(cmd, ignoreCacheFlag)
verbose := mustGetFlagBool(cmd, vFlag)
showLogs := mustGetFlagBool(cmd, showLogsFlag)

if showLogs {
return errors.New("-show-logs unimplemented")
}

d.log.Printf("unit test args: stress=%t race=%t filter=%s timeout=%s ignore-cache=%t pkgs=%s",
stress, race, filter, timeout, ignoreCache, pkgs)

// If we're running `stress`, we need to build it first.
var stressBin string
if stress {
var args []string
args = append(args, "build", "--color=yes", "--experimental_convenience_symlinks=ignore")
args = append(args, getConfigFlags()...)
args = append(args, stressTarget)
_, err := d.exec.CommandContextSilent(ctx, "bazel", args...)
if err != nil {
return err
}
stressBin, err = d.getPathToBin(ctx, stressTarget)
if err != nil {
return err
}
}

var args []string
args = append(args, "test")
args = append(args, "--color=yes")
Expand Down Expand Up @@ -167,10 +179,12 @@ func (d *dev) runUnitTest(cmd *cobra.Command, pkgs []string) error {
if ignoreCache {
args = append(args, "--nocache_test_results")
}
if stress {
if stress && timeout > 0 {
// TODO(irfansharif): Should this be pulled into a top-level flag?
// Should we just re-purpose timeout here?
args = append(args, "--run_under", fmt.Sprintf("stress -maxtime=%s", timeout))
args = append(args, "--run_under", fmt.Sprintf("%s -maxtime=%s", stressBin, timeout))
} else if stress {
args = append(args, "--run_under", stressBin)
} else if timeout > 0 {
args = append(args, fmt.Sprintf("--test_timeout=%d", int(timeout.Seconds())))
}
Expand Down
32 changes: 30 additions & 2 deletions pkg/cmd/dev/testdata/recording/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@ Executed 1 out of 1 test: 1 test passes.
----
----

bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under stress -maxtime=10s --test_filter='TestStartChild*' --test_output errors
bazel build --color=yes --experimental_convenience_symlinks=ignore --config=dev @com_github_cockroachdb_stress//:stress
----
----
ok
----
----

bazel info bazel-bin --color=no --config=dev
----
----
/private/var/tmp/_bazel/99e666e4e674209ecdb66b46371278df/execroot/cockroach/bazel-out/darwin-fastbuild/bin
----
----

bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under /private/var/tmp/_bazel/99e666e4e674209ecdb66b46371278df/execroot/cockroach/bazel-out/darwin-fastbuild/bin/external/com_github_cockroachdb_stress/stress_/stress --test_filter='TestStartChild*' --test_output errors
----
----
//pkg/util/tracing:tracing_test PASSED in 12.3s
Expand All @@ -69,7 +83,21 @@ Executed 1 out of 1 test: 1 test passes.
----
----

bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under stress -maxtime=10s --test_filter='TestStartChild*' --test_output all --test_arg -test.v
bazel build --color=yes --experimental_convenience_symlinks=ignore --config=dev @com_github_cockroachdb_stress//:stress
----
----
ok
----
----

bazel info bazel-bin --color=no --config=dev
----
----
/private/var/tmp/_bazel/99e666e4e674209ecdb66b46371278df/execroot/cockroach/bazel-out/darwin-fastbuild/bin
----
----

bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under /private/var/tmp/_bazel/99e666e4e674209ecdb66b46371278df/execroot/cockroach/bazel-out/darwin-fastbuild/bin/external/com_github_cockroachdb_stress/stress_/stress -maxtime=10s --test_filter='TestStartChild*' --test_output all --test_arg -test.v
----
----
==================== Test output for //pkg/util/tracing:tracing_test:
Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/dev/testdata/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ dev test pkg/util/tracing -f 'TestStartChild*' --ignore-cache
----
bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --nocache_test_results --test_filter='TestStartChild*' --test_output errors

dev test --stress pkg/util/tracing --filter 'TestStartChild*' --timeout=10s
dev test --stress pkg/util/tracing --filter 'TestStartChild*'
----
bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under stress -maxtime=10s --test_filter='TestStartChild*' --test_output errors
bazel build --color=yes --experimental_convenience_symlinks=ignore --config=dev @com_github_cockroachdb_stress//:stress
bazel info bazel-bin --color=no --config=dev
bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under /private/var/tmp/_bazel/99e666e4e674209ecdb66b46371278df/execroot/cockroach/bazel-out/darwin-fastbuild/bin/external/com_github_cockroachdb_stress/stress_/stress --test_filter='TestStartChild*' --test_output errors

dev test --stress pkg/util/tracing --filter 'TestStartChild*' --timeout=10s -v
----
bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under stress -maxtime=10s --test_filter='TestStartChild*' --test_output all --test_arg -test.v
bazel build --color=yes --experimental_convenience_symlinks=ignore --config=dev @com_github_cockroachdb_stress//:stress
bazel info bazel-bin --color=no --config=dev
bazel test --color=yes --experimental_convenience_symlinks=ignore --config=dev //pkg/util/tracing:tracing_test --run_under /private/var/tmp/_bazel/99e666e4e674209ecdb66b46371278df/execroot/cockroach/bazel-out/darwin-fastbuild/bin/external/com_github_cockroachdb_stress/stress_/stress -maxtime=10s --test_filter='TestStartChild*' --test_output all --test_arg -test.v

dev test //pkg/testutils --timeout=10s
----
Expand Down

0 comments on commit fce4c8d

Please sign in to comment.