blob: e20616034393169b66cba24ce172891abe02977d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package parser
import (
"math"
"strings"
"time"
)
// parseTestFunctionDuration parses the duration in seconds as a float64
// from a line of go test output that looks something like this:
// --- PASS: TestOldSchool_PassesWithMessage (0.03 seconds)
func parseTestFunctionDuration(line string) float64 {
line = strings.Replace(line, "(", "", 1)
fields := strings.Split(line, " ")
return parseDurationInSeconds(fields[3]+"s", 2)
}
func parseDurationInSeconds(raw string, precision int) float64 {
elapsed, _ := time.ParseDuration(raw)
return round(elapsed.Seconds(), precision)
}
// round returns the rounded version of x with precision.
//
// Special cases are:
// round(±0) = ±0
// round(±Inf) = ±Inf
// round(NaN) = NaN
//
// Why, oh why doesn't the math package come with a round function?
// Inspiration: http://play.golang.org/p/ZmFfr07oHp
func round(x float64, precision int) float64 {
var rounder float64
pow := math.Pow(10, float64(precision))
intermediate := x * pow
if intermediate < 0.0 {
intermediate -= 0.5
} else {
intermediate += 0.5
}
rounder = float64(int64(intermediate))
return rounder / float64(pow)
}
|