/
timing.go
72 lines (62 loc) · 1.59 KB
/
timing.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package utils
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/logrusorgru/aurora"
"github.com/olekukonko/tablewriter"
)
var profiling = false
type timeLog struct {
Time time.Time
Interval time.Duration
Cumulative time.Duration
Operation string
}
var Timing []timeLog
func ShouldProfile() bool {
profilingEnv, exists := os.LookupEnv("STEAMPIPE_PROFILE")
if exists {
return strings.ToUpper(profilingEnv) == "TRUE"
}
return profiling
}
func LogTime(operation string) {
if !ShouldProfile() {
return
}
lastTimelogIdx := len(Timing) - 1
var elapsed time.Duration
var cumulative time.Duration
if lastTimelogIdx >= 0 {
elapsed = time.Since(Timing[lastTimelogIdx].Time)
cumulative = time.Since(Timing[0].Time)
}
Timing = append(Timing, timeLog{time.Now(), elapsed, cumulative, operation})
}
func DisplayProfileData(op io.Writer) {
if ShouldProfile() {
fmt.Fprint(op, "Timing\n")
var data [][]string
for _, logEntry := range Timing {
var itemData []string
itemData = append(itemData, logEntry.Operation)
if logEntry.Interval > 300*time.Millisecond {
itemData = append(itemData, aurora.Bold(aurora.BrightRed(logEntry.Interval.String())).String())
} else {
itemData = append(itemData, logEntry.Interval.String())
}
itemData = append(itemData, logEntry.Cumulative.String())
data = append(data, itemData)
}
table := tablewriter.NewWriter(op)
table.SetHeader([]string{"Operation", "Elapsed", "Cumulative"})
table.SetBorder(true)
table.SetReflowDuringAutoWrap(false)
table.SetAutoWrapText(false)
table.AppendBulk(data)
table.Render()
}
}