|
| 1 | +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package statesynctest |
| 5 | + |
| 6 | +import ( |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ava-labs/avalanchego/vms/evm/sync/stats" |
| 11 | +) |
| 12 | + |
| 13 | +var _ stats.HandlerStats = (*TestHandlerStats)(nil) |
| 14 | + |
| 15 | +// TestHandlerStats is test for capturing and asserting on handler metrics in test |
| 16 | +type TestHandlerStats struct { |
| 17 | + lock sync.Mutex |
| 18 | + |
| 19 | + BlockRequestCount, |
| 20 | + MissingBlockHashCount, |
| 21 | + BlocksReturnedSum uint32 |
| 22 | + BlockRequestProcessingTimeSum time.Duration |
| 23 | + |
| 24 | + CodeRequestCount, |
| 25 | + MissingCodeHashCount, |
| 26 | + TooManyHashesRequested, |
| 27 | + DuplicateHashesRequested, |
| 28 | + CodeBytesReturnedSum uint32 |
| 29 | + CodeReadTimeSum time.Duration |
| 30 | + |
| 31 | + LeafsRequestCount, |
| 32 | + InvalidLeafsRequestCount, |
| 33 | + LeafsReturnedSum, |
| 34 | + MissingRootCount, |
| 35 | + TrieErrorCount, |
| 36 | + ProofErrorCount, |
| 37 | + SnapshotReadErrorCount, |
| 38 | + SnapshotReadAttemptCount, |
| 39 | + SnapshotReadSuccessCount, |
| 40 | + SnapshotSegmentValidCount, |
| 41 | + SnapshotSegmentInvalidCount uint32 |
| 42 | + ProofValsReturned int64 |
| 43 | + LeafsReadTime, |
| 44 | + SnapshotReadTime, |
| 45 | + GenerateRangeProofTime, |
| 46 | + LeafRequestProcessingTimeSum time.Duration |
| 47 | +} |
| 48 | + |
| 49 | +func (m *TestHandlerStats) Reset() { |
| 50 | + m.lock.Lock() |
| 51 | + defer m.lock.Unlock() |
| 52 | + m.BlockRequestCount = 0 |
| 53 | + m.MissingBlockHashCount = 0 |
| 54 | + m.BlocksReturnedSum = 0 |
| 55 | + m.BlockRequestProcessingTimeSum = 0 |
| 56 | + m.CodeRequestCount = 0 |
| 57 | + m.MissingCodeHashCount = 0 |
| 58 | + m.TooManyHashesRequested = 0 |
| 59 | + m.DuplicateHashesRequested = 0 |
| 60 | + m.CodeBytesReturnedSum = 0 |
| 61 | + m.CodeReadTimeSum = 0 |
| 62 | + m.LeafsRequestCount = 0 |
| 63 | + m.InvalidLeafsRequestCount = 0 |
| 64 | + m.LeafsReturnedSum = 0 |
| 65 | + m.MissingRootCount = 0 |
| 66 | + m.TrieErrorCount = 0 |
| 67 | + m.ProofErrorCount = 0 |
| 68 | + m.SnapshotReadErrorCount = 0 |
| 69 | + m.SnapshotReadAttemptCount = 0 |
| 70 | + m.SnapshotReadSuccessCount = 0 |
| 71 | + m.SnapshotSegmentValidCount = 0 |
| 72 | + m.SnapshotSegmentInvalidCount = 0 |
| 73 | + m.ProofValsReturned = 0 |
| 74 | + m.LeafsReadTime = 0 |
| 75 | + m.SnapshotReadTime = 0 |
| 76 | + m.GenerateRangeProofTime = 0 |
| 77 | + m.LeafRequestProcessingTimeSum = 0 |
| 78 | +} |
| 79 | + |
| 80 | +func (m *TestHandlerStats) IncBlockRequest() { |
| 81 | + m.lock.Lock() |
| 82 | + defer m.lock.Unlock() |
| 83 | + m.BlockRequestCount++ |
| 84 | +} |
| 85 | + |
| 86 | +func (m *TestHandlerStats) IncMissingBlockHash() { |
| 87 | + m.lock.Lock() |
| 88 | + defer m.lock.Unlock() |
| 89 | + m.MissingBlockHashCount++ |
| 90 | +} |
| 91 | + |
| 92 | +func (m *TestHandlerStats) UpdateBlocksReturned(num uint16) { |
| 93 | + m.lock.Lock() |
| 94 | + defer m.lock.Unlock() |
| 95 | + m.BlocksReturnedSum += uint32(num) |
| 96 | +} |
| 97 | + |
| 98 | +func (m *TestHandlerStats) UpdateBlockRequestProcessingTime(duration time.Duration) { |
| 99 | + m.lock.Lock() |
| 100 | + defer m.lock.Unlock() |
| 101 | + m.BlockRequestProcessingTimeSum += duration |
| 102 | +} |
| 103 | + |
| 104 | +func (m *TestHandlerStats) IncCodeRequest() { |
| 105 | + m.lock.Lock() |
| 106 | + defer m.lock.Unlock() |
| 107 | + m.CodeRequestCount++ |
| 108 | +} |
| 109 | + |
| 110 | +func (m *TestHandlerStats) IncMissingCodeHash() { |
| 111 | + m.lock.Lock() |
| 112 | + defer m.lock.Unlock() |
| 113 | + m.MissingCodeHashCount++ |
| 114 | +} |
| 115 | + |
| 116 | +func (m *TestHandlerStats) IncTooManyHashesRequested() { |
| 117 | + m.lock.Lock() |
| 118 | + defer m.lock.Unlock() |
| 119 | + m.TooManyHashesRequested++ |
| 120 | +} |
| 121 | + |
| 122 | +func (m *TestHandlerStats) IncDuplicateHashesRequested() { |
| 123 | + m.lock.Lock() |
| 124 | + defer m.lock.Unlock() |
| 125 | + m.DuplicateHashesRequested++ |
| 126 | +} |
| 127 | + |
| 128 | +func (m *TestHandlerStats) UpdateCodeReadTime(duration time.Duration) { |
| 129 | + m.lock.Lock() |
| 130 | + defer m.lock.Unlock() |
| 131 | + m.CodeReadTimeSum += duration |
| 132 | +} |
| 133 | + |
| 134 | +func (m *TestHandlerStats) UpdateCodeBytesReturned(bytes uint32) { |
| 135 | + m.lock.Lock() |
| 136 | + defer m.lock.Unlock() |
| 137 | + m.CodeBytesReturnedSum += bytes |
| 138 | +} |
| 139 | + |
| 140 | +func (m *TestHandlerStats) IncLeafsRequest() { |
| 141 | + m.lock.Lock() |
| 142 | + defer m.lock.Unlock() |
| 143 | + m.LeafsRequestCount++ |
| 144 | +} |
| 145 | + |
| 146 | +func (m *TestHandlerStats) IncInvalidLeafsRequest() { |
| 147 | + m.lock.Lock() |
| 148 | + defer m.lock.Unlock() |
| 149 | + m.InvalidLeafsRequestCount++ |
| 150 | +} |
| 151 | + |
| 152 | +func (m *TestHandlerStats) UpdateLeafsReturned(numLeafs uint16) { |
| 153 | + m.lock.Lock() |
| 154 | + defer m.lock.Unlock() |
| 155 | + m.LeafsReturnedSum += uint32(numLeafs) |
| 156 | +} |
| 157 | + |
| 158 | +func (m *TestHandlerStats) UpdateLeafsRequestProcessingTime(duration time.Duration) { |
| 159 | + m.lock.Lock() |
| 160 | + defer m.lock.Unlock() |
| 161 | + m.LeafRequestProcessingTimeSum += duration |
| 162 | +} |
| 163 | + |
| 164 | +func (m *TestHandlerStats) UpdateReadLeafsTime(duration time.Duration) { |
| 165 | + m.lock.Lock() |
| 166 | + defer m.lock.Unlock() |
| 167 | + m.LeafsReadTime += duration |
| 168 | +} |
| 169 | + |
| 170 | +func (m *TestHandlerStats) UpdateGenerateRangeProofTime(duration time.Duration) { |
| 171 | + m.lock.Lock() |
| 172 | + defer m.lock.Unlock() |
| 173 | + m.GenerateRangeProofTime += duration |
| 174 | +} |
| 175 | + |
| 176 | +func (m *TestHandlerStats) UpdateSnapshotReadTime(duration time.Duration) { |
| 177 | + m.lock.Lock() |
| 178 | + defer m.lock.Unlock() |
| 179 | + m.SnapshotReadTime += duration |
| 180 | +} |
| 181 | + |
| 182 | +func (m *TestHandlerStats) UpdateRangeProofValsReturned(numProofVals int64) { |
| 183 | + m.lock.Lock() |
| 184 | + defer m.lock.Unlock() |
| 185 | + m.ProofValsReturned += numProofVals |
| 186 | +} |
| 187 | + |
| 188 | +func (m *TestHandlerStats) IncMissingRoot() { |
| 189 | + m.lock.Lock() |
| 190 | + defer m.lock.Unlock() |
| 191 | + m.MissingRootCount++ |
| 192 | +} |
| 193 | + |
| 194 | +func (m *TestHandlerStats) IncTrieError() { |
| 195 | + m.lock.Lock() |
| 196 | + defer m.lock.Unlock() |
| 197 | + m.TrieErrorCount++ |
| 198 | +} |
| 199 | + |
| 200 | +func (m *TestHandlerStats) IncProofError() { |
| 201 | + m.lock.Lock() |
| 202 | + defer m.lock.Unlock() |
| 203 | + m.ProofErrorCount++ |
| 204 | +} |
| 205 | + |
| 206 | +func (m *TestHandlerStats) IncSnapshotReadError() { |
| 207 | + m.lock.Lock() |
| 208 | + defer m.lock.Unlock() |
| 209 | + m.SnapshotReadErrorCount++ |
| 210 | +} |
| 211 | + |
| 212 | +func (m *TestHandlerStats) IncSnapshotReadAttempt() { |
| 213 | + m.lock.Lock() |
| 214 | + defer m.lock.Unlock() |
| 215 | + m.SnapshotReadAttemptCount++ |
| 216 | +} |
| 217 | + |
| 218 | +func (m *TestHandlerStats) IncSnapshotReadSuccess() { |
| 219 | + m.lock.Lock() |
| 220 | + defer m.lock.Unlock() |
| 221 | + m.SnapshotReadSuccessCount++ |
| 222 | +} |
| 223 | + |
| 224 | +func (m *TestHandlerStats) IncSnapshotSegmentValid() { |
| 225 | + m.lock.Lock() |
| 226 | + defer m.lock.Unlock() |
| 227 | + m.SnapshotSegmentValidCount++ |
| 228 | +} |
| 229 | + |
| 230 | +func (m *TestHandlerStats) IncSnapshotSegmentInvalid() { |
| 231 | + m.lock.Lock() |
| 232 | + defer m.lock.Unlock() |
| 233 | + m.SnapshotSegmentInvalidCount++ |
| 234 | +} |
0 commit comments