github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/perfschema/perfschema_test.go (about) 1 // Copyright 2016 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package perfschema_test 15 16 import ( 17 "database/sql" 18 "fmt" 19 "sync" 20 "testing" 21 22 . "github.com/insionng/yougam/libraries/pingcap/check" 23 "github.com/insionng/yougam/libraries/pingcap/tidb" 24 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 25 ) 26 27 func TestT(t *testing.T) { 28 TestingT(t) 29 } 30 31 type testPerfSchemaSuit struct { 32 vars map[string]interface{} 33 } 34 35 var _ = Suite(&testPerfSchemaSuit{ 36 vars: make(map[string]interface{}), 37 }) 38 39 func mustBegin(c *C, currDB *sql.DB) *sql.Tx { 40 tx, err := currDB.Begin() 41 c.Assert(err, IsNil) 42 return tx 43 } 44 45 func mustCommit(c *C, tx *sql.Tx) { 46 err := tx.Commit() 47 c.Assert(err, IsNil) 48 } 49 50 func mustExecuteSql(c *C, tx *sql.Tx, sql string) sql.Result { 51 r, err := tx.Exec(sql) 52 c.Assert(err, IsNil) 53 return r 54 } 55 56 func mustQuery(c *C, currDB *sql.DB, s string) int { 57 tx := mustBegin(c, currDB) 58 r, err := tx.Query(s) 59 c.Assert(err, IsNil) 60 cols, _ := r.Columns() 61 l := len(cols) 62 c.Assert(l, Greater, 0) 63 cnt := 0 64 res := make([]interface{}, l) 65 for i := 0; i < l; i++ { 66 res[i] = &sql.RawBytes{} 67 } 68 for r.Next() { 69 err := r.Scan(res...) 70 c.Assert(err, IsNil) 71 cnt++ 72 } 73 c.Assert(r.Err(), IsNil) 74 r.Close() 75 mustCommit(c, tx) 76 return cnt 77 } 78 79 func mustFailQuery(c *C, currDB *sql.DB, s string) { 80 rows, err := currDB.Query(s) 81 c.Assert(err, IsNil) 82 rows.Next() 83 c.Assert(rows.Err(), NotNil) 84 rows.Close() 85 } 86 87 func mustExec(c *C, currDB *sql.DB, sql string) sql.Result { 88 tx := mustBegin(c, currDB) 89 r := mustExecuteSql(c, tx, sql) 90 mustCommit(c, tx) 91 return r 92 } 93 94 func mustFailExec(c *C, currDB *sql.DB, sql string) { 95 tx := mustBegin(c, currDB) 96 _, err := tx.Exec(sql) 97 c.Assert(err, NotNil) 98 mustCommit(c, tx) 99 } 100 101 func checkResult(c *C, r sql.Result, affectedRows int64, insertID int64) { 102 gotRows, err := r.RowsAffected() 103 c.Assert(err, IsNil) 104 c.Assert(gotRows, Equals, affectedRows) 105 106 gotID, err := r.LastInsertId() 107 c.Assert(err, IsNil) 108 c.Assert(gotID, Equals, insertID) 109 } 110 111 func (p *testPerfSchemaSuit) TestInsert(c *C) { 112 defer testleak.AfterTest(c)() 113 testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/test/test") 114 c.Assert(err, IsNil) 115 defer testDB.Close() 116 117 r := mustExec(c, testDB, `insert into performance_schema.setup_actors values("localhost", "nieyy", "contributor", "NO", "NO");`) 118 checkResult(c, r, 0, 0) 119 cnt := mustQuery(c, testDB, "select * from performance_schema.setup_actors") 120 c.Assert(cnt, Equals, 2) 121 122 r = mustExec(c, testDB, `insert into performance_schema.setup_actors (host, user, role) values ("localhost", "lijian", "contributor");`) 123 checkResult(c, r, 0, 0) 124 cnt = mustQuery(c, testDB, "select * from performance_schema.setup_actors") 125 c.Assert(cnt, Equals, 3) 126 127 r = mustExec(c, testDB, `insert into performance_schema.setup_objects values("EVENT", "test", "%", "NO", "NO")`) 128 checkResult(c, r, 0, 0) 129 cnt = mustQuery(c, testDB, "select * from performance_schema.setup_objects") 130 c.Assert(cnt, Equals, 13) 131 132 r = mustExec(c, testDB, `insert into performance_schema.setup_objects (object_schema, object_name) values ("test1", "%")`) 133 checkResult(c, r, 0, 0) 134 cnt = mustQuery(c, testDB, "select * from performance_schema.setup_objects") 135 c.Assert(cnt, Equals, 14) 136 137 mustFailExec(c, testDB, `insert into performance_schema.setup_actors (host) values (null);`) 138 mustFailExec(c, testDB, `insert into performance_schema.setup_objects (object_type) values (null);`) 139 mustFailExec(c, testDB, `insert into performance_schema.setup_instruments values("select", "N/A", "N/A");`) 140 mustFailExec(c, testDB, `insert into performance_schema.setup_consumers values("events_stages_current", "N/A");`) 141 mustFailExec(c, testDB, `insert into performance_schema.setup_timers values("timer1", "XXXSECOND");`) 142 } 143 144 func (p *testPerfSchemaSuit) TestInstrument(c *C) { 145 defer testleak.AfterTest(c)() 146 testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/test/test") 147 c.Assert(err, IsNil) 148 defer testDB.Close() 149 150 cnt := mustQuery(c, testDB, "select * from performance_schema.setup_instruments") 151 c.Assert(cnt, Greater, 0) 152 153 mustExec(c, testDB, "show tables") 154 cnt = mustQuery(c, testDB, "select * from performance_schema.events_statements_current") 155 c.Assert(cnt, Equals, 1) 156 cnt = mustQuery(c, testDB, "select * from performance_schema.events_statements_history") 157 c.Assert(cnt, Greater, 0) 158 } 159 160 func (p *testPerfSchemaSuit) TestConcurrentStatement(c *C) { 161 defer testleak.AfterTest(c)() 162 testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/test/test") 163 c.Assert(err, IsNil) 164 defer testDB.Close() 165 166 mustExec(c, testDB, "create table test (a int, b int)") 167 168 var wg sync.WaitGroup 169 iFunc := func(a, b int) { 170 defer wg.Done() 171 mustExec(c, testDB, fmt.Sprintf(`INSERT INTO test VALUES (%d, %d);`, a, b)) 172 } 173 sFunc := func() { 174 defer wg.Done() 175 mustQuery(c, testDB, "select * from performance_schema.events_statements_current") 176 mustQuery(c, testDB, "select * from performance_schema.events_statements_history") 177 } 178 179 cnt := 10 180 for i := 0; i < cnt; i++ { 181 wg.Add(2) 182 go iFunc(i, i) 183 go sFunc() 184 } 185 wg.Wait() 186 }