Skip to content

Commit

Permalink
Merge pull request #27 from vitessio/cobra-main
Browse files Browse the repository at this point in the history
Migrate to cobra
  • Loading branch information
systay authored Sep 30, 2024
2 parents 26bc180 + 4f2e3ef commit d914fab
Show file tree
Hide file tree
Showing 22 changed files with 332 additions and 267 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ go.work
# the produced binary
report.xml
errors/
vitess-tester
vtbenchstat
/vt

# Do not ignore anything inside the src/vitess-tester directory
!/src/vitess-tester/
!/src/cmd/vtbenchstat
!/go
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@ check_version:

default: check_version build

build: vitess-tester vtbenchstat

vitess-tester:
$(GO) build -o $@ ./

vtbenchstat:
$(GO) build -o $@ ./src/cmd/vtbenchstat
build:
$(GO) build -o vt ./

test: build
$(GO) test -cover ./...
#./vitess-tester -check-error

tidy:
$(GO) mod tidy

clean:
$(GO) clean -i ./...
rm -f vitess-tester vtbenchstat
rm -f vt
113 changes: 39 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# Vitess Tester
# VT utils

Vitess tester tests Vitess using the same test files as the [MySQL Test Framework](https://github.com/mysql/mysql-server/tree/8.0/mysql-test).
The `vt` binary encapsulates several utils tools for Vitess.
It includes the following utilities:

- `vt tester`, a testing utility, using the same test files as the [MySQL Test Framework](https://github.com/mysql/mysql-server/tree/8.0/mysql-test).
- `vt benchstat`, a benchmark utility that compares the query planning performance of two Vitess versions.

## Install

If you only want the `vitess-tester` binary, install it using the following command:
Install `vt` using the following command:

```
go install github.com/vitessio/vitess-tester@latest
```

The `vtbenchstat` binary can be had by running:

```
go install github.com/vitessio/vitess-tester/src/cmd/vtbenchstat@latest
```

## Testing methodology

To ensure compatibility and correctness, our testing strategy involves running identical queries against both MySQL and vtgate, then comparing the results and errors from the two systems. This approach helps us verify that vtgate behaves as expected in a variety of scenarios, mimicking MySQL's behavior as closely as possible.
Expand All @@ -37,8 +35,8 @@ This method allows us to thoroughly test queries within a sharded environment, e

## How to use

After installing the `vitess-tester` binary, you need to have Vitess installed and in your path.
To run `vitess-tester` and Vitess, you will need to set the `VTDATAROOT` and `VTROOT` environment variables.
After installing the `vt` binary, you need to have Vitess installed and in your path.
To run `vt tester` and Vitess, you will need to set the `VTDATAROOT` and `VTROOT` environment variables.
You can do this, and set up the Vitess environment by running the following command:

```sh
Expand All @@ -47,92 +45,59 @@ source build.env

Basic usage:
```
Usage of ./vitess-tester:
-alsologtostderr
log to standard error as well as files
-force-base-tablet-uid int
force assigning tablet ports based on this seed
-force-port-start int
force assigning ports based on this seed
-force-vtdataroot string
force path for VTDATAROOT, which may already be populated
-is-coverage
whether coverage is required
-keep-data
don't delete the per-test VTDATAROOT subfolders (default true)
-log-level string
The log level of vitess-tester: info, warn, error, debug. (default "error")
-log_backtrace_at value
when logging hits line file:N, emit a stack trace
-log_dir string
If non-empty, write log files in this directory
-log_link string
If non-empty, add symbolic links in this directory to the log files
-logbuflevel int
Buffer log messages logged at this level or lower (-1 means don't buffer; 0 means buffer INFO only; ...). Has limited applicability on non-prod platforms.
-logtostderr
log to standard error instead of files
-olap
Use OLAP to run the queries.
-partial-keyspace
add a second keyspace for sharded tests and mark first shard as moved to this keyspace in the shard routing rules
-perf-test
include end-to-end performance tests
-sharded
Run all tests on a sharded keyspace and using auto-vschema. This cannot be used with either -vschema or -vtexplain-vschema.
-stderrthreshold value
logs at or above this threshold go to stderr (default 2)
-topo-flavor string
choose a topo server from etcd2, zk2 or consul (default "etcd2")
-trace string
Do a vexplain trace on all queries and store the output in the given file.
-v value
log level for V logs
-vmodule value
comma-separated list of pattern=N settings for file-filtered logging
-vschema string
Disable auto-vschema by providing your own vschema file. This cannot be used with either -vtexplain-vschema or -sharded.
-vtexplain-vschema string
Disable auto-vschema by providing your own vtexplain vschema file. This cannot be used with either -vschema or -sharded.
-xunit
Get output in an xml file instead of errors directory
Test the given workload against both Vitess and MySQL.
Usage:
vt tester [flags]
Examples:
vt tester
Flags:
-h, --help help for tester
--log-level string The log level of vitess-tester: info, warn, error, debug. (default "error")
--olap Use OLAP to run the queries.
--sharded Run all tests on a sharded keyspace and using auto-vschema. This cannot be used with either -vschema or -vtexplain-vschema.
--trace-file string Do a vexplain trace on all queries and store the output in the given file.
--vschema string Disable auto-vschema by providing your own vschema file. This cannot be used with either -vtexplain-vschema or -sharded.
--vtexplain-vschema string Disable auto-vschema by providing your own vtexplain vschema file. This cannot be used with either -vschema or -sharded.
--xunit Get output in an xml file instead of errors directory
```

It will bring up an entire Vitess cluster on 127.0.0.1, unsharded or sharded depending on the configuration. MySQL and VTGate both start with root and no password configured.

```sh
vitess-tester t/example.test # run a specified test
vitess-tester t/example1.test t/example2.test t/example3.test # separate different tests with one or more spaces
vitess-tester t/*.test # wildcards can be used
vitess-tester https://raw.githubusercontent.com/vitessio/vitess-tester/main/t/basic.test # can also be run against an URL
vitess-tester -vtexplain-vschema t/vtexplain-vschema.json t/vtexplain.test # run a test with a custom vschema
vt tester t/example.test # run a specified test
vt tester t/example1.test t/example2.test t/example3.test # separate different tests with one or more spaces
vt tester t/*.test # wildcards can be used
vt tester https://raw.githubusercontent.com/vitessio/vitess-tester/main/t/basic.test # can also be run against an URL
vt tester --vtexplain-vschema t/vtexplain-vschema.json t/vtexplain.test # run a test with a custom vschema
```

The test files can be amended with directives to control the testing process. Check out `directives.test` to see examples of what directives are available.

## Tracing and comparing execution plans

`vitess-tester` can run in tracing mode. When it does, it will not only run the tests but also generate a trace of the query execution plan.
`vt tester` can run in tracing mode. When it does, it will not only run the tests but also generate a trace of the query execution plan.
The trace is created using `vexplain trace`, a tool that provides detailed information about how a query is executed.

To run `vitess-tester` in tracing mode, use the `-trace` flag:
To run `vt tester` in tracing mode, use the `--trace` flag:

```bash
vitess-tester --sharded -trace=trace-log.json t/tpch.test
vt tester --sharded --trace=trace-log.json t/tpch.test
```

This will create a trace log, which you can then either summarize using `vtbenchstat` or compare with another trace log.
This will create a trace log, which you can then either summarize using `vt benchstat` or compare with another trace log.

Running `vtbenchstat` will provide a summary of the trace log, including the number of queries, the number of rows returned, and the time taken to execute the queries.
Running `vt benchstat` will provide a summary of the trace log, including the number of queries, the number of rows returned, and the time taken to execute the queries.

```bash
vtbenchstat trace-log.json
vt benchstat trace-log.json
```

To compare two trace logs, use the `vtbenchstat` command with two trace logs as arguments:
To compare two trace logs, use the `vt benchstat` command with two trace logs as arguments:

```bash
vtbenchstat trace-log1.json trace-log2.json
vt benchstat trace-log1.json trace-log2.json
```

## Contributing
Expand Down
22 changes: 18 additions & 4 deletions src/cmd/vtbenchstat/vtbenchstat.go → go/benchstat/benchstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package benchstat

import (
"encoding/json"
"fmt"
"github.com/alecthomas/chroma/quick"
"github.com/olekukonko/tablewriter"
"golang.org/x/term"
"io"
"math"
"os"
"sort"
"strconv"
"strings"

"github.com/alecthomas/chroma/quick"
"github.com/olekukonko/tablewriter"
"golang.org/x/term"
)

type (
Expand Down Expand Up @@ -63,6 +64,19 @@ type (
}
)

func Run(args []string) {
traces := make([]TraceFile, len(args))
for i, arg := range args {
traces[i] = readTraceFile(arg)
}

if len(traces) == 1 {
printSummary(os.Stdout, terminalWidth(), highlightQuery, traces[0])
} else {
compareTraces(os.Stdout, terminalWidth(), highlightQuery, traces[0], traces[1])
}
}

func visit(trace Trace, f func(Trace)) {
f(trace)
for _, input := range trace.Inputs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package benchstat

import (
"github.com/stretchr/testify/assert"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

var tf1 = TraceFile{
Expand Down
File renamed without changes.
34 changes: 11 additions & 23 deletions src/cmd/vtbenchstat/main.go → go/cmd/benchstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cmd

import (
"flag"
"fmt"
"os"
"github.com/spf13/cobra"
vtbenchstat "github.com/vitessio/vitess-tester/go/benchstat"
)

func main() {
flag.Parse()
args := flag.Args()

if len(args) < 1 || len(args) > 2 {
fmt.Println("Usage: vtbenchstat <trace_file1> [trace_file2]")
os.Exit(1)
}

traces := make([]TraceFile, len(args))
for i, arg := range args {
traces[i] = readTraceFile(arg)
}

if len(traces) == 1 {
printSummary(os.Stdout, terminalWidth(), highlightQuery, traces[0])
} else {
compareTraces(os.Stdout, terminalWidth(), highlightQuery, traces[0], traces[1])
}
var benchstat = &cobra.Command{
Use: "benchstat old_file.json [new_file.json]",
Short: "Compares and analyses a trace output",
Example: "vt benchstat old.json new.json",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
vtbenchstat.Run(args)
},
}
45 changes: 45 additions & 0 deletions go/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2024 The Vitess 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 cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var root = &cobra.Command{
Use: "vt",
Short: "Utils tools for testing, running and benchmarking Vitess.",
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := root.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
root.CompletionOptions.HiddenDefaultCmd = true

root.AddCommand(benchstat)
root.AddCommand(testerCmd())
}
Loading

0 comments on commit d914fab

Please sign in to comment.