Skip to content

Commit

Permalink
🚑 fix nil panics
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanasati committed Jan 18, 2024
1 parent d8db11e commit 3874adb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Range: {{ .Min }} ... {{ .Max }}
var summaryColor = `
${yellow}Executed Command: ${green}{{ .Command }} ${reset}
${yellow}Total runs: ${green}{{ .Runs }} ${reset}
${yellow}Average time taken: ${green}{{ .AverageElapsed }} ± {{ .StandardDeviation }} ${reset} [User: ${blue}{{ .AverageUser }}${reset}, System: {$blue}{{ .AverageSystem }}{$reset}]
${yellow}Average time taken: ${green}{{ .AverageElapsed }} ± {{ .StandardDeviation }} ${reset} [User: ${blue}{{ .AverageUser }}${reset}, System: ${blue}{{ .AverageSystem }}${reset}]
${yellow}Range: ${green}{{ .Min }} ... {{ .Max }} ${reset}
`

Expand Down
28 changes: 17 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,19 @@ type RunResult struct {
err error
}

func emptyRunResult() *RunResult {
return &RunResult{
elapsed: 0,
user: 0,
system: 0,
err: nil,
}
}

// runs the built command using os/exec and returns the duration the command lasted
func RunCommand(runOpts *RunOptions) *RunResult {
var cmd *exec.Cmd
runResult := &RunResult{
elapsed: 0,
user: 0,
system: 0,
err: nil,
}
runResult := emptyRunResult()
ctx, cancel := context.WithTimeout(context.Background(), runOpts.timeout)
defer cancel()
cmd = exec.CommandContext(ctx, runOpts.command[0], runOpts.command[1:]...)
Expand Down Expand Up @@ -275,7 +279,8 @@ func Benchmark(opts BenchmarkOptions) ([]*RunResult, bool) {
}
startI := 1
if opts.runs < 0 {
var prepareResult, cleanupResult *RunResult
prepareResult := emptyRunResult()
cleanupResult := emptyRunResult()
if opts.executePrepareCmd {
prepareResult = RunCommand(&prepareRunOpts)
if errors.As(prepareResult.err, &processErr) {
Expand Down Expand Up @@ -360,7 +365,8 @@ func Benchmark(opts BenchmarkOptions) ([]*RunResult, bool) {
barMax, pbarOptions...,
)
startI := 1
var prepareResult, cleanupResult *RunResult
prepareResult := emptyRunResult()
cleanupResult := emptyRunResult()

// automatically determine runs
if opts.runs < 0 {
Expand Down Expand Up @@ -605,7 +611,7 @@ func main() {
return
}

var shellCalibration *RunResult
var shellCalibration = emptyRunResult()
if useShell {
shellEmptyCommand, err := buildCommand("''", true, shellPath)
if err != nil {
Expand All @@ -624,7 +630,7 @@ func main() {
cleanupCmd: []string{},
mode: shellMode,
timeout: LargestDuration,
shellCalibration: &RunResult{},
shellCalibration: emptyRunResult(),
}
runs, failed := Benchmark(calibrationOpts)
if failed {
Expand Down Expand Up @@ -653,7 +659,7 @@ func main() {
panic(err)
}
// ! don't remove this println: for some weird reason the above colorstring.Printf
// doesnt' work without this
// ! doesnt' work without this
fmt.Println()

command, err := buildCommand(commandString, useShell, shellPath)
Expand Down

0 comments on commit 3874adb

Please sign in to comment.