github.com/adecaro/fabric-ca@v2.0.0-alpha+incompatible/lib/server/db/metrics_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package db_test 8 9 import ( 10 "github.com/hyperledger/fabric-ca/lib/server/db" 11 "github.com/hyperledger/fabric-ca/lib/server/db/mocks" 12 "github.com/hyperledger/fabric/common/metrics/metricsfakes" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("metrics", func() { 18 var ( 19 fakeAPICounter *metricsfakes.Counter 20 fakeAPIHistogram *metricsfakes.Histogram 21 testDB *db.DB 22 ) 23 24 BeforeEach(func() { 25 fakeAPICounter = &metricsfakes.Counter{} 26 fakeAPICounter.WithReturns(fakeAPICounter) 27 28 fakeAPIHistogram = &metricsfakes.Histogram{} 29 fakeAPIHistogram.WithReturns(fakeAPIHistogram) 30 31 testDB = &db.DB{ 32 DB: &mocks.SqlxDB{}, 33 Metrics: db.Metrics{ 34 APICounter: fakeAPICounter, 35 APIDuration: fakeAPIHistogram, 36 }, 37 CAName: "testCA", 38 } 39 }) 40 41 Context("DB", func() { 42 It("records metrics", func() { 43 By("recoring count and duration metrics for calls to Select database API", func() { 44 testDB.Select("selectFunc", nil, "") 45 Expect(fakeAPICounter.AddCallCount()).To(Equal(1)) 46 Expect(fakeAPICounter.WithArgsForCall(0)).NotTo(BeZero()) 47 Expect(fakeAPICounter.WithArgsForCall(0)).To(Equal([]string{"ca_name", "testCA", "func_name", "selectFunc", "dbapi_name", "Select"})) 48 49 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(1)) 50 Expect(fakeAPIHistogram.WithArgsForCall(0)).NotTo(BeZero()) 51 Expect(fakeAPIHistogram.WithArgsForCall(0)).To(Equal([]string{"ca_name", "testCA", "func_name", "selectFunc", "dbapi_name", "Select"})) 52 }) 53 54 By("counting number of calls to the Exec database API", func() { 55 testDB.Exec("execFunc", "") 56 Expect(fakeAPICounter.AddCallCount()).To(Equal(2)) 57 Expect(fakeAPICounter.WithArgsForCall(1)).NotTo(BeZero()) 58 Expect(fakeAPICounter.WithArgsForCall(1)).To(Equal([]string{"ca_name", "testCA", "func_name", "execFunc", "dbapi_name", "Exec"})) 59 60 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(2)) 61 Expect(fakeAPIHistogram.WithArgsForCall(1)).NotTo(BeZero()) 62 Expect(fakeAPIHistogram.WithArgsForCall(1)).To(Equal([]string{"ca_name", "testCA", "func_name", "execFunc", "dbapi_name", "Exec"})) 63 }) 64 65 By("counting number of calls to the NamedExec database API", func() { 66 testDB.NamedExec("namedExecFunc", "", nil) 67 Expect(fakeAPICounter.AddCallCount()).To(Equal(3)) 68 Expect(fakeAPICounter.WithArgsForCall(2)).NotTo(BeZero()) 69 Expect(fakeAPICounter.WithArgsForCall(2)).To(Equal([]string{"ca_name", "testCA", "func_name", "namedExecFunc", "dbapi_name", "NamedExec"})) 70 71 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(3)) 72 Expect(fakeAPIHistogram.WithArgsForCall(2)).NotTo(BeZero()) 73 Expect(fakeAPIHistogram.WithArgsForCall(2)).To(Equal([]string{"ca_name", "testCA", "func_name", "namedExecFunc", "dbapi_name", "NamedExec"})) 74 }) 75 76 By("counting number of calls to the Get database API", func() { 77 testDB.Get("getFunc", nil, "") 78 Expect(fakeAPICounter.AddCallCount()).To(Equal(4)) 79 Expect(fakeAPICounter.WithArgsForCall(3)).NotTo(BeZero()) 80 Expect(fakeAPICounter.WithArgsForCall(3)).To(Equal([]string{"ca_name", "testCA", "func_name", "getFunc", "dbapi_name", "Get"})) 81 82 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(4)) 83 Expect(fakeAPIHistogram.WithArgsForCall(3)).NotTo(BeZero()) 84 Expect(fakeAPIHistogram.WithArgsForCall(3)).To(Equal([]string{"ca_name", "testCA", "func_name", "getFunc", "dbapi_name", "Get"})) 85 }) 86 87 By("counting number of calls to the Queryx database API", func() { 88 testDB.Queryx("queryxFunc", "", nil) 89 Expect(fakeAPICounter.AddCallCount()).To(Equal(5)) 90 Expect(fakeAPICounter.WithArgsForCall(4)).NotTo(BeZero()) 91 Expect(fakeAPICounter.WithArgsForCall(4)).To(Equal([]string{"ca_name", "testCA", "func_name", "queryxFunc", "dbapi_name", "Queryx"})) 92 93 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(5)) 94 Expect(fakeAPIHistogram.WithArgsForCall(4)).NotTo(BeZero()) 95 Expect(fakeAPIHistogram.WithArgsForCall(4)).To(Equal([]string{"ca_name", "testCA", "func_name", "queryxFunc", "dbapi_name", "Queryx"})) 96 }) 97 }) 98 }) 99 100 Context("TX", func() { 101 var fabTx *db.TX 102 103 BeforeEach(func() { 104 fabTx = &db.TX{ 105 TX: &mocks.SqlxTx{}, 106 Record: testDB, 107 } 108 }) 109 110 It("records metrics", func() { 111 By("recording count and duration metrics for calls to Select database API", func() { 112 fabTx.Select("selectFunc", nil, "") 113 Expect(fakeAPICounter.AddCallCount()).To(Equal(1)) 114 Expect(fakeAPICounter.WithArgsForCall(0)).NotTo(BeZero()) 115 Expect(fakeAPICounter.WithArgsForCall(0)).To(Equal([]string{"ca_name", "testCA", "func_name", "selectFunc", "dbapi_name", "Select"})) 116 117 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(1)) 118 Expect(fakeAPIHistogram.WithArgsForCall(0)).NotTo(BeZero()) 119 Expect(fakeAPIHistogram.WithArgsForCall(0)).To(Equal([]string{"ca_name", "testCA", "func_name", "selectFunc", "dbapi_name", "Select"})) 120 }) 121 122 By("recording count and duration metrics for calls to Exec database API", func() { 123 fabTx.Exec("execFunc", "") 124 Expect(fakeAPICounter.AddCallCount()).To(Equal(2)) 125 Expect(fakeAPICounter.WithArgsForCall(1)).NotTo(BeZero()) 126 Expect(fakeAPICounter.WithArgsForCall(1)).To(Equal([]string{"ca_name", "testCA", "func_name", "execFunc", "dbapi_name", "Exec"})) 127 128 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(2)) 129 Expect(fakeAPIHistogram.WithArgsForCall(1)).NotTo(BeZero()) 130 Expect(fakeAPIHistogram.WithArgsForCall(1)).To(Equal([]string{"ca_name", "testCA", "func_name", "execFunc", "dbapi_name", "Exec"})) 131 }) 132 133 By("recording count and duration metrics for calls to Get database API", func() { 134 fabTx.Get("getFunc", nil, "") 135 Expect(fakeAPICounter.AddCallCount()).To(Equal(3)) 136 Expect(fakeAPICounter.WithArgsForCall(2)).NotTo(BeZero()) 137 Expect(fakeAPICounter.WithArgsForCall(2)).To(Equal([]string{"ca_name", "testCA", "func_name", "getFunc", "dbapi_name", "Get"})) 138 139 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(3)) 140 Expect(fakeAPIHistogram.WithArgsForCall(2)).NotTo(BeZero()) 141 Expect(fakeAPIHistogram.WithArgsForCall(2)).To(Equal([]string{"ca_name", "testCA", "func_name", "getFunc", "dbapi_name", "Get"})) 142 }) 143 144 By("recording count and duration metrics for calls to Queryx database API", func() { 145 fabTx.Queryx("queryxFunc", "", nil) 146 Expect(fakeAPICounter.AddCallCount()).To(Equal(4)) 147 Expect(fakeAPICounter.WithArgsForCall(3)).NotTo(BeZero()) 148 Expect(fakeAPICounter.WithArgsForCall(3)).To(Equal([]string{"ca_name", "testCA", "func_name", "queryxFunc", "dbapi_name", "Queryx"})) 149 150 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(4)) 151 Expect(fakeAPIHistogram.WithArgsForCall(3)).NotTo(BeZero()) 152 Expect(fakeAPIHistogram.WithArgsForCall(3)).To(Equal([]string{"ca_name", "testCA", "func_name", "queryxFunc", "dbapi_name", "Queryx"})) 153 }) 154 155 By("recording count and duration metrics for calls to Commit database API", func() { 156 fabTx.Commit("commitFunc") 157 Expect(fakeAPICounter.AddCallCount()).To(Equal(5)) 158 Expect(fakeAPICounter.WithArgsForCall(4)).NotTo(BeZero()) 159 Expect(fakeAPICounter.WithArgsForCall(4)).To(Equal([]string{"ca_name", "testCA", "func_name", "commitFunc", "dbapi_name", "Commit"})) 160 161 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(5)) 162 Expect(fakeAPIHistogram.WithArgsForCall(4)).NotTo(BeZero()) 163 Expect(fakeAPIHistogram.WithArgsForCall(4)).To(Equal([]string{"ca_name", "testCA", "func_name", "commitFunc", "dbapi_name", "Commit"})) 164 }) 165 166 By("recording count and duration metrics for calls to Rollback database API", func() { 167 fabTx.Rollback("rollbackFunc") 168 Expect(fakeAPICounter.AddCallCount()).To(Equal(6)) 169 Expect(fakeAPICounter.WithArgsForCall(5)).NotTo(BeZero()) 170 Expect(fakeAPICounter.WithArgsForCall(5)).To(Equal([]string{"ca_name", "testCA", "func_name", "rollbackFunc", "dbapi_name", "Rollback"})) 171 172 Expect(fakeAPIHistogram.ObserveCallCount()).To(Equal(6)) 173 Expect(fakeAPIHistogram.WithArgsForCall(5)).NotTo(BeZero()) 174 Expect(fakeAPIHistogram.WithArgsForCall(5)).To(Equal([]string{"ca_name", "testCA", "func_name", "rollbackFunc", "dbapi_name", "Rollback"})) 175 }) 176 }) 177 }) 178 })