Skip to content

Commit

Permalink
add range in results and rounding floats
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanasati committed Jan 3, 2024
1 parent 0a3a19f commit 3a0966f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
9 changes: 7 additions & 2 deletions internal/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Result struct {
Iterations int
Average string
StandardDeviation string
Min string
Max string
}

// todo in all exports, include individual run details
Expand All @@ -29,6 +31,7 @@ Ended: {{ .Ended }}
Executed Command: {{ .Command }}
Total iterations: {{ .Iterations }}
Average time taken: {{ .Average }} ± {{ .StandardDeviation }}
Range: {{ .Min }} ... {{ .Max }}
`

var summaryColor = `
Expand All @@ -40,6 +43,7 @@ ${yellow}Ended: ${green}{{ .Ended }} ${reset}
${yellow}Executed Command: ${green}{{ .Command }} ${reset}
${yellow}Total iterations: ${green}{{ .Iterations }} ${reset}
${yellow}Average time taken: ${green}{{ .Average }} ± {{ .StandardDeviation }} ${reset}
${yellow}Range: ${green}{{ .Min }} ... {{ .Max }} ${reset}
`

// Consolify prints the benchmark summary of the Result struct to the console, with color codes.
Expand Down Expand Up @@ -101,6 +105,7 @@ func markdownify(r *Result) {
| Executed Command | {{.Command}} |
| Total iterations | {{.Iterations}} |
| Average time taken | {{.Average}} ± {{ .StandardDeviation }} |
| Range | {{.Min}} ... {{ .Max }} |
`
tmpl, err := template.New("summary").Parse(text)
if err != nil {
Expand Down Expand Up @@ -133,8 +138,8 @@ func jsonify(r *Result) ([]byte, error) {
// csvify converts the Result struct to CSV.
func csvify(r *Result) {
text := `
Started,Ended,Executed Command,Total iterations,Average time taken
{{.Started}}, {{.Ended}}, {{.Command}}, {{.Iterations}}, {{.Average}} ± {{ .StandardDeviation }}
Started,Ended,Executed Command,Total iterations,Average time taken,Range
{{.Started}}, {{.Ended}}, {{.Command}}, {{.Iterations}}, {{.Average}} ± {{ .StandardDeviation }}, {{.Min}} ... {{.Max}}
`
tmpl, err := template.New("summary").Parse(text)
if err != nil {
Expand Down
20 changes: 17 additions & 3 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ func format(text string, params map[string]string) string {
return text
}

// MapFunc returns a slice of all elements in the given slice mapped by the given function.
func MapFunc[T any, S any](function func (T) S, slice []T) ([]S) {
mappedSlice := make([]S, len(slice))
for i, v := range slice {
mappedSlice[i] = function(v)
}
return mappedSlice
}

// writeToFile writes text string to the given filename.
func writeToFile(text, filename string) (err error) {
f, err := os.Create(filename)
Expand All @@ -31,6 +40,11 @@ func writeToFile(text, filename string) (err error) {
return err
}

func roundFloat(num float64, digits int) float64 {
tenMultiplier := math.Pow10(digits)
return math.Round(num * tenMultiplier) / tenMultiplier
}

type numberLike interface {
~int | ~float64 | ~int32 | ~int64 | ~float32
}
Expand All @@ -49,7 +63,7 @@ func ComputeAverageAndStandardDeviation[T numberLike](population []T) (float64,
deviationSum /= n
deviationSum = math.Sqrt(deviationSum)

return avg, deviationSum
return roundFloat(avg, 2), roundFloat(deviationSum, 2)
}

func checkPathExists(fp string) bool {
Expand Down Expand Up @@ -104,11 +118,11 @@ func DurationFromNumber[T numberLike](number T, unit time.Duration) (time.Durati
// this function is only used internally, panic if unknown time unit is passed
panic("unknown time unit in DurationFromNumber: " + unit.String())
}
timeString := fmt.Sprintf("%.2v%v", number, suffix)
timeString := fmt.Sprintf("%v%v", number, suffix)
duration, err := time.ParseDuration(timeString)
if err != nil {
// again, function only used internally, invalid duration must not be present
panic("unable to parse duration in DurationFromNumber: " + err.Error())
panic("unable to parse duration" + timeString + "in DurationFromNumber: " + err.Error())
}
return duration
}
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"slices"

Check failure on line 7 in main.go

View workflow job for this annotation

GitHub Actions / build

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/slices)
"strings"
"time"

Expand All @@ -22,8 +23,6 @@ const (
// NO_COLOR is a global variable that is used to determine whether or not to enable color output.
var NO_COLOR bool = false

// todo add range too

func run(command []string, verbose bool, ignoreError bool) (time.Duration, error) {
// todo add shell support
// todo measure shell spawn time too and deduct it from runs
Expand Down Expand Up @@ -59,6 +58,7 @@ func main() {

updateCh := make(chan string, 1)
go internal.CheckForUpdates(VERSION, &updateCh)
defer fmt.Println(<-updateCh)

// * basic configuration
commando.
Expand Down Expand Up @@ -122,6 +122,10 @@ func main() {
internal.Log("red", "Application error: cannot parse flag values.")
}

if iterations <= 0 {
return
}

// warmup runs
for i := 0; i < warmupRuns; i++ {
// todo replace running logs with a progress bar in non-verbose mode
Expand All @@ -132,7 +136,7 @@ func main() {
}
}

// actual runs
// actual runs, each entry stored in microseconds
var runs []int64
started := time.Now().Format("02-01-2006 15:04:05")
// * looping for given iterations
Expand All @@ -152,13 +156,19 @@ func main() {
avg, stddev := internal.ComputeAverageAndStandardDeviation(runs)
avgDuration := internal.DurationFromNumber(avg, time.Microsecond)
stddevDuration := internal.DurationFromNumber(stddev, time.Microsecond)
max_ := slices.Max(runs)
min_ := slices.Min(runs)
maxDuration := internal.DurationFromNumber(max_, time.Microsecond)
minDuration := internal.DurationFromNumber(min_, time.Microsecond)
result := internal.Result{
Started: started,
Ended: ended,
Command: strings.Join(command, " "),
Iterations: iterations,
Average: avgDuration.String(),
StandardDeviation: stddevDuration.String(),
Max: maxDuration.String(),
Min: minDuration.String(),
}

result.Consolify()
Expand All @@ -172,7 +182,7 @@ func main() {
result.Export(exportFormat)

})
// todo detect statistical outliers

commando.Parse(nil)
fmt.Println(<-updateCh)
}

0 comments on commit 3a0966f

Please sign in to comment.