Skip to content

Commit 3bed812

Browse files
committed
add support to run specific (multiple) tests and envs through command line -t flag and change default time from 2s to 15s
1 parent c4db1df commit 3bed812

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ tests.dev.yml
88
dev
99
client_secret.json
1010
binaries
11+
_code/rest/request.json

Diff for: benchmark.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type (
3333
NumberOfConnections uint64 `yaml:"NumberOfConnections"` // defaults to 125.
3434
NumberOfRequests uint64 `yaml:"NumberOfRequests"`
3535
Duration time.Duration `yaml:"Duration"`
36-
Timeout time.Duration `yaml:"Timeout"` // defaults to 2s
36+
Timeout time.Duration `yaml:"Timeout"` // defaults to 15s
3737
Headers map[string]string `yaml:"Headers"`
3838
Method string `yaml:"Method"`
3939
URL string `yaml:"URL"`
@@ -114,9 +114,9 @@ func (t *Test) buildArgs() (args []string) {
114114
t.NumberOfConnections = 125
115115
}
116116

117-
// default timeout to 2 seconds (this can be omitted, as it's the bombardier's default).
117+
// default timeout to 15 seconds (this can be omitted, as it's the bombardier's default).
118118
if t.Timeout == 0 {
119-
t.Timeout = 2 * time.Second
119+
t.Timeout = 15 * time.Second
120120
}
121121

122122
// if not number of requests to fire defined and not a test duration,
@@ -373,6 +373,10 @@ func runBenchmark(t *Test, env *TestEnv) (err error) {
373373
}
374374

375375
func benchmark(t *Test) error {
376+
if len(t.Envs) == 0 {
377+
return nil
378+
}
379+
376380
for _, env := range t.Envs {
377381
if !env.CanBenchmark() {
378382
continue

Diff for: main.go

+36-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const (
2323
var (
2424
waitRunDur = flag.Duration("wait-run", 3*time.Second, "wait time between tests")
2525
testsFile = flag.String("i", "./tests.yml", "yaml file path contains the tests to run")
26-
specificTest = flag.String("t", "", "run only a specific test by its name")
2726
outputDir = flag.String("o", "./", "directory to save generaged Markdown and CSV files")
2827
enableREADMEOutput = flag.Bool("readme", false, "to generate a README.md file near the RESULTS.md")
2928
spreadsheetID = flag.String("g-spreadsheet", "", "Google Spreadsheet ID to send results")
@@ -35,6 +34,8 @@ var (
3534

3635
// server-benchmarks --wait-run=3s -i ./tests.dev.yml -o ./dev -g-spreadsheet $GoogleSpreadsheetID -g-secret client_secret.json
3736
func main() {
37+
var specificTests stringSlice
38+
flag.Var(&specificTests, "t", "run only specific tests by their names") // support multiple -t flags.
3839
flag.Parse()
3940

4041
if _, err := os.Stat("/.dockerenv"); err == nil || os.IsExist(err) {
@@ -44,11 +45,7 @@ func main() {
4445
tests, err := readTests(*testsFile)
4546
catch(err)
4647

47-
if specificTestName := strings.ToLower(*specificTest); specificTestName != "" {
48-
tests = slices.DeleteFunc(tests, func(t *Test) bool {
49-
return strings.ToLower(t.Name) != specificTestName
50-
})
51-
}
48+
tests = filterTests(specificTests, tests...)
5249

5350
// TESTS
5451
for _, t := range tests {
@@ -95,6 +92,39 @@ func readTests(filename string) ([]*Test, error) {
9592
return tests, nil
9693
}
9794

95+
func filterTests(specificTests stringSlice, tests ...*Test) []*Test {
96+
testsAndEnvsToKeep := make(map[string][]string, len(specificTests))
97+
for _, specificTest := range specificTests {
98+
if specificTestName := strings.ToLower(specificTest); specificTestName != "" {
99+
specificTestEnvName := ""
100+
if dotParts := strings.Split(specificTestName, "."); len(dotParts) > 1 {
101+
specificTestName = strings.Join(dotParts[0:len(dotParts)-1], ".") // name all except last dot.
102+
specificTestEnvName = dotParts[len(dotParts)-1]
103+
}
104+
testsAndEnvsToKeep[specificTestName] = append(testsAndEnvsToKeep[specificTestName], specificTestEnvName)
105+
}
106+
}
107+
108+
// Delete all tests and envs that are not in the specificTests list.
109+
return slices.DeleteFunc(tests, func(t *Test) bool {
110+
// keep only the specific test.
111+
testName := strings.ToLower(t.Name)
112+
if specificTestEnvNames, ok := testsAndEnvsToKeep[testName]; ok {
113+
// keep only the specific test's environment.
114+
// E.g. -t rest.iris -t rest.iris-private
115+
if len(specificTestEnvNames) > 0 {
116+
t.Envs = slices.DeleteFunc(t.Envs, func(e *TestEnv) bool {
117+
return !slices.Contains(specificTestEnvNames, strings.ToLower(e.GetName()))
118+
})
119+
}
120+
121+
return false
122+
}
123+
124+
return true
125+
})
126+
}
127+
98128
func cleanup() {
99129
for i, f := range downloadedFiles {
100130
if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) {

Diff for: slice_flag.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "strings"
4+
5+
type stringSlice []string
6+
7+
func (s *stringSlice) String() string {
8+
return strings.Join(*s, ", ")
9+
}
10+
11+
func (s *stringSlice) Set(value string) error {
12+
*s = append(*s, value)
13+
return nil
14+
}
15+
16+
// type sliceFlag[T any] struct {
17+
// slice []T
18+
// }
19+
// func (s *sliceFlag[T]) String() string {
20+
// return fmt.Sprintf("%v", s.slice)
21+
// }
22+
// func (s *sliceFlag[T]) Set(value string) error {
23+
// s.slice = append(s.slice, *(*T)(unsafe.Pointer(&value)))
24+
// return nil
25+
// }

Diff for: tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# or
1212
# Duration: 5s
1313
#
14-
# Timeout: 2s
14+
# Timeout: 15s
1515
# Method: POST
1616
# BodyFile: ./request_payload.json
1717
# Headers:

0 commit comments

Comments
 (0)