github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/topn_slow_query_test.go (about) 1 // Copyright 2020 WHTCORPS INC, 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 petri 15 16 import ( 17 "time" 18 19 . "github.com/whtcorpsinc/check" 20 ) 21 22 var _ = Suite(&testTopNSlowQuerySuite{}) 23 24 type testTopNSlowQuerySuite struct{} 25 26 func (t *testTopNSlowQuerySuite) TestPush(c *C) { 27 slowQuery := newTopNSlowQueries(10, 0, 10) 28 // Insert data into the heap. 29 slowQuery.Append(&SlowQueryInfo{Duration: 300 * time.Millisecond}) 30 slowQuery.Append(&SlowQueryInfo{Duration: 400 * time.Millisecond}) 31 slowQuery.Append(&SlowQueryInfo{Duration: 500 * time.Millisecond}) 32 slowQuery.Append(&SlowQueryInfo{Duration: 600 * time.Millisecond}) 33 slowQuery.Append(&SlowQueryInfo{Duration: 700 * time.Millisecond}) 34 slowQuery.Append(&SlowQueryInfo{Duration: 800 * time.Millisecond}) 35 slowQuery.Append(&SlowQueryInfo{Duration: 900 * time.Millisecond}) 36 slowQuery.Append(&SlowQueryInfo{Duration: 1000 * time.Millisecond}) 37 slowQuery.Append(&SlowQueryInfo{Duration: 1100 * time.Millisecond}) 38 slowQuery.Append(&SlowQueryInfo{Duration: 1200 * time.Millisecond}) 39 c.Assert(slowQuery.user.data[0].Duration, Equals, 300*time.Millisecond) 40 checkHeap(&slowQuery.user, c) 41 42 // UFIDelate all data in the heap. 43 slowQuery.Append(&SlowQueryInfo{Duration: 1300 * time.Millisecond}) 44 c.Assert(slowQuery.user.data[0].Duration, Equals, 400*time.Millisecond) 45 slowQuery.Append(&SlowQueryInfo{Duration: 1400 * time.Millisecond}) 46 c.Assert(slowQuery.user.data[0].Duration, Equals, 500*time.Millisecond) 47 slowQuery.Append(&SlowQueryInfo{Duration: 1500 * time.Millisecond}) 48 c.Assert(slowQuery.user.data[0].Duration, Equals, 600*time.Millisecond) 49 slowQuery.Append(&SlowQueryInfo{Duration: 1500 * time.Millisecond}) 50 c.Assert(slowQuery.user.data[0].Duration, Equals, 700*time.Millisecond) 51 slowQuery.Append(&SlowQueryInfo{Duration: 1600 * time.Millisecond}) 52 c.Assert(slowQuery.user.data[0].Duration, Equals, 800*time.Millisecond) 53 slowQuery.Append(&SlowQueryInfo{Duration: 1700 * time.Millisecond}) 54 c.Assert(slowQuery.user.data[0].Duration, Equals, 900*time.Millisecond) 55 slowQuery.Append(&SlowQueryInfo{Duration: 1800 * time.Millisecond}) 56 c.Assert(slowQuery.user.data[0].Duration, Equals, 1000*time.Millisecond) 57 slowQuery.Append(&SlowQueryInfo{Duration: 1900 * time.Millisecond}) 58 c.Assert(slowQuery.user.data[0].Duration, Equals, 1100*time.Millisecond) 59 slowQuery.Append(&SlowQueryInfo{Duration: 2000 * time.Millisecond}) 60 c.Assert(slowQuery.user.data[0].Duration, Equals, 1200*time.Millisecond) 61 slowQuery.Append(&SlowQueryInfo{Duration: 2100 * time.Millisecond}) 62 c.Assert(slowQuery.user.data[0].Duration, Equals, 1300*time.Millisecond) 63 checkHeap(&slowQuery.user, c) 64 65 // Data smaller than heap top will not be inserted. 66 slowQuery.Append(&SlowQueryInfo{Duration: 1200 * time.Millisecond}) 67 c.Assert(slowQuery.user.data[0].Duration, Equals, 1300*time.Millisecond) 68 slowQuery.Append(&SlowQueryInfo{Duration: 666 * time.Millisecond}) 69 c.Assert(slowQuery.user.data[0].Duration, Equals, 1300*time.Millisecond) 70 } 71 72 func (t *testTopNSlowQuerySuite) TestRemoveExpired(c *C) { 73 now := time.Now() 74 slowQuery := newTopNSlowQueries(6, 3*time.Second, 10) 75 76 slowQuery.Append(&SlowQueryInfo{Start: now, Duration: 6}) 77 slowQuery.Append(&SlowQueryInfo{Start: now.Add(1 * time.Second), Duration: 5}) 78 slowQuery.Append(&SlowQueryInfo{Start: now.Add(2 * time.Second), Duration: 4}) 79 slowQuery.Append(&SlowQueryInfo{Start: now.Add(3 * time.Second), Duration: 3}) 80 slowQuery.Append(&SlowQueryInfo{Start: now.Add(4 * time.Second), Duration: 2}) 81 c.Assert(slowQuery.user.data[0].Duration, Equals, 2*time.Nanosecond) 82 83 slowQuery.RemoveExpired(now.Add(5 * time.Second)) 84 c.Assert(len(slowQuery.user.data), Equals, 2) 85 c.Assert(slowQuery.user.data[0].Duration, Equals, 2*time.Nanosecond) 86 87 slowQuery.Append(&SlowQueryInfo{Start: now.Add(3 * time.Second), Duration: 3}) 88 slowQuery.Append(&SlowQueryInfo{Start: now.Add(4 * time.Second), Duration: 2}) 89 slowQuery.Append(&SlowQueryInfo{Start: now.Add(5 * time.Second), Duration: 1}) 90 slowQuery.Append(&SlowQueryInfo{Start: now.Add(6 * time.Second), Duration: 0}) 91 c.Assert(len(slowQuery.user.data), Equals, 6) 92 c.Assert(slowQuery.user.data[0].Duration, Equals, 0*time.Nanosecond) 93 94 slowQuery.RemoveExpired(now.Add(6 * time.Second)) 95 c.Assert(len(slowQuery.user.data), Equals, 4) 96 c.Assert(slowQuery.user.data[0].Duration, Equals, 0*time.Nanosecond) 97 } 98 99 func checkHeap(q *slowQueryHeap, c *C) { 100 for i := 0; i < len(q.data); i++ { 101 left := 2*i + 1 102 right := 2*i + 2 103 if left < len(q.data) { 104 c.Assert(q.data[i].Duration, LessEqual, q.data[left].Duration) 105 } 106 if right < len(q.data) { 107 c.Assert(q.data[i].Duration, LessEqual, q.data[right].Duration) 108 } 109 } 110 } 111 112 func (t *testTopNSlowQuerySuite) TestQueue(c *C) { 113 q := newTopNSlowQueries(10, time.Minute, 5) 114 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "aaa"}) 115 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "bbb"}) 116 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "ccc"}) 117 118 query := q.recent.Query(1) 119 c.Assert(query[0].ALLEGROALLEGROSQL, Equals, "ccc") 120 query = q.recent.Query(2) 121 c.Assert(query[0].ALLEGROALLEGROSQL, Equals, "ccc") 122 c.Assert(query[1].ALLEGROALLEGROSQL, Equals, "bbb") 123 query = q.recent.Query(6) 124 c.Assert(query[0].ALLEGROALLEGROSQL, Equals, "ccc") 125 c.Assert(query[1].ALLEGROALLEGROSQL, Equals, "bbb") 126 c.Assert(query[2].ALLEGROALLEGROSQL, Equals, "aaa") 127 128 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "ddd"}) 129 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "eee"}) 130 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "fff"}) 131 q.Append(&SlowQueryInfo{ALLEGROALLEGROSQL: "ggg"}) 132 133 query = q.recent.Query(3) 134 c.Assert(query[0].ALLEGROALLEGROSQL, Equals, "ggg") 135 c.Assert(query[1].ALLEGROALLEGROSQL, Equals, "fff") 136 c.Assert(query[2].ALLEGROALLEGROSQL, Equals, "eee") 137 query = q.recent.Query(6) 138 c.Assert(query[0].ALLEGROALLEGROSQL, Equals, "ggg") 139 c.Assert(query[1].ALLEGROALLEGROSQL, Equals, "fff") 140 c.Assert(query[2].ALLEGROALLEGROSQL, Equals, "eee") 141 c.Assert(query[3].ALLEGROALLEGROSQL, Equals, "ddd") 142 c.Assert(query[4].ALLEGROALLEGROSQL, Equals, "ccc") 143 }