-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatistics.go
More file actions
97 lines (76 loc) · 2 KB
/
statistics.go
File metadata and controls
97 lines (76 loc) · 2 KB
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
package main
import (
"fmt"
"sync"
"time"
)
type Statistics struct {
duplicatedCount, totalCount int64
blockTimestamps map[uint64]int64
txTimestamps map[uint64][]int64
processorLagSum int64
processorLagMax int64
processorLagCount int64
mu sync.Mutex
}
func newStatistics() *Statistics {
return &Statistics{
blockTimestamps: make(map[uint64]int64),
txTimestamps: make(map[uint64][]int64),
}
}
func (s *Statistics) add(blockTs int64, slot uint64, duplicated bool, timestamp time.Time, processingTime time.Time) {
s.mu.Lock()
defer s.mu.Unlock()
if duplicated {
s.duplicatedCount++
}
s.totalCount++
if blockTs > 0 {
s.blockTimestamps[slot] = blockTs * 1000
}
if !duplicated {
s.txTimestamps[slot] = append(s.txTimestamps[slot], timestamp.UnixMilli())
}
}
func (s *Statistics) report() {
s.mu.Lock()
defer s.mu.Unlock()
fmt.Printf("-----------------------------------------------------------\n")
fmt.Printf("total txs processed: %d duplicate transactions: %d (%.1f %%) \n", s.totalCount, s.duplicatedCount,
float64(s.duplicatedCount)*100/float64(s.totalCount))
count := int64(0)
sumLag := int64(0)
maxLag := int64(0)
for slot, txts := range s.txTimestamps {
slotTs, hasTx := s.blockTimestamps[slot]
if !hasTx {
continue
}
for _, ts := range txts {
lag := ts - slotTs
if lag > maxLag {
maxLag = lag
}
sumLag += lag
count++
}
}
if count > 0 {
fmt.Printf("Average lag to block time %d msec, max lag %d msec\n", sumLag/count, maxLag)
}
if s.processorLagCount > 0 {
fmt.Printf("Average lag to message time %d msec, max lag %d msec\n", s.processorLagSum/s.processorLagCount, s.processorLagMax)
}
fmt.Printf("-----------------------------------------------------------\n")
}
func (s *Statistics) record(timestamp time.Time, processingTime time.Time) {
s.mu.Lock()
defer s.mu.Unlock()
lag := processingTime.Sub(timestamp).Milliseconds()
s.processorLagCount++
s.processorLagSum += lag
if lag > s.processorLagMax {
s.processorLagMax = lag
}
}