Skip to content

Commit facc4e6

Browse files
authored
Fix the initialization and inheritance of cold and warm run values
Fixes #35
1 parent 07d35d5 commit facc4e6

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

cmd/loadjson/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"encoding/json"
77
"fmt"
8-
"github.com/spf13/cobra"
98
"os"
109
"os/signal"
1110
"path/filepath"
@@ -17,6 +16,8 @@ import (
1716
"sync"
1817
"syscall"
1918
"time"
19+
20+
"github.com/spf13/cobra"
2021
)
2122

2223
var (
@@ -74,7 +75,7 @@ func Run(_ *cobra.Command, args []string) {
7475
// To reuse the `pbench run` code, especially run recorders, we create a pseudo main stage.
7576
pseudoStage = &stage.Stage{
7677
Id: "load_json",
77-
ColdRuns: 1,
78+
ColdRuns: &stage.RunsValueOne,
7879
States: &stage.SharedStageStates{
7980
RunName: RunName,
8081
Comment: Comment,

go.mod

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@ go 1.22
55
require (
66
github.com/alecthomas/participle/v2 v2.1.1
77
github.com/docker/go-units v0.5.0
8+
github.com/go-playground/validator/v10 v10.24.0
89
github.com/go-sql-driver/mysql v1.8.1
910
github.com/influxdata/influxdb-client-go/v2 v2.13.0
1011
github.com/pkg/errors v0.9.1
1112
github.com/rs/zerolog v1.32.0
1213
github.com/spf13/cobra v1.8.0
1314
github.com/stretchr/testify v1.9.0
1415
github.com/xhit/go-str2duration/v2 v2.1.0
15-
golang.org/x/sys v0.28.0
16+
golang.org/x/sys v0.29.0
1617
)
1718

1819
require (
1920
filippo.io/edwards25519 v1.1.0 // indirect
2021
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
2122
github.com/davecgh/go-spew v1.1.1 // indirect
23+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
24+
github.com/go-playground/locales v0.14.1 // indirect
25+
github.com/go-playground/universal-translator v0.18.1 // indirect
2226
github.com/google/uuid v1.6.0 // indirect
2327
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2428
github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf // indirect
2529
github.com/kr/pretty v0.1.0 // indirect
30+
github.com/leodido/go-urn v1.4.0 // indirect
2631
github.com/mattn/go-colorable v0.1.13 // indirect
2732
github.com/mattn/go-isatty v0.0.20 // indirect
2833
github.com/oapi-codegen/runtime v1.1.1 // indirect
2934
github.com/pmezard/go-difflib v1.0.0 // indirect
3035
github.com/spf13/pflag v1.0.5 // indirect
31-
golang.org/x/net v0.33.0 // indirect
36+
golang.org/x/crypto v0.32.0 // indirect
37+
golang.org/x/net v0.34.0 // indirect
38+
golang.org/x/text v0.21.0 // indirect
3239
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
3340
gopkg.in/yaml.v3 v3.0.1 // indirect
3441
)

go.sum

+20-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1717
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1818
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
1919
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
20+
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
21+
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
22+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
23+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
24+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
25+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
26+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
27+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
28+
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
29+
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
2030
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
2131
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
2232
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@@ -36,6 +46,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
3646
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
3747
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3848
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
49+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
50+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
3951
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4052
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
4153
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -63,13 +75,17 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
6375
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
6476
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
6577
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
66-
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
67-
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
78+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
79+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
80+
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
81+
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
6882
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6983
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7084
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
72-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
85+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
86+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
87+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
88+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
7389
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7490
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
7591
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

stage/map.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"path/filepath"
88
"pbench/log"
9+
10+
"github.com/go-playground/validator/v10"
911
)
1012

1113
type Map map[string]*Stage
@@ -57,6 +59,11 @@ func ReadStageFromFile(filePath string) (*Stage, error) {
5759
if err = json.Unmarshal(bytes, stage); err != nil {
5860
return nil, fmt.Errorf("failed to parse json %s: %w", filePath, err)
5961
}
62+
validate := validator.New()
63+
err = validate.Struct(stage)
64+
if err != nil {
65+
return nil, fmt.Errorf("failed to parse json %s: %w", filePath, err)
66+
}
6067
for i, queryFile := range stage.QueryFiles {
6168
if !filepath.IsAbs(queryFile) {
6269
queryFile = filepath.Join(stage.BaseDir, queryFile)

stage/stage.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ type Stage struct {
6262
// before exiting.
6363
RandomlyExecuteUntil *string `json:"randomly_execute_until,omitempty"`
6464
// If not set, the default is 1. The default value is set when the stage is run.
65-
ColdRuns int `json:"cold_runs,omitempty"`
65+
ColdRuns *int `json:"cold_runs,omitempty" validate:"omitempty,gte=0"`
6666
// If not set, the default is 0.
67-
WarmRuns int `json:"warm_runs,omitempty"`
67+
WarmRuns *int `json:"warm_runs,omitempty" validate:"omitempty,gte=0"`
6868
// If StartOnNewClient is set to true, this stage will create a new client to execute itself.
6969
// This new client will be passed down to its descendant stages unless those stages also set StartOnNewClient to true.
7070
// Each client can carry their own set of client information, tags, session properties, user credentials, etc.
@@ -415,13 +415,13 @@ func (s *Stage) runQueries(ctx context.Context, queries []string, queryFile *str
415415
if preQueryCycleErr != nil {
416416
return fmt.Errorf("pre-query script execution failed: %w", preQueryCycleErr)
417417
}
418-
for j := 0; j < s.ColdRuns+s.WarmRuns; j++ {
418+
for j := 0; j < *s.ColdRuns+*s.WarmRuns; j++ {
419419
query := &Query{
420420
Text: queryText,
421421
File: queryFile,
422422
Index: i,
423423
BatchSize: batchSize,
424-
ColdRun: j < s.ColdRuns,
424+
ColdRun: j < *s.ColdRuns,
425425
SequenceNo: j,
426426
ExpectedRowCount: -1, // -1 means unspecified.
427427
}

stage/stage_utils.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const (
2020
DefaultStageFileExt = ".json"
2121
)
2222

23+
var (
24+
RunsValueOne = 1
25+
RunsValueZero = 0
26+
)
27+
2328
type OnQueryCompletionFn func(result *QueryResult)
2429

2530
var DefaultNewClientFn = func() *presto.Client {
@@ -66,10 +71,10 @@ func (s *Stage) MergeWith(other *Stage) *Stage {
6671
if other.RandomlyExecuteUntil != nil {
6772
s.RandomlyExecuteUntil = other.RandomlyExecuteUntil
6873
}
69-
if other.ColdRuns > 0 {
74+
if other.ColdRuns != nil {
7075
s.ColdRuns = other.ColdRuns
7176
}
72-
if other.WarmRuns > 0 {
77+
if other.WarmRuns != nil {
7378
s.WarmRuns = other.WarmRuns
7479
}
7580
s.StartOnNewClient = other.StartOnNewClient
@@ -201,8 +206,15 @@ func (s *Stage) setDefaults() {
201206
if s.SaveJson == nil {
202207
s.SaveJson = &falseValue
203208
}
204-
if s.ColdRuns+s.WarmRuns == 0 {
205-
s.ColdRuns = 1
209+
if s.ColdRuns == nil {
210+
s.ColdRuns = &RunsValueZero
211+
}
212+
if s.WarmRuns == nil {
213+
s.WarmRuns = &RunsValueZero
214+
}
215+
if *s.ColdRuns+*s.WarmRuns <= 0 {
216+
s.ColdRuns = &RunsValueOne
217+
s.WarmRuns = &RunsValueZero
206218
}
207219
}
208220

@@ -234,11 +246,17 @@ func (s *Stage) propagateStates() {
234246
nextStage.SessionParams[k] = v
235247
}
236248
}
237-
if nextStage.ColdRuns == 0 {
249+
if nextStage.ColdRuns == nil && nextStage.WarmRuns == nil {
238250
nextStage.ColdRuns = s.ColdRuns
239-
}
240-
if nextStage.WarmRuns == 0 {
241251
nextStage.WarmRuns = s.WarmRuns
252+
} else if nextStage.ColdRuns == nil {
253+
nextStage.ColdRuns = &RunsValueZero
254+
} else if nextStage.WarmRuns == nil {
255+
nextStage.WarmRuns = &RunsValueZero
256+
}
257+
if *nextStage.ColdRuns+*nextStage.WarmRuns <= 0 {
258+
nextStage.ColdRuns = &RunsValueOne
259+
nextStage.WarmRuns = &RunsValueZero
242260
}
243261
if nextStage.AbortOnError == nil {
244262
nextStage.AbortOnError = s.AbortOnError
@@ -344,7 +362,7 @@ func (s *Stage) querySourceString(result *QueryResult) (sourceStr string) {
344362
} else {
345363
sourceStr = fmt.Sprintf("%s_%s", s.Id, sourceStr)
346364
}
347-
if s.ColdRuns+s.WarmRuns > 1 {
365+
if *s.ColdRuns+*s.WarmRuns > 1 {
348366
if result.Query.ColdRun {
349367
sourceStr += "_c"
350368
} else {

0 commit comments

Comments
 (0)