github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/metrics_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kvledger 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/hyperledger/fabric-protos-go/common" 14 "github.com/hyperledger/fabric-protos-go/peer" 15 "github.com/hyperledger/fabric/bccsp/sw" 16 "github.com/hyperledger/fabric/common/ledger/testutil" 17 "github.com/hyperledger/fabric/common/metrics" 18 "github.com/hyperledger/fabric/common/metrics/metricsfakes" 19 lgr "github.com/hyperledger/fabric/core/ledger" 20 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr" 21 "github.com/hyperledger/fabric/core/ledger/mock" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestStatsBlockCommit(t *testing.T) { 26 conf, cleanup := testConfig(t) 27 defer cleanup() 28 testMetricProvider := testutilConstructMetricProvider() 29 30 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 31 assert.NoError(t, err) 32 provider, err := NewProvider( 33 &lgr.Initializer{ 34 DeployedChaincodeInfoProvider: &mock.DeployedChaincodeInfoProvider{}, 35 MetricsProvider: testMetricProvider.fakeProvider, 36 Config: conf, 37 Hasher: cryptoProvider, 38 }, 39 ) 40 if err != nil { 41 t.Fatalf("Failed to create new Provider: %s", err) 42 } 43 defer provider.Close() 44 45 // create a ledger 46 ledgerid := "ledger1" 47 _, gb := testutil.NewBlockGenerator(t, ledgerid, false) 48 l, err := provider.Create(gb) 49 assert.NoError(t, err) 50 ledger := l.(*kvLedger) 51 defer ledger.Close() 52 53 // calls during committing genesis block 54 assert.Equal(t, 55 []string{"channel", ledgerid}, 56 testMetricProvider.fakeBlockProcessingTimeHist.WithArgsForCall(0), 57 ) 58 assert.Equal(t, 59 []string{"channel", ledgerid}, 60 testMetricProvider.fakeBlockstorageCommitWithPvtDataTimeHist.WithArgsForCall(0), 61 ) 62 assert.Equal(t, 63 []string{"channel", ledgerid}, 64 testMetricProvider.fakeStatedbCommitTimeHist.WithArgsForCall(0), 65 ) 66 assert.Equal(t, 67 []string{ 68 "channel", ledgerid, 69 "transaction_type", common.HeaderType_CONFIG.String(), 70 "chaincode", "unknown", 71 "validation_code", peer.TxValidationCode_VALID.String(), 72 }, 73 testMetricProvider.fakeTransactionsCount.WithArgsForCall(0), 74 ) 75 76 // invoke updateBlockStats api explicitly and verify the calls with fake metrics 77 ledger.updateBlockStats( 78 1*time.Second, 2*time.Second, 3*time.Second, 79 []*txmgr.TxStatInfo{ 80 { 81 ValidationCode: peer.TxValidationCode_VALID, 82 TxType: common.HeaderType_ENDORSER_TRANSACTION, 83 ChaincodeID: &peer.ChaincodeID{Name: "mycc", Version: "1.0"}, 84 NumCollections: 2, 85 }, 86 { 87 ValidationCode: peer.TxValidationCode_INVALID_OTHER_REASON, 88 TxType: -1, 89 }, 90 }, 91 ) 92 assert.Equal(t, 93 []string{"channel", ledgerid}, 94 testMetricProvider.fakeBlockProcessingTimeHist.WithArgsForCall(1), 95 ) 96 assert.Equal(t, 97 float64(1), 98 testMetricProvider.fakeBlockProcessingTimeHist.ObserveArgsForCall(1), 99 ) 100 assert.Equal(t, 101 []string{"channel", ledgerid}, 102 testMetricProvider.fakeBlockstorageCommitWithPvtDataTimeHist.WithArgsForCall(1), 103 ) 104 assert.Equal(t, 105 float64(2), 106 testMetricProvider.fakeBlockstorageCommitWithPvtDataTimeHist.ObserveArgsForCall(1), 107 ) 108 assert.Equal(t, 109 []string{"channel", ledgerid}, 110 testMetricProvider.fakeStatedbCommitTimeHist.WithArgsForCall(1), 111 ) 112 assert.Equal(t, 113 float64(3), 114 testMetricProvider.fakeStatedbCommitTimeHist.ObserveArgsForCall(1), 115 ) 116 assert.Equal(t, 117 []string{ 118 "channel", ledgerid, 119 "transaction_type", common.HeaderType_ENDORSER_TRANSACTION.String(), 120 "chaincode", "mycc:1.0", 121 "validation_code", peer.TxValidationCode_VALID.String(), 122 }, 123 testMetricProvider.fakeTransactionsCount.WithArgsForCall(1), 124 ) 125 assert.Equal(t, 126 float64(1), 127 testMetricProvider.fakeTransactionsCount.AddArgsForCall(1), 128 ) 129 130 assert.Equal(t, 131 []string{ 132 "channel", ledgerid, 133 "transaction_type", "unknown", 134 "chaincode", "unknown", 135 "validation_code", peer.TxValidationCode_INVALID_OTHER_REASON.String(), 136 }, 137 testMetricProvider.fakeTransactionsCount.WithArgsForCall(2), 138 ) 139 assert.Equal(t, 140 float64(1), 141 testMetricProvider.fakeTransactionsCount.AddArgsForCall(2), 142 ) 143 } 144 145 type testMetricProvider struct { 146 fakeProvider *metricsfakes.Provider 147 fakeBlockProcessingTimeHist *metricsfakes.Histogram 148 fakeBlockstorageCommitWithPvtDataTimeHist *metricsfakes.Histogram 149 fakeStatedbCommitTimeHist *metricsfakes.Histogram 150 fakeTransactionsCount *metricsfakes.Counter 151 } 152 153 func testutilConstructMetricProvider() *testMetricProvider { 154 fakeProvider := &metricsfakes.Provider{} 155 fakeBlockProcessingTimeHist := testutilConstructHist() 156 fakeBlockstorageCommitWithPvtDataTimeHist := testutilConstructHist() 157 fakeStatedbCommitTimeHist := testutilConstructHist() 158 fakeTransactionsCount := testutilConstructCounter() 159 fakeProvider.NewGaugeStub = func(opts metrics.GaugeOpts) metrics.Gauge { 160 // return a gauge for metrics in common/ledger 161 return testutilConstructGauge() 162 } 163 fakeProvider.NewHistogramStub = func(opts metrics.HistogramOpts) metrics.Histogram { 164 switch opts.Name { 165 case blockProcessingTimeOpts.Name: 166 return fakeBlockProcessingTimeHist 167 case blockAndPvtdataStoreCommitTimeOpts.Name: 168 return fakeBlockstorageCommitWithPvtDataTimeHist 169 case statedbCommitTimeOpts.Name: 170 return fakeStatedbCommitTimeHist 171 default: 172 // return a histogram for metrics in common/ledger 173 return testutilConstructHist() 174 } 175 } 176 177 fakeProvider.NewCounterStub = func(opts metrics.CounterOpts) metrics.Counter { 178 switch opts.Name { 179 case transactionCountOpts.Name: 180 return fakeTransactionsCount 181 } 182 return nil 183 } 184 return &testMetricProvider{ 185 fakeProvider, 186 fakeBlockProcessingTimeHist, 187 fakeBlockstorageCommitWithPvtDataTimeHist, 188 fakeStatedbCommitTimeHist, 189 fakeTransactionsCount, 190 } 191 } 192 193 func testutilConstructGauge() *metricsfakes.Gauge { 194 fakeGauge := &metricsfakes.Gauge{} 195 fakeGauge.WithStub = func(lableValues ...string) metrics.Gauge { 196 return fakeGauge 197 } 198 return fakeGauge 199 } 200 201 func testutilConstructHist() *metricsfakes.Histogram { 202 fakeHist := &metricsfakes.Histogram{} 203 fakeHist.WithStub = func(lableValues ...string) metrics.Histogram { 204 return fakeHist 205 } 206 return fakeHist 207 } 208 209 func testutilConstructCounter() *metricsfakes.Counter { 210 fakeCounter := &metricsfakes.Counter{} 211 fakeCounter.WithStub = func(lableValues ...string) metrics.Counter { 212 return fakeCounter 213 } 214 return fakeCounter 215 }