-
Notifications
You must be signed in to change notification settings - Fork 178
/
metrics.go
84 lines (71 loc) · 3.05 KB
/
metrics.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
package fvm
import (
"time"
"github.com/onflow/cadence/runtime/common"
)
// A MetricsCollector accumulates performance metrics reported by the Cadence runtime.
//
// A single collector instance will sum all reported values. For example, the "parsed" field will be
// incremented each time a program is parsed.
type MetricsCollector struct {
parsed time.Duration
checked time.Duration
interpreted time.Duration
valueEncoded time.Duration
valueDecoded time.Duration
}
// NewMetricsCollectors returns a new runtime metrics collector.
func NewMetricsCollector() *MetricsCollector {
return &MetricsCollector{}
}
func (m *MetricsCollector) Parsed() time.Duration { return m.parsed }
func (m *MetricsCollector) Checked() time.Duration { return m.checked }
func (m *MetricsCollector) Interpreted() time.Duration { return m.interpreted }
func (m *MetricsCollector) ValueEncoded() time.Duration { return m.valueEncoded }
func (m *MetricsCollector) ValueDecoded() time.Duration { return m.valueDecoded }
type metricsCollector struct {
*MetricsCollector
}
func (m metricsCollector) ProgramParsed(location common.Location, duration time.Duration) {
if m.MetricsCollector != nil && location != nil {
// These checks prevent re-reporting durations, the metrics collection is a bit counter-intuitive:
// The three functions (parsing, checking, interpretation) are not called in sequence, but in some cases as part of each other.
// We basically only measure the durations reported for the entry-point (the transaction), and not for child locations,
// because they might be already part of the duration for the entry-point.
if _, ok := location.(common.TransactionLocation); ok {
m.parsed = duration
}
}
}
func (m metricsCollector) ProgramChecked(location common.Location, duration time.Duration) {
if m.MetricsCollector != nil && location != nil {
// see the comment for ProgramParsed
if _, ok := location.(common.TransactionLocation); ok {
m.checked = duration
}
}
}
func (m metricsCollector) ProgramInterpreted(location common.Location, duration time.Duration) {
if m.MetricsCollector != nil && location != nil {
// see the comment for ProgramInterpreted
if _, ok := location.(common.TransactionLocation); ok {
m.interpreted = duration
}
}
}
func (m metricsCollector) ValueEncoded(duration time.Duration) {
if m.MetricsCollector != nil {
m.valueEncoded += duration
}
}
func (m metricsCollector) ValueDecoded(duration time.Duration) {
if m.MetricsCollector != nil {
m.valueDecoded += duration
}
}
type noopMetricsCollector struct{}
func (m noopMetricsCollector) ProgramParsed(location common.Location, duration time.Duration) {}
func (m noopMetricsCollector) ProgramChecked(location common.Location, duration time.Duration) {}
func (m noopMetricsCollector) ProgramInterpreted(location common.Location, duration time.Duration) {}
func (m noopMetricsCollector) ValueEncoded(duration time.Duration) {}
func (m noopMetricsCollector) ValueDecoded(duration time.Duration) {}