github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/metric_util_test.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // Functions used for testing metrics. 12 13 package sql_test 14 15 import ( 16 "github.com/cockroachdb/cockroach/pkg/sql" 17 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 18 "github.com/cockroachdb/cockroach/pkg/util/metric" 19 "github.com/cockroachdb/errors" 20 ) 21 22 // initializeQueryCounter returns a queryCounter that accounts for system 23 // migrations that may have run DDL statements. 24 func initializeQueryCounter(s serverutils.TestServerInterface) queryCounter { 25 return queryCounter{ 26 txnBeginCount: s.MustGetSQLCounter(sql.MetaTxnBeginStarted.Name), 27 selectCount: s.MustGetSQLCounter(sql.MetaSelectStarted.Name), 28 selectExecutedCount: s.MustGetSQLCounter(sql.MetaSelectExecuted.Name), 29 distSQLSelectCount: s.MustGetSQLCounter(sql.MetaDistSQLSelect.Name), 30 updateCount: s.MustGetSQLCounter(sql.MetaUpdateStarted.Name), 31 insertCount: s.MustGetSQLCounter(sql.MetaInsertStarted.Name), 32 deleteCount: s.MustGetSQLCounter(sql.MetaDeleteStarted.Name), 33 ddlCount: s.MustGetSQLCounter(sql.MetaDdlStarted.Name), 34 miscCount: s.MustGetSQLCounter(sql.MetaMiscStarted.Name), 35 miscExecutedCount: s.MustGetSQLCounter(sql.MetaMiscExecuted.Name), 36 txnCommitCount: s.MustGetSQLCounter(sql.MetaTxnCommitStarted.Name), 37 txnRollbackCount: s.MustGetSQLCounter(sql.MetaTxnRollbackStarted.Name), 38 txnAbortCount: s.MustGetSQLCounter(sql.MetaTxnAbort.Name), 39 savepointCount: s.MustGetSQLCounter(sql.MetaSavepointStarted.Name), 40 restartSavepointCount: s.MustGetSQLCounter(sql.MetaRestartSavepointStarted.Name), 41 releaseRestartSavepointCount: s.MustGetSQLCounter(sql.MetaReleaseRestartSavepointStarted.Name), 42 rollbackToRestartSavepointCount: s.MustGetSQLCounter(sql.MetaRollbackToRestartSavepointStarted.Name), 43 } 44 } 45 46 func checkCounterDelta( 47 s serverutils.TestServerInterface, meta metric.Metadata, init, delta int64, 48 ) (int64, error) { 49 actual := s.MustGetSQLCounter(meta.Name) 50 if actual != (init + delta) { 51 return actual, errors.Errorf("query %s: actual %d != (init %d + delta %d)", 52 meta.Name, actual, init, delta) 53 } 54 return actual, nil 55 } 56 57 func checkCounterGE(s serverutils.TestServerInterface, meta metric.Metadata, e int64) error { 58 if a := s.MustGetSQLCounter(meta.Name); a < e { 59 return errors.Errorf("stat %s: expected: actual %d >= %d", 60 meta.Name, a, e) 61 } 62 return nil 63 }