-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_test.go
More file actions
286 lines (230 loc) · 8.07 KB
/
metrics_test.go
File metadata and controls
286 lines (230 loc) · 8.07 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package paywall
import (
"sync"
"testing"
"time"
)
func TestMetricsCollector_MultisigOperations(t *testing.T) {
m := NewMetricsCollector()
m.IncrementMultisigAddressGenerated()
m.IncrementMultisigAddressGenerated()
m.IncrementPartialSignatureSubmitted()
m.IncrementMultisigTransactionCompleted()
snapshot := m.Snapshot()
if snapshot.MultisigAddressGenerated != 2 {
t.Errorf("expected 2 multisig addresses generated, got %d", snapshot.MultisigAddressGenerated)
}
if snapshot.PartialSignatureSubmitted != 1 {
t.Errorf("expected 1 partial signature submitted, got %d", snapshot.PartialSignatureSubmitted)
}
if snapshot.MultisigTransactionCompleted != 1 {
t.Errorf("expected 1 multisig transaction completed, got %d", snapshot.MultisigTransactionCompleted)
}
}
func TestMetricsCollector_EscrowStateTransitions(t *testing.T) {
m := NewMetricsCollector()
m.IncrementEscrowCreated()
m.IncrementEscrowFunded()
m.IncrementEscrowCompleted()
m.IncrementEscrowDisputed()
m.IncrementEscrowDisputeResolved()
snapshot := m.Snapshot()
if snapshot.EscrowCreated != 1 {
t.Errorf("expected 1 escrow created, got %d", snapshot.EscrowCreated)
}
if snapshot.EscrowFunded != 1 {
t.Errorf("expected 1 escrow funded, got %d", snapshot.EscrowFunded)
}
if snapshot.EscrowCompleted != 1 {
t.Errorf("expected 1 escrow completed, got %d", snapshot.EscrowCompleted)
}
if snapshot.EscrowDisputed != 1 {
t.Errorf("expected 1 escrow disputed, got %d", snapshot.EscrowDisputed)
}
if snapshot.EscrowDisputeResolved != 1 {
t.Errorf("expected 1 escrow dispute resolved, got %d", snapshot.EscrowDisputeResolved)
}
}
func TestMetricsCollector_DisputeResolutionTiming(t *testing.T) {
m := NewMetricsCollector()
// Record 3 dispute resolutions
m.RecordDisputeResolutionDuration(100 * time.Millisecond)
m.RecordDisputeResolutionDuration(200 * time.Millisecond)
m.RecordDisputeResolutionDuration(300 * time.Millisecond)
snapshot := m.Snapshot()
if snapshot.DisputeResolutionCount != 3 {
t.Errorf("expected 3 dispute resolutions, got %d", snapshot.DisputeResolutionCount)
}
expectedTotal := int64(600)
if snapshot.DisputeResolutionTotalMs != expectedTotal {
t.Errorf("expected total %d ms, got %d ms", expectedTotal, snapshot.DisputeResolutionTotalMs)
}
expectedAvg := int64(200)
if snapshot.DisputeResolutionAvgMs != expectedAvg {
t.Errorf("expected avg %d ms, got %d ms", expectedAvg, snapshot.DisputeResolutionAvgMs)
}
}
func TestMetricsCollector_PaymentOperations(t *testing.T) {
m := NewMetricsCollector()
m.IncrementPaymentCreated()
m.IncrementPaymentCreated()
m.IncrementPaymentConfirmed()
m.IncrementPaymentExpired()
snapshot := m.Snapshot()
if snapshot.PaymentCreated != 2 {
t.Errorf("expected 2 payments created, got %d", snapshot.PaymentCreated)
}
if snapshot.PaymentConfirmed != 1 {
t.Errorf("expected 1 payment confirmed, got %d", snapshot.PaymentConfirmed)
}
if snapshot.PaymentExpired != 1 {
t.Errorf("expected 1 payment expired, got %d", snapshot.PaymentExpired)
}
}
func TestMetricsCollector_ErrorMetrics(t *testing.T) {
m := NewMetricsCollector()
m.IncrementSignatureVerificationFailed()
m.IncrementTransactionBroadcastFailed()
m.IncrementEscrowTimeoutTriggered()
m.IncrementArbiterConsensusRequired()
snapshot := m.Snapshot()
if snapshot.SignatureVerificationFailed != 1 {
t.Errorf("expected 1 signature verification failed, got %d", snapshot.SignatureVerificationFailed)
}
if snapshot.TransactionBroadcastFailed != 1 {
t.Errorf("expected 1 transaction broadcast failed, got %d", snapshot.TransactionBroadcastFailed)
}
if snapshot.EscrowTimeoutTriggered != 1 {
t.Errorf("expected 1 escrow timeout triggered, got %d", snapshot.EscrowTimeoutTriggered)
}
if snapshot.ArbiterConsensusRequired != 1 {
t.Errorf("expected 1 arbiter consensus required, got %d", snapshot.ArbiterConsensusRequired)
}
}
func TestMetricsCollector_PerformanceMetrics(t *testing.T) {
m := NewMetricsCollector()
m.RecordAddressGenerationDuration(50 * time.Millisecond)
m.RecordAddressGenerationDuration(100 * time.Millisecond)
m.RecordSignatureVerificationDuration(25 * time.Millisecond)
m.RecordStateTransitionDuration(10 * time.Millisecond)
snapshot := m.Snapshot()
if snapshot.AddressGenerationDurationMs != 150 {
t.Errorf("expected 150ms address generation, got %d ms", snapshot.AddressGenerationDurationMs)
}
if snapshot.SignatureVerificationDurationMs != 25 {
t.Errorf("expected 25ms signature verification, got %d ms", snapshot.SignatureVerificationDurationMs)
}
if snapshot.StateTransitionDurationMs != 10 {
t.Errorf("expected 10ms state transition, got %d ms", snapshot.StateTransitionDurationMs)
}
}
func TestMetricsCollector_Reset(t *testing.T) {
m := NewMetricsCollector()
// Set some metrics
m.IncrementPaymentCreated()
m.IncrementEscrowCreated()
m.RecordDisputeResolutionDuration(100 * time.Millisecond)
// Verify metrics are set
snapshot1 := m.Snapshot()
if snapshot1.PaymentCreated == 0 {
t.Error("expected payment created to be set before reset")
}
// Reset metrics
m.Reset()
// Verify all metrics are zero
snapshot2 := m.Snapshot()
if snapshot2.PaymentCreated != 0 {
t.Errorf("expected payment created to be 0 after reset, got %d", snapshot2.PaymentCreated)
}
if snapshot2.EscrowCreated != 0 {
t.Errorf("expected escrow created to be 0 after reset, got %d", snapshot2.EscrowCreated)
}
if snapshot2.DisputeResolutionCount != 0 {
t.Errorf("expected dispute resolution count to be 0 after reset, got %d", snapshot2.DisputeResolutionCount)
}
}
func TestMetricsCollector_ConcurrentAccess(t *testing.T) {
m := NewMetricsCollector()
// Run 100 goroutines that increment metrics concurrently
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
m.IncrementPaymentCreated()
m.IncrementEscrowCreated()
m.RecordDisputeResolutionDuration(10 * time.Millisecond)
}()
}
wg.Wait()
snapshot := m.Snapshot()
if snapshot.PaymentCreated != 100 {
t.Errorf("expected 100 payments created, got %d", snapshot.PaymentCreated)
}
if snapshot.EscrowCreated != 100 {
t.Errorf("expected 100 escrows created, got %d", snapshot.EscrowCreated)
}
if snapshot.DisputeResolutionCount != 100 {
t.Errorf("expected 100 dispute resolutions, got %d", snapshot.DisputeResolutionCount)
}
}
func TestMetricsCollector_DisputeResolutionRingBuffer(t *testing.T) {
m := NewMetricsCollector()
// Record 150 dispute resolutions (more than the 100 buffer size)
for i := 0; i < 150; i++ {
m.RecordDisputeResolutionDuration(time.Duration(i+1) * time.Millisecond)
}
snapshot := m.Snapshot()
if snapshot.DisputeResolutionCount != 150 {
t.Errorf("expected 150 dispute resolutions, got %d", snapshot.DisputeResolutionCount)
}
// Total should be sum of 1+2+...+150 = 150*151/2 = 11325
expectedTotal := int64(11325)
if snapshot.DisputeResolutionTotalMs != expectedTotal {
t.Errorf("expected total %d ms, got %d ms", expectedTotal, snapshot.DisputeResolutionTotalMs)
}
// Average = 11325 / 150 = 75.5 = 75 (integer division)
expectedAvg := int64(75)
if snapshot.DisputeResolutionAvgMs != expectedAvg {
t.Errorf("expected avg %d ms, got %d ms", expectedAvg, snapshot.DisputeResolutionAvgMs)
}
}
func BenchmarkMetricsCollector_IncrementCounter(b *testing.B) {
m := NewMetricsCollector()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m.IncrementPaymentCreated()
}
}
func BenchmarkMetricsCollector_RecordDuration(b *testing.B) {
m := NewMetricsCollector()
duration := 100 * time.Millisecond
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m.RecordDisputeResolutionDuration(duration)
}
}
func BenchmarkMetricsCollector_Snapshot(b *testing.B) {
m := NewMetricsCollector()
// Set some metrics
m.IncrementPaymentCreated()
m.IncrementEscrowCreated()
m.RecordDisputeResolutionDuration(100 * time.Millisecond)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = m.Snapshot()
}
}
func BenchmarkMetricsCollector_ConcurrentIncrements(b *testing.B) {
m := NewMetricsCollector()
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
m.IncrementPaymentCreated()
}
})
}