Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Set up Python
uses: actions/setup-python@v6
Expand All @@ -41,7 +41,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Set up Python
uses: actions/setup-python@v6
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Run go vet
run: go vet ./...
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Install govulncheck
run: go install golang.org/x/vuln/cmd/govulncheck@latest
Expand All @@ -121,7 +121,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Build
run: go build ./...
Expand Down Expand Up @@ -168,7 +168,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Build pbench
run: go build -o ./bin/pbench
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.4"
go-version: "1.26.5"

- name: Build and package
run: make tar
Expand Down
8 changes: 8 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ func init() {
runCmd.Flags().StringVar(&run.InfluxCfgPath, "influx", "", "InfluxDB connection config for run recorder (optional)")
runCmd.Flags().StringVar(&run.MySQLCfgPath, "mysql", "", "MySQL connection config for run recorder (optional)")
runCmd.Flags().StringVar(&run.PulumiCfgPath, "pulumi", "", "(only works when a MySQL run recorder is specified) Pulumi API config for storing deployment details with MySQL (optional)")
runCmd.Flags().BoolVar(&run.Snapshots, "snapshots", false,
"Periodically save /v1/query snapshots while queries run (DIAGNOSTIC MODE: adds coordinator load, not for timed baseline runs; default false)")
runCmd.Flags().DurationVar(&run.SnapshotInterval, "snapshot-interval", 30*time.Second,
"Polling interval for query json snapshots when --snapshots is on (floor 5s)")
runCmd.Flags().IntVar(&run.SnapshotMax, "snapshot-max", 20,
"Maximum snapshots retained per query (0 = unlimited; must be set explicitly)")
runCmd.Flags().DurationVar(&run.SnapshotFetchTimeout, "snapshot-fetch-timeout", 0,
"Per-fetch timeout for query json snapshots (default: same as --snapshot-interval)")
}
41 changes: 30 additions & 11 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ var (
InfluxCfgPath string
MySQLCfgPath string
PulumiCfgPath string
// Snapshots and friends are the run-wide defaults for periodic query JSON snapshot
// collection (diagnostic mode; see stage/json_snapshot.go). Individual stages can
// override them via save_json_snapshots / json_snapshot_interval / json_snapshot_max /
// json_snapshot_fetch_timeout.
Snapshots bool
SnapshotInterval time.Duration
SnapshotMax int
SnapshotFetchTimeout time.Duration
)

func Run(_ *cobra.Command, args []string) {
Expand All @@ -35,17 +43,7 @@ func Run(_ *cobra.Command, args []string) {
utils.ExpandHomeDirectory(&InfluxCfgPath)
utils.ExpandHomeDirectory(&MySQLCfgPath)
utils.ExpandHomeDirectory(&PulumiCfgPath)
mainStage := &stage.Stage{
States: &stage.SharedStageStates{
RunName: Name,
Comment: Comment,
RandSeed: RandSeed,
RandSkip: RandSkip,
ServerFQDN: parsedServerUrl.Host,
RunStartTime: time.Now(),
OutputPath: OutputPath,
},
}
mainStage := &stage.Stage{States: newSharedStageStates(parsedServerUrl)}

var defaultRunNameBuilder *strings.Builder
if mainStage.States.RunName == "" {
Expand Down Expand Up @@ -86,6 +84,27 @@ func Run(_ *cobra.Command, args []string) {
os.Exit(mainStage.Run(context.Background()))
}

// newSharedStageStates builds the SharedStageStates populated from the run-level CLI flag
// vars, including the four --snapshots/--snapshot-interval/--snapshot-max/
// --snapshot-fetch-timeout vars (M2). Extracted out of Run() - which calls os.Exit() and so
// cannot be safely invoked from a test - specifically so this flag-to-SharedStageStates
// wiring can be asserted directly; see main_test.go.
func newSharedStageStates(parsedServerUrl *url.URL) *stage.SharedStageStates {
return &stage.SharedStageStates{
RunName: Name,
Comment: Comment,
RandSeed: RandSeed,
RandSkip: RandSkip,
ServerFQDN: parsedServerUrl.Host,
RunStartTime: time.Now(),
OutputPath: OutputPath,
SnapshotsEnabled: Snapshots,
SnapshotInterval: SnapshotInterval,
SnapshotMax: SnapshotMax,
SnapshotFetchTimeout: SnapshotFetchTimeout,
}
}

func processStagePath(path string) (st *stage.Stage, returnErr error) {
defer func() {
if returnErr != nil {
Expand Down
95 changes: 95 additions & 0 deletions cmd/run/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package run

import (
"net/url"
"testing"
"time"

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

// M2: asserts the run-level CLI flag vars (Snapshots, SnapshotInterval, SnapshotMax,
// SnapshotFetchTimeout - populated by cmd/run.go's flag registration) are correctly copied
// into SharedStageStates by newSharedStageStates(), for both the documented defaults
// (30s / 20 / 0=use-interval) and explicit overrides. Run() itself calls os.Exit() at the
// end and so cannot be invoked directly from a test; newSharedStageStates() was extracted
// specifically to make this wiring testable in isolation (see main.go).
func TestNewSharedStageStates_SnapshotFlagDefaults(t *testing.T) {
restoreSnapshotVars(t)
// Mirrors the defaults cmd/run.go registers for these flags.
Snapshots = false
SnapshotInterval = 30 * time.Second
SnapshotMax = 20
SnapshotFetchTimeout = 0

states := newSharedStageStates(mustParseURL(t, "http://localhost:8080"))

assert.False(t, states.SnapshotsEnabled)
assert.Equal(t, 30*time.Second, states.SnapshotInterval)
assert.Equal(t, 20, states.SnapshotMax)
assert.Equal(t, time.Duration(0), states.SnapshotFetchTimeout, "0 means \"use the interval\", resolved later in stage.resolveSnapshotSettings")
}

func TestNewSharedStageStates_SnapshotFlagOverrides(t *testing.T) {
restoreSnapshotVars(t)
Snapshots = true
SnapshotInterval = 45 * time.Second
SnapshotMax = 0 // explicit unlimited
SnapshotFetchTimeout = 90 * time.Second

states := newSharedStageStates(mustParseURL(t, "http://localhost:8080"))

assert.True(t, states.SnapshotsEnabled)
assert.Equal(t, 45*time.Second, states.SnapshotInterval)
assert.Equal(t, 0, states.SnapshotMax)
assert.Equal(t, 90*time.Second, states.SnapshotFetchTimeout)
}

// TestNewSharedStageStates_OtherFieldsUnaffected is a light sanity check that the
// pre-existing (non-snapshot) wiring survived the refactor that extracted
// newSharedStageStates() out of Run().
func TestNewSharedStageStates_OtherFieldsUnaffected(t *testing.T) {
restoreSnapshotVars(t)
origName, origComment, origSeed, origSkip, origOutput := Name, Comment, RandSeed, RandSkip, OutputPath
t.Cleanup(func() {
Name, Comment, RandSeed, RandSkip, OutputPath = origName, origComment, origSeed, origSkip, origOutput
})
Name = "my_run"
Comment = "a comment"
RandSeed = 42
RandSkip = 3
OutputPath = "/tmp/somewhere"

states := newSharedStageStates(mustParseURL(t, "http://coordinator.example.com:8889"))

assert.Equal(t, "my_run", states.RunName)
assert.Equal(t, "a comment", states.Comment)
assert.Equal(t, int64(42), states.RandSeed)
assert.Equal(t, 3, states.RandSkip)
assert.Equal(t, "/tmp/somewhere", states.OutputPath)
assert.Equal(t, "coordinator.example.com:8889", states.ServerFQDN)
}

func mustParseURL(t *testing.T, raw string) *url.URL {
t.Helper()
u, err := url.Parse(raw)
require.NoError(t, err)
return u
}

// restoreSnapshotVars snapshots and restores the four package-level snapshot flag vars so
// tests don't leak mutations into each other.
func restoreSnapshotVars(t *testing.T) {
t.Helper()
origSnapshots := Snapshots
origInterval := SnapshotInterval
origMax := SnapshotMax
origTimeout := SnapshotFetchTimeout
t.Cleanup(func() {
Snapshots = origSnapshots
SnapshotInterval = origInterval
SnapshotMax = origMax
SnapshotFetchTimeout = origTimeout
})
}
72 changes: 72 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"pbench/cmd/run"
"testing"
"time"

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

// M2: verifies the four snapshot flags are actually registered on runCmd (cmd/run.go's
// init()) with the documented defaults (design.md §4.1: --snapshots default false,
// --snapshot-interval default 30s, --snapshot-max default 20, --snapshot-fetch-timeout
// default 0 = "use the interval"), and that setting them updates the run package vars they
// are bound to. This is the flag-registration half of the wiring; cmd/run/main_test.go
// covers the SharedStageStates-construction half (newSharedStageStates()).
func TestRunCmdSnapshotFlags_RegisteredWithDocumentedDefaults(t *testing.T) {
flags := runCmd.Flags()

snapshotsFlag := flags.Lookup("snapshots")
require.NotNil(t, snapshotsFlag, "--snapshots must be registered on `pbench run`")
assert.Equal(t, "false", snapshotsFlag.DefValue)

intervalFlag := flags.Lookup("snapshot-interval")
require.NotNil(t, intervalFlag, "--snapshot-interval must be registered on `pbench run`")
assert.Equal(t, "30s", intervalFlag.DefValue)

maxFlag := flags.Lookup("snapshot-max")
require.NotNil(t, maxFlag, "--snapshot-max must be registered on `pbench run`")
assert.Equal(t, "20", maxFlag.DefValue)

fetchTimeoutFlag := flags.Lookup("snapshot-fetch-timeout")
require.NotNil(t, fetchTimeoutFlag, "--snapshot-fetch-timeout must be registered on `pbench run`")
assert.Equal(t, "0s", fetchTimeoutFlag.DefValue, `0 means "use the polling interval", resolved later in stage.resolveSnapshotSettings`)
}

// TestRunCmdSnapshotFlags_ParsingUpdatesBoundVars proves the flags are bound to the exact
// run package vars that newSharedStageStates() reads from (rather than merely registered),
// by parsing a flag set and checking the bound vars afterwards.
func TestRunCmdSnapshotFlags_ParsingUpdatesBoundVars(t *testing.T) {
restoreCmdSnapshotVars(t)

err := runCmd.Flags().Parse([]string{
"--snapshots",
"--snapshot-interval=45s",
"--snapshot-max=7",
"--snapshot-fetch-timeout=90s",
})
require.NoError(t, err)

assert.True(t, run.Snapshots)
assert.Equal(t, 45*time.Second, run.SnapshotInterval)
assert.Equal(t, 7, run.SnapshotMax)
assert.Equal(t, 90*time.Second, run.SnapshotFetchTimeout)
}

// restoreCmdSnapshotVars snapshots and restores the run package's snapshot vars (and resets
// the flags to their defaults) so this test doesn't leak state into others.
func restoreCmdSnapshotVars(t *testing.T) {
t.Helper()
origSnapshots := run.Snapshots
origInterval := run.SnapshotInterval
origMax := run.SnapshotMax
origTimeout := run.SnapshotFetchTimeout
t.Cleanup(func() {
run.Snapshots = origSnapshots
run.SnapshotInterval = origInterval
run.SnapshotMax = origMax
run.SnapshotFetchTimeout = origTimeout
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module pbench

go 1.26.4
go 1.26.5

require (
github.com/alecthomas/participle/v2 v2.1.4
Expand Down
Loading
Loading