vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/queryz_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tabletserver 18 19 import ( 20 "fmt" 21 "io" 22 "net/http" 23 "net/http/httptest" 24 "regexp" 25 "strings" 26 "testing" 27 "time" 28 29 "vitess.io/vitess/go/vt/dbconfigs" 30 "vitess.io/vitess/go/vt/sqlparser" 31 "vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder" 32 "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" 33 ) 34 35 func TestQueryzHandler(t *testing.T) { 36 resp := httptest.NewRecorder() 37 req, _ := http.NewRequest("GET", "/schemaz", nil) 38 qe := newTestQueryEngine(10*time.Second, true, &dbconfigs.DBConfigs{}) 39 40 const query1 = "select name from test_table" 41 plan1 := &TabletPlan{ 42 Original: query1, 43 Plan: &planbuilder.Plan{ 44 Table: &schema.Table{Name: sqlparser.NewIdentifierCS("test_table")}, 45 PlanID: planbuilder.PlanSelect, 46 }, 47 } 48 plan1.AddStats(10, 2*time.Second, 1*time.Second, 0, 2, 0) 49 qe.plans.Set(query1, plan1) 50 51 const query2 = "insert into test_table values 1" 52 plan2 := &TabletPlan{ 53 Original: query2, 54 Plan: &planbuilder.Plan{ 55 Table: &schema.Table{Name: sqlparser.NewIdentifierCS("test_table")}, 56 PlanID: planbuilder.PlanDDL, 57 }, 58 } 59 plan2.AddStats(1, 2*time.Millisecond, 1*time.Millisecond, 1, 0, 0) 60 qe.plans.Set(query2, plan2) 61 62 const query3 = "show tables" 63 plan3 := &TabletPlan{ 64 Original: query3, 65 Plan: &planbuilder.Plan{ 66 Table: &schema.Table{Name: sqlparser.NewIdentifierCS("")}, 67 PlanID: planbuilder.PlanOtherRead, 68 }, 69 } 70 plan3.AddStats(1, 75*time.Millisecond, 50*time.Millisecond, 0, 1, 0) 71 qe.plans.Set(query3, plan3) 72 qe.plans.Set("", (*TabletPlan)(nil)) 73 74 hugeInsert := "insert into test_table values 0" 75 for i := 1; i < 1000; i++ { 76 hugeInsert = hugeInsert + fmt.Sprintf(", %d", i) 77 } 78 plan4 := &TabletPlan{ 79 Original: hugeInsert, 80 Plan: &planbuilder.Plan{ 81 Table: &schema.Table{Name: sqlparser.NewIdentifierCS("")}, 82 PlanID: planbuilder.PlanOtherRead, 83 }, 84 } 85 plan4.AddStats(1, 1*time.Millisecond, 1*time.Millisecond, 1, 0, 0) 86 qe.plans.Set(hugeInsert, plan4) 87 qe.plans.Set("", (*TabletPlan)(nil)) 88 89 // Wait for cache to settle 90 qe.plans.Wait() 91 92 queryzHandler(qe, resp, req) 93 body, _ := io.ReadAll(resp.Body) 94 planPattern1 := []string{ 95 `<tr class="high">`, 96 `<td>select name from test_table</td>`, 97 `<td>test_table</td>`, 98 `<td>Select</td>`, 99 `<td>10</td>`, 100 `<td>2.000000</td>`, 101 `<td>1.000000</td>`, 102 `<td>0</td>`, 103 `<td>2</td>`, 104 `<td>0</td>`, 105 `<td>0.200000</td>`, 106 `<td>0.100000</td>`, 107 `<td>0.000000</td>`, 108 `<td>0.200000</td>`, 109 `<td>0.000000</td>`, 110 } 111 checkQueryzHasPlan(t, planPattern1, plan1, body) 112 planPattern2 := []string{ 113 `<tr class="low">`, 114 `<td>insert into test_table values 1</td>`, 115 `<td>test_table</td>`, 116 `<td>DDL</td>`, 117 `<td>1</td>`, 118 `<td>0.002000</td>`, 119 `<td>0.001000</td>`, 120 `<td>1</td>`, 121 `<td>0</td>`, 122 `<td>0</td>`, 123 `<td>0.002000</td>`, 124 `<td>0.001000</td>`, 125 `<td>1.000000</td>`, 126 `<td>0.000000</td>`, 127 `<td>0.000000</td>`, 128 } 129 checkQueryzHasPlan(t, planPattern2, plan2, body) 130 planPattern3 := []string{ 131 `<tr class="medium">`, 132 `<td>show tables</td>`, 133 `<td></td>`, 134 `<td>OtherRead</td>`, 135 `<td>1</td>`, 136 `<td>0.075000</td>`, 137 `<td>0.050000</td>`, 138 `<td>0</td>`, 139 `<td>1</td>`, 140 `<td>0</td>`, 141 `<td>0.075000</td>`, 142 `<td>0.050000</td>`, 143 `<td>0.000000</td>`, 144 `<td>1.000000</td>`, 145 `<td>0.000000</td>`, 146 } 147 checkQueryzHasPlan(t, planPattern3, plan3, body) 148 planPattern4 := []string{ 149 `<tr class="low">`, 150 `<td>insert into test_table values .* \[TRUNCATED\][^<]*</td>`, 151 `<td></td>`, 152 `<td>OtherRead</td>`, 153 `<td>1</td>`, 154 `<td>0.001000</td>`, 155 `<td>0.001000</td>`, 156 `<td>1</td>`, 157 `<td>0</td>`, 158 `<td>0</td>`, 159 `<td>0.001000</td>`, 160 `<td>0.001000</td>`, 161 `<td>1.000000</td>`, 162 `<td>0.000000</td>`, 163 `<td>0.000000</td>`, 164 } 165 checkQueryzHasPlan(t, planPattern4, plan4, body) 166 } 167 168 func checkQueryzHasPlan(t *testing.T, planPattern []string, plan *TabletPlan, page []byte) { 169 matcher := regexp.MustCompile(strings.Join(planPattern, `\s*`)) 170 if !matcher.Match(page) { 171 t.Fatalf("queryz page does not contain\nplan:\n%#v\npattern:\n%v\npage:\n%s", plan, strings.Join(planPattern, `\s*`), string(page)) 172 } 173 }