-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtiming.go
163 lines (139 loc) · 3.81 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Copyright (c) 2020-2023 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"fmt"
"os"
"time"
"github.com/markkurossi/mpc/p2p"
"github.com/markkurossi/tabulate"
)
// Timing records timing samples and renders a profiling report.
type Timing struct {
Start time.Time
Samples []*Sample
}
// NewTiming creates a new Timing instance.
func NewTiming() *Timing {
return &Timing{
Start: time.Now(),
}
}
// Sample adds a timing sample with label and data columns.
func (t *Timing) Sample(label string, cols []string) *Sample {
start := t.Start
if len(t.Samples) > 0 {
start = t.Samples[len(t.Samples)-1].End
}
sample := &Sample{
Label: label,
Start: start,
End: time.Now(),
Cols: cols,
}
t.Samples = append(t.Samples, sample)
return sample
}
// Print prints profiling report to standard output.
func (t *Timing) Print(stats p2p.IOStats) {
if len(t.Samples) == 0 {
return
}
sent := stats.Sent.Load()
received := stats.Recvd.Load()
flushed := stats.Flushed.Load()
tab := tabulate.New(tabulate.UnicodeLight)
tab.Header("Op").SetAlign(tabulate.ML)
tab.Header("Time").SetAlign(tabulate.MR)
tab.Header("%").SetAlign(tabulate.MR)
tab.Header("Xfer").SetAlign(tabulate.MR)
total := t.Samples[len(t.Samples)-1].End.Sub(t.Start)
for _, sample := range t.Samples {
row := tab.Row()
row.Column(sample.Label)
duration := sample.End.Sub(sample.Start)
row.Column(fmt.Sprintf("%s", duration.String()))
row.Column(fmt.Sprintf("%.2f%%",
float64(duration)/float64(total)*100))
for _, col := range sample.Cols {
row.Column(col)
}
for idx, sub := range sample.Samples {
row := tab.Row()
var prefix string
if idx+1 >= len(sample.Samples) {
prefix = "\u2570\u2574"
} else {
prefix = "\u251C\u2574"
}
row.Column(prefix + sub.Label).SetFormat(tabulate.FmtItalic)
var d time.Duration
if sub.Abs > 0 {
d = sub.Abs
} else {
d = sub.End.Sub(sub.Start)
}
row.Column(d.String()).SetFormat(tabulate.FmtItalic)
row.Column(
fmt.Sprintf("%.2f%%", float64(d)/float64(duration)*100)).
SetFormat(tabulate.FmtItalic)
}
}
row := tab.Row()
row.Column("Total").SetFormat(tabulate.FmtBold)
row.Column(t.Samples[len(t.Samples)-1].End.Sub(t.Start).String()).
SetFormat(tabulate.FmtBold)
row.Column("").SetFormat(tabulate.FmtBold)
row.Column(FileSize(sent + received).String()).SetFormat(tabulate.FmtBold)
row = tab.Row()
row.Column("\u251C\u2574Sent").SetFormat(tabulate.FmtItalic)
row.Column("")
row.Column(
fmt.Sprintf("%.2f%%", float64(sent)/float64(sent+received)*100)).
SetFormat(tabulate.FmtItalic)
row.Column(FileSize(sent).String()).SetFormat(tabulate.FmtItalic)
row = tab.Row()
row.Column("\u251C\u2574Rcvd").SetFormat(tabulate.FmtItalic)
row.Column("")
row.Column(
fmt.Sprintf("%.2f%%", float64(received)/float64(sent+received)*100)).
SetFormat(tabulate.FmtItalic)
row.Column(FileSize(received).String()).SetFormat(tabulate.FmtItalic)
row = tab.Row()
row.Column("\u2570\u2574Flcd").SetFormat(tabulate.FmtItalic)
row.Column("")
row.Column("")
row.Column(fmt.Sprintf("%v", flushed)).SetFormat(tabulate.FmtItalic)
tab.Print(os.Stdout)
}
// Sample contains information about one timing sample.
type Sample struct {
Label string
Start time.Time
End time.Time
Abs time.Duration
Cols []string
Samples []*Sample
}
// SubSample adds a sub-sample for a timing sample.
func (s *Sample) SubSample(label string, end time.Time) {
start := s.Start
if len(s.Samples) > 0 {
start = s.Samples[len(s.Samples)-1].End
}
s.Samples = append(s.Samples, &Sample{
Label: label,
Start: start,
End: end,
})
}
// AbsSubSample adds an absolute sub-sample for a timing sample.
func (s *Sample) AbsSubSample(label string, duration time.Duration) {
s.Samples = append(s.Samples, &Sample{
Label: label,
Abs: duration,
})
}