forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_analyzer.go
119 lines (107 loc) · 2.24 KB
/
query_analyzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"sort"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/exit"
"github.com/youtube/vitess/go/vt/sqlparser"
)
var (
ignores = [][]byte{
[]byte("#"),
[]byte("/*"),
[]byte("SET"),
[]byte("use"),
[]byte("BEGIN"),
[]byte("COMMIT"),
[]byte("ROLLBACK"),
}
bindIndex = 0
queries = make(map[string]int)
)
type stat struct {
Query string
Count int
}
type stats []stat
func (a stats) Len() int { return len(a) }
func (a stats) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a stats) Less(i, j int) bool { return a[i].Count > a[j].Count }
func main() {
defer exit.Recover()
flag.Parse()
for _, filename := range flag.Args() {
fmt.Printf("processing: %s\n", filename)
if err := processFile(filename); err != nil {
log.Errorf("processFile error: %v", err)
exit.Return(1)
}
}
var stats = make(stats, 0, 128)
for k, v := range queries {
stats = append(stats, stat{Query: k, Count: v})
}
sort.Sort(stats)
for _, s := range stats {
fmt.Printf("%d: %s\n", s.Count, s.Query)
}
}
func processFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
r := bufio.NewReader(f)
for {
line, err := r.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
analyze(line)
}
return nil
}
func analyze(line []byte) {
for _, ignore := range ignores {
if bytes.HasPrefix(line, ignore) {
return
}
}
dml := string(bytes.TrimRight(line, "\n"))
ast, err := sqlparser.Parse(dml)
if err != nil {
log.Errorf("Error parsing %s", dml)
return
}
bindIndex = 0
buf := sqlparser.NewTrackedBuffer(formatWithBind)
buf.Myprintf("%v", ast)
addQuery(buf.ParsedQuery().Query)
}
func formatWithBind(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {
switch node := node.(type) {
case sqlparser.StrVal, sqlparser.NumVal:
buf.WriteArg(fmt.Sprintf(":v%d", bindIndex))
bindIndex++
default:
node.Format(buf)
}
}
func addQuery(query string) {
count, ok := queries[query]
if !ok {
count = 0
}
queries[query] = count + 1
}