github.com/MetalBlockchain/subnet-evm@v0.4.9/sync/handlers/stats/mock_stats.go (about)

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