1+ package metrics
2+
3+ import (
4+ "sync/atomic"
5+ "time"
6+ )
7+
8+ type Metrics struct {
9+ TableClient map [string ]map [string ]* TableClientMetrics
10+ }
11+
12+ type TableClientMetrics struct {
13+ Resources uint64
14+ Errors uint64
15+ Panics uint64
16+ StartTime time.Time
17+ EndTime time.Time
18+ }
19+
20+ func (s * TableClientMetrics ) Equal (other * TableClientMetrics ) bool {
21+ return s .Resources == other .Resources && s .Errors == other .Errors && s .Panics == other .Panics
22+ }
23+
24+ // Equal compares to stats. Mostly useful in testing
25+ func (s * Metrics ) Equal (other * Metrics ) bool {
26+ for table , clientStats := range s .TableClient {
27+ for client , stats := range clientStats {
28+ if _ , ok := other .TableClient [table ]; ! ok {
29+ return false
30+ }
31+ if _ , ok := other.TableClient [table ][client ]; ! ok {
32+ return false
33+ }
34+ if ! stats .Equal (other.TableClient [table ][client ]) {
35+ return false
36+ }
37+ }
38+ }
39+ for table , clientStats := range other .TableClient {
40+ for client , stats := range clientStats {
41+ if _ , ok := s .TableClient [table ]; ! ok {
42+ return false
43+ }
44+ if _ , ok := s.TableClient [table ][client ]; ! ok {
45+ return false
46+ }
47+ if ! stats .Equal (s.TableClient [table ][client ]) {
48+ return false
49+ }
50+ }
51+ }
52+ return true
53+ }
54+
55+ func (s * Metrics ) TotalErrors () uint64 {
56+ var total uint64
57+ for _ , clientMetrics := range s .TableClient {
58+ for _ , metrics := range clientMetrics {
59+ total += metrics .Errors
60+ }
61+ }
62+ return total
63+ }
64+
65+ func (s * Metrics ) TotalErrorsAtomic () uint64 {
66+ var total uint64
67+ for _ , clientMetrics := range s .TableClient {
68+ for _ , metrics := range clientMetrics {
69+ total += atomic .LoadUint64 (& metrics .Errors )
70+ }
71+ }
72+ return total
73+ }
74+
75+ func (s * Metrics ) TotalPanics () uint64 {
76+ var total uint64
77+ for _ , clientMetrics := range s .TableClient {
78+ for _ , metrics := range clientMetrics {
79+ total += metrics .Panics
80+ }
81+ }
82+ return total
83+ }
84+
85+ func (s * Metrics ) TotalPanicsAtomic () uint64 {
86+ var total uint64
87+ for _ , clientMetrics := range s .TableClient {
88+ for _ , metrics := range clientMetrics {
89+ total += atomic .LoadUint64 (& metrics .Panics )
90+ }
91+ }
92+ return total
93+ }
94+
95+ func (s * Metrics ) TotalResources () uint64 {
96+ var total uint64
97+ for _ , clientMetrics := range s .TableClient {
98+ for _ , metrics := range clientMetrics {
99+ total += metrics .Resources
100+ }
101+ }
102+ return total
103+ }
104+
105+ func (s * Metrics ) TotalResourcesAtomic () uint64 {
106+ var total uint64
107+ for _ , clientMetrics := range s .TableClient {
108+ for _ , metrics := range clientMetrics {
109+ total += atomic .LoadUint64 (& metrics .Resources )
110+ }
111+ }
112+ return total
113+ }
0 commit comments