Skip to content

Commit

Permalink
add median
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 7, 2022
1 parent fa559ef commit a9675b2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5
)

require github.com/mattn/go-runewidth v0.0.9 // indirect
require (
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/joshdk/go-junit v0.0.0-20210226021600-6145f504ca0d h1:lcSbmPJf3b19MTZ
github.com/joshdk/go-junit v0.0.0-20210226021600-6145f504ca0d/go.mod h1:TiiV0PqkaNfFXjEiyjWM3XXrhVyCa1K4Zfga6W52ung=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/montanaflynn/stats v0.6.6 h1:Duep6KMIDpY4Yo11iFsvyqJDyfzLF9+sndUKT+v64GQ=
github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
53 changes: 44 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"flag"
"github.com/joshdk/go-junit"
"github.com/montanaflynn/stats"
"github.com/olekukonko/tablewriter"
"path"
"regexp"
Expand Down Expand Up @@ -43,25 +44,58 @@ func formatDuration(d time.Duration) string {
return d.Round(scale / 100).String()
}

func (u *Unit) GetDuration(version string, ticks *bool) (time.Duration, error) {
func (u *Unit) getDurationSum(input []time.Duration) time.Duration {
d := time.Duration(0)
var i int64 = 0
for _, i := range input {
d += i
}

return d
}

func (u *Unit) getDurationAverage(input []time.Duration) time.Duration {
return time.Duration(int64(u.getDurationSum(input)) / int64(len(input)))
}

func (u *Unit) getDurationMedian(input []time.Duration) time.Duration {
var results []float64
for _, i := range input {
results = append(results, float64(i))
}

median, err := stats.Median(results)
if err != nil {
return 0
}

return time.Duration(int64(median) / int64(len(input)))
}

func (u *Unit) GetDuration(version string, ticks *bool, median *bool) (time.Duration, error) {
var results []time.Duration
for _, c := range u.t {
if c.Version == version {
if c.JUnit.Status != "passed" {
return 0, errors.New("-")
}

d = d + c.JUnit.Duration
i++
results = append(results, c.JUnit.Duration)
}
}

if *ticks && i > 0 {
return time.Duration(int64(d) / i), nil
if len(results) == 0 {
return 0, errors.New("-")
}

if *ticks {
if *median {
return u.getDurationMedian(results), nil
}

return u.getDurationAverage(results), nil
}

return d, nil
return u.getDurationSum(results), nil
}

func NewUnit(version string, t junit.Test) Unit {
Expand All @@ -85,6 +119,7 @@ func main() {
ticks := flag.Bool("ticks", false, "Time per ticks")
group := flag.Bool("group", false, "Groups by version")
major := flag.Bool("major", false, "Can only be used with a group")
median := flag.Bool("median", false, "Median search")
rotate := flag.Bool("rotate", false, "Swap versions and names")
directory := flag.String("path", "./build", "Specify folder path")
flag.Parse()
Expand Down Expand Up @@ -168,7 +203,7 @@ func main() {
values = append(values, version)
for _, unitKey := range unitList {
unit := units[unitKey]
if dur, err := unit.GetDuration(version, ticks); err != nil {
if dur, err := unit.GetDuration(version, ticks, median); err != nil {
values = append(values, err.Error())
} else {
values = append(values, formatDuration(dur))
Expand All @@ -186,7 +221,7 @@ func main() {
var values []string
values = append(values, unit.FullName())
for _, version := range versions {
if dur, err := unit.GetDuration(version, ticks); err != nil {
if dur, err := unit.GetDuration(version, ticks, median); err != nil {
values = append(values, err.Error())
} else {
values = append(values, formatDuration(dur))
Expand Down

0 comments on commit a9675b2

Please sign in to comment.