go(stats/table): let the printing func just print
All checks were successful
continuous-integration/drone/push Build is passing

...and handle constructing the actual table in a separate func.
also, add more tests.
This commit is contained in:
surtur 2022-06-27 21:56:44 +02:00
parent 58edf3c506
commit 13fd0b3bff
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 58 additions and 5 deletions

View File

@ -34,12 +34,15 @@ func PrintStatisticTable(algoStats [][]Stats) {
fmt.Fprintln(os.Stderr, "printing statistic table data (min, max, mean, median, stddev)")
for _, singleFunc := range algoStats {
statsSingleFunc(singleFunc)
fmt.Fprintln(os.Stderr, statsSingleFunc(singleFunc))
}
}
// statsSingleFunc computes statistics out of results of a single bench func.
func statsSingleFunc(singleFuncStats []Stats) {
// statsSingleFunc computes statistics out of results of a single bench func
// and returns the statistic table as a []interface{} because it contains
// multiple table headers and rows. perhaps a table struct could be returned in
// the future.
func statsSingleFunc(singleFuncStats []Stats) []interface{} {
var (
// hdr is the table header as determined based on the data being dealt with.
hdr statsHdr
@ -47,6 +50,10 @@ func statsSingleFunc(singleFuncStats []Stats) {
row statsRow
)
// out contains the constructed table and is returned at the end of
// this func.
out := make([]interface{}, 0)
for _, s := range singleFuncStats {
for _, dim := range s.BenchFuncStats {
hdr = makeTableHdr(
@ -56,7 +63,7 @@ func statsSingleFunc(singleFuncStats []Stats) {
s.Generations,
s.Iterations,
)
fmt.Fprintln(os.Stderr, hdr)
out = append(out, "\n", hdr, "\n")
// collect the best.
var best []float64
@ -73,9 +80,13 @@ func statsSingleFunc(singleFuncStats []Stats) {
row.Median = stat.Mean(best, nil)
row.StdDev = stat.StdDev(best, nil)
fmt.Fprintln(os.Stderr, row)
out = append(out, row)
}
}
out = append(out, "\n")
return out
}
// makeTableHdr fills the table header with passed information.

View File

@ -27,3 +27,45 @@ func TestNewStatsHdr(t *testing.T) {
t.Error("could not create hdr")
}
}
func TestStatsSingleFunc(t *testing.T) {
testHdr := makeTableHdr("Aladeen Search", "De Jong 5th", 5, 3, 4)
benchFuncStats := []FuncStats{
{
BenchName: "De Jong 5th",
Solution: []BenchRound{
{Iteration: 0, Results: []float64{2803.7015205977887, 2296.736381649773, 1763.9319525203364}},
{Iteration: 1, Results: []float64{2169.7378893600176, 2169.7378893600176, 2169.7378893600176}},
{Iteration: 2, Results: []float64{1909.6488355929007, 1336.3261400058473, 1336.3261400058473}},
{Iteration: 3, Results: []float64{2227.0782796718786, 2227.0782796718786, 1681.0667739991388}},
},
},
}
testStats := []Stats{
{
Algo: "Aladeen Search",
Dimens: 5,
BenchFuncStats: benchFuncStats,
Iterations: 4,
Generations: 3,
},
}
want := []interface{}{
"\n",
testHdr,
"\n",
statsRow{1336.3261400058473, 2169.7378893600176, 1737.765688971335, 1737.765688971335, 342.37072192259393},
"\n",
}
got := statsSingleFunc(testStats)
if len(want) != len(got) {
t.Errorf("outputs are of different sizes, want: %d, got: %d", len(want), len(got))
}
for i := range want {
if want[i] != got[i] {
t.Errorf("outputs don't match, want: %v, got: %v", want, got)
}
}
}