From 95c8770cbd8212a3cef8ed10262ad140cc23144f Mon Sep 17 00:00:00 2001 From: Will Chandler Date: Mon, 20 Mar 2023 23:02:27 -0400 Subject: cmd: Add `gitaly-bench` binary Add the new `gitaly-bench` binary for coordinating starting and stopping Gitaly for benchmarking. The tests at this level do not attempt to verify anything other than the basic CLI output as `gitaly-bench` requires a benchmarking environment to run successfully. In particular we need to have a `gitaly` service available in `systemd` for invocations to succeed. --- cmd/gitaly-bench/bench.go | 14 +++++++ cmd/gitaly-bench/bench_test.go | 69 +++++++++++++++++++++++++++++++++ internal/testhelper/testcfg/binaries.go | 5 +++ 3 files changed, 88 insertions(+) create mode 100644 cmd/gitaly-bench/bench.go create mode 100644 cmd/gitaly-bench/bench_test.go diff --git a/cmd/gitaly-bench/bench.go b/cmd/gitaly-bench/bench.go new file mode 100644 index 000000000..9bc7b03b5 --- /dev/null +++ b/cmd/gitaly-bench/bench.go @@ -0,0 +1,14 @@ +package main + +import ( + "log" + "os" + + cli "gitlab.com/gitlab-org/gitaly/v15/internal/cli/bench" +) + +func main() { + if err := cli.NewApp().Run(os.Args); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/gitaly-bench/bench_test.go b/cmd/gitaly-bench/bench_test.go new file mode 100644 index 000000000..6feef32a2 --- /dev/null +++ b/cmd/gitaly-bench/bench_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "bytes" + "os/exec" + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} + +func TestGitalyBenchCLI(t *testing.T) { + t.Parallel() + + cfg := testcfg.Build(t) + testcfg.BuildGitalyBench(t, cfg) + + for _, tc := range []struct { + name string + args []string + stdout string + exitCode int + }{ + { + name: "no args", + stdout: "NAME:\n gitaly-bench - coordinate starting and stopping Gitaly for benchmarking\n\nUSAGE:\n gitaly-bench [global options] command [command options] [arguments...]\n\nCOMMANDS:\n coordinate coordinate starting and stopping Gitaly for benchmarking\n client send gRPC requests to Gitaly for benchmarking\n help, h Shows a list of commands or help for one command\n\nGLOBAL OPTIONS:\n --help, -h show help\n", + exitCode: 0, + }, + { + name: "client no args", + args: []string{"client"}, + stdout: "NAME:\n gitaly-bench client - send gRPC requests to Gitaly for benchmarking\n\nUSAGE:\n gitaly-bench client [command options] [arguments...]\n\nDESCRIPTION:\n Run on a client machine to benchmark RPC performance and order the coordinator to start/stop Gitaly.\n\nOPTIONS:\n --out-dir value Directory to write results to\n --server-addr value Address of Gitaly server\n --coord-port value TCP port coordinator is listening on (default: \"7075\")\n --query-dir value Path to directory containing queries to execute (default: \"/opt/ghz/queries\")\n --help, -h show help\n", + exitCode: 1, + }, + { + name: "coordinator no args", + args: []string{"coordinate"}, + stdout: "NAME:\n gitaly-bench coordinate - coordinate starting and stopping Gitaly for benchmarking\n\nUSAGE:\n gitaly-bench coordinate [command options] [arguments...]\n\nDESCRIPTION:\n Handle starting and stopping Gitaly for benchmarking, directed by the client. This will listen on a separate TCP port from Gitaly.\n\nOPTIONS:\n --repo-dir value Directory containing git repositories\n --coord-port value TCP port for coordinator to listen on (default: \"7075\")\n --help, -h show help\n", + exitCode: 1, + }, + } { + tc := tc + + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + var stderr, stdout bytes.Buffer + cmd := exec.Command(cfg.BinaryPath("gitaly-bench"), tc.args...) + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + err := cmd.Run() + + if tc.exitCode == 0 { + require.NoError(t, err) + } else { + require.Error(t, err) + require.Equal(t, tc.exitCode, err.(*exec.ExitError).ExitCode()) + } + + require.Contains(t, stdout.String(), tc.stdout) + }) + } +} diff --git a/internal/testhelper/testcfg/binaries.go b/internal/testhelper/testcfg/binaries.go index c9f4a360b..46d6a902f 100644 --- a/internal/testhelper/testcfg/binaries.go +++ b/internal/testhelper/testcfg/binaries.go @@ -34,6 +34,11 @@ func BuildGitalyLFSSmudge(tb testing.TB, cfg config.Cfg) string { return buildGitalyCommand(tb, cfg, "gitaly-lfs-smudge") } +// BuildGitalyBench builds the gitaly-bench command and installs it into the binary directory. +func BuildGitalyBench(tb testing.TB, cfg config.Cfg) string { + return buildGitalyCommand(tb, cfg, "gitaly-bench") +} + // BuildGitalyHooks builds the gitaly-hooks command and installs it into the binary directory. func BuildGitalyHooks(tb testing.TB, cfg config.Cfg) string { return buildGitalyCommand(tb, cfg, "gitaly-hooks") -- cgit v1.2.3