Skip to content

Commit

Permalink
bump go version + move result file content + standard deviation in re…
Browse files Browse the repository at this point in the history
…sult
  • Loading branch information
shravanasati committed Dec 30, 2023
1 parent 354031f commit d835b86
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module github.com/Shravan-1908/bench

go 1.16
go 1.20

require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/thatisuday/commando v1.0.4
)

require github.com/thatisuday/clapper v1.0.10 // indirect
17 changes: 13 additions & 4 deletions internal/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import (
"text/template"
)

// Result struct which is shown at the end as benchmarking summary and is written to a file.
type Result struct {
Started string
Ended string
Command string
Iterations int
Average string
StandardDeviation string
}

var summaryNoColor = `
Benchmarking Summary
--------------------
Expand All @@ -14,7 +24,7 @@ Started: {{ .Started }}
Ended: {{ .Ended }}
Executed Command: {{ .Command }}
Total iterations: {{ .Iterations }}
Average time taken: {{ .Average }}
Average time taken: {{ .Average }} ± {{ .StandardDeviation }}
`

var summaryColor = `
Expand All @@ -25,9 +35,8 @@ ${yellow}Started: ${green}{{ .Started }} ${reset}
${yellow}Ended: ${green}{{ .Ended }} ${reset}
${yellow}Executed Command: ${green}{{ .Command }} ${reset}
${yellow}Total iterations: ${green}{{ .Iterations }} ${reset}
${yellow}Average time taken: ${green}{{ .Average }} ${reset}
${yellow}Average time taken: ${green}{{ .Average }} ± {{ .StandardDeviation }} ${reset}
`
// todo add standard deviation to summary

// Consolify prints the benchmark summary of the Result struct to the console, with color codes.
func (result *Result) Consolify() {
Expand Down Expand Up @@ -126,6 +135,6 @@ func (result *Result) Export(exportFormat string) {
markdownify(result)

} else if exportFormat != "none" {
Log("red", "Invalid export format: " + exportFormat + ".")
Log("red", "Invalid export format: "+exportFormat+".")
}
}
10 changes: 0 additions & 10 deletions internal/result.go

This file was deleted.

10 changes: 6 additions & 4 deletions internal/up.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package internal

// todo replace self updater with check for updates

import (
"io"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"sync"
)


// Update updates bench by downloading the latest executable from github, and renaming the
// old executable to `bench-old` so that it can be deleted by `DeletePreviousInstallation`.
func Update() {
Expand Down Expand Up @@ -80,16 +81,17 @@ func Update() {
}

// DeletePreviousInstallation deletes previous installation if it exists.
func DeletePreviousInstallation() {
func DeletePreviousInstallation(wg *sync.WaitGroup) {
benchDir, _ := os.UserHomeDir()
benchDir += "/.bench"

files, _ := ioutil.ReadDir(benchDir)
files, _ := os.ReadDir(benchDir)
for _, f := range files {
if strings.HasSuffix(f.Name(), "-old") {
// fmt.Println("found existsing installation")
os.Remove(benchDir + "/" + f.Name())
}
// fmt.Println(f.Name())
}
wg.Done()
}
22 changes: 22 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"math"
"os"
"strings"
)
Expand All @@ -25,3 +26,24 @@ func writeToFile(text, filename string) (err error) {
_, err = f.WriteString(text)
return err
}

type numberLike interface {
~int | ~float64 | ~int32 | ~int64 | ~float32

Check failure on line 31 in internal/utils.go

View workflow job for this annotation

GitHub Actions / build

invalid character U+007E '~'

Check failure on line 31 in internal/utils.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected |, expecting semicolon or newline or }
}

func ComputeAverageAndStandardDeviation[T numberLike](population []T) (float64, float64) {

Check failure on line 34 in internal/utils.go

View workflow job for this annotation

GitHub Actions / build

missing function body

Check failure on line 34 in internal/utils.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected [, expecting (
var deviationSum float64 = 0
var avg float64 = 0
n := float64(len(population))

Check failure on line 37 in internal/utils.go

View workflow job for this annotation

GitHub Actions / build

syntax error: non-declaration statement outside function body
for _, v := range population {
avg += float64(v)
}
avg /= n
for _, v := range population {
deviationSum += math.Pow((float64(v) - avg), 2)
}
deviationSum /= n
deviationSum = math.Sqrt(deviationSum)

return avg, deviationSum
}
27 changes: 21 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"os"
"os/exec"
"strings"
"sync"
"time"

"github.com/Shravan-1908/bench/internal"
"github.com/thatisuday/commando"
"github.com/google/shlex"
"github.com/thatisuday/commando"
)

const (
Expand Down Expand Up @@ -55,7 +56,9 @@ func run(command []string, verbose bool, ignoreError bool) (time.Duration, error
func main() {
internal.Log("white", fmt.Sprintf("%v %v\n", NAME, VERSION))

go internal.DeletePreviousInstallation()
var wg sync.WaitGroup
wg.Add(1)
go internal.DeletePreviousInstallation(&wg)

// * basic configuration
commando.
Expand Down Expand Up @@ -130,9 +133,8 @@ func main() {
}

// actual runs
var sum time.Duration
var runs []int64
started := time.Now().Format("02-01-2006 15:04:05")

// * looping for given iterations
for i := 1; i <= iterations; i++ {
internal.Log("purple", fmt.Sprintf("***********\nRunning iteration %d\n***********", i))
Expand All @@ -141,18 +143,30 @@ func main() {
if e != nil {
return
}
sum += dur
runs = append(runs, (dur.Microseconds()))
}

ended := time.Now().Format("02-01-2006 15:04:05")

// * intialising the template struct
avg, stddev := internal.ComputeAverageAndStandardDeviation(runs)
avgString := fmt.Sprintf("%.2fus", avg)
avgDuration, err := time.ParseDuration(avgString)
if err != nil {
panic("unable to parse duration from string: " + avgString + "\n" + err.Error())
}
stddevString := fmt.Sprintf("%.2fus", stddev)
stddevDuration, err := time.ParseDuration(stddevString)
if err != nil {
panic("unable to parse duration from string: " + stddevString + "\n" + err.Error())
}
result := internal.Result{
Started: started,
Ended: ended,
Command: strings.Join(command, " "),
Iterations: iterations,
Average: (sum / time.Duration(iterations)).String(),
Average: avgDuration.String(),
StandardDeviation: stddevDuration.String(),
}

result.Consolify()
Expand Down Expand Up @@ -182,4 +196,5 @@ func main() {
})

commando.Parse(nil)
wg.Wait()
}

0 comments on commit d835b86

Please sign in to comment.