github.com/lzy4123/fabric@v2.1.1+incompatible/gossip/metrics/mocks/metrics.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package mocks 8 9 import ( 10 "github.com/hyperledger/fabric/common/metrics" 11 "github.com/hyperledger/fabric/common/metrics/metricsfakes" 12 gmetrics "github.com/hyperledger/fabric/gossip/metrics" 13 ) 14 15 type TestMetricProvider struct { 16 FakeProvider *metricsfakes.Provider 17 18 FakeHeightGauge *metricsfakes.Gauge 19 FakeCommitDurationHist *metricsfakes.Histogram 20 FakePayloadBufferSizeGauge *metricsfakes.Gauge 21 22 FakeDeclarationGauge *metricsfakes.Gauge 23 24 FakeSentMessages *metricsfakes.Counter 25 FakeBufferOverflow *metricsfakes.Counter 26 FakeReceivedMessages *metricsfakes.Counter 27 28 FakeTotalGauge *metricsfakes.Gauge 29 30 FakeValidationDuration *metricsfakes.Histogram 31 FakeListMissingPrivateDataDuration *metricsfakes.Histogram 32 FakeFetchDuration *metricsfakes.Histogram 33 FakeCommitPrivateDataDuration *metricsfakes.Histogram 34 FakePurgeDuration *metricsfakes.Histogram 35 FakeSendDuration *metricsfakes.Histogram 36 FakeReconciliationDuration *metricsfakes.Histogram 37 FakePullDuration *metricsfakes.Histogram 38 FakeRetrieveDuration *metricsfakes.Histogram 39 } 40 41 func TestUtilConstructMetricProvider() *TestMetricProvider { 42 fakeProvider := &metricsfakes.Provider{} 43 44 fakeHeightGauge := testUtilConstructGauge() 45 fakeCommitDurationHist := testUtilConstructHist() 46 fakePayloadBufferSizeGauge := testUtilConstructGauge() 47 48 fakeDeclarationGauge := testUtilConstructGauge() 49 50 fakeSentMessages := testUtilConstructCounter() 51 fakeBufferOverflow := testUtilConstructCounter() 52 fakeReceivedMessages := testUtilConstructCounter() 53 54 fakeTotalGauge := testUtilConstructGauge() 55 56 fakeValidationDuration := testUtilConstructHist() 57 fakeListMissingPrivateDataDuration := testUtilConstructHist() 58 fakeFetchDuration := testUtilConstructHist() 59 fakeCommitPrivateDataDuration := testUtilConstructHist() 60 fakePurgeDuration := testUtilConstructHist() 61 fakeSendDuration := testUtilConstructHist() 62 fakeReconciliationDuration := testUtilConstructHist() 63 fakePullDuration := testUtilConstructHist() 64 fakeRetrieveDuration := testUtilConstructHist() 65 66 fakeProvider.NewCounterStub = func(opts metrics.CounterOpts) metrics.Counter { 67 switch opts.Name { 68 case gmetrics.BufferOverflowOpts.Name: 69 return fakeBufferOverflow 70 case gmetrics.SentMessagesOpts.Name: 71 return fakeSentMessages 72 case gmetrics.ReceivedMessagesOpts.Name: 73 return fakeReceivedMessages 74 } 75 return nil 76 } 77 fakeProvider.NewHistogramStub = func(opts metrics.HistogramOpts) metrics.Histogram { 78 switch opts.Name { 79 case gmetrics.CommitDurationOpts.Name: 80 return fakeCommitDurationHist 81 case gmetrics.ValidationDurationOpts.Name: 82 return fakeValidationDuration 83 case gmetrics.ListMissingPrivateDataDurationOpts.Name: 84 return fakeListMissingPrivateDataDuration 85 case gmetrics.FetchDurationOpts.Name: 86 return fakeFetchDuration 87 case gmetrics.CommitPrivateDataDurationOpts.Name: 88 return fakeCommitPrivateDataDuration 89 case gmetrics.PurgeDurationOpts.Name: 90 return fakePurgeDuration 91 case gmetrics.SendDurationOpts.Name: 92 return fakeSendDuration 93 case gmetrics.ReconciliationDurationOpts.Name: 94 return fakeReconciliationDuration 95 case gmetrics.PullDurationOpts.Name: 96 return fakePullDuration 97 case gmetrics.RetrieveDurationOpts.Name: 98 return fakeRetrieveDuration 99 } 100 return nil 101 } 102 fakeProvider.NewGaugeStub = func(opts metrics.GaugeOpts) metrics.Gauge { 103 switch opts.Name { 104 case gmetrics.PayloadBufferSizeOpts.Name: 105 return fakePayloadBufferSizeGauge 106 case gmetrics.HeightOpts.Name: 107 return fakeHeightGauge 108 case gmetrics.LeaderDeclerationOpts.Name: 109 return fakeDeclarationGauge 110 case gmetrics.TotalOpts.Name: 111 return fakeTotalGauge 112 } 113 return nil 114 } 115 116 return &TestMetricProvider{ 117 fakeProvider, 118 fakeHeightGauge, 119 fakeCommitDurationHist, 120 fakePayloadBufferSizeGauge, 121 fakeDeclarationGauge, 122 fakeSentMessages, 123 fakeBufferOverflow, 124 fakeReceivedMessages, 125 fakeTotalGauge, 126 fakeValidationDuration, 127 fakeListMissingPrivateDataDuration, 128 fakeFetchDuration, 129 fakeCommitPrivateDataDuration, 130 fakePurgeDuration, 131 fakeSendDuration, 132 fakeReconciliationDuration, 133 fakePullDuration, 134 fakeRetrieveDuration, 135 } 136 } 137 138 func testUtilConstructGauge() *metricsfakes.Gauge { 139 fakeGauge := &metricsfakes.Gauge{} 140 fakeGauge.WithReturns(fakeGauge) 141 return fakeGauge 142 } 143 144 func testUtilConstructHist() *metricsfakes.Histogram { 145 fakeHist := &metricsfakes.Histogram{} 146 fakeHist.WithReturns(fakeHist) 147 return fakeHist 148 } 149 150 func testUtilConstructCounter() *metricsfakes.Counter { 151 fakeCounter := &metricsfakes.Counter{} 152 fakeCounter.WithReturns(fakeCounter) 153 return fakeCounter 154 }