Skip to content

Commit 104a75e

Browse files
committed
added few sorting options and results in output
1 parent fb876ac commit 104a75e

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

cmd/slowql-digest/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (a *app) digest(q query.Query, wg *sync.WaitGroup) error {
6565
cur.cumLockTime += time.Duration(q.LockTime)
6666
cur.cumRowsExamined += q.RowsExamined
6767
cur.cumRowsSent += q.RowsSent
68+
s.cumQueryTime += time.Duration(q.QueryTime)
6869

6970
// update the entry in the map
7071
a.res[s.hash] = cur
@@ -76,6 +77,7 @@ func (a *app) digest(q query.Query, wg *sync.WaitGroup) error {
7677
s.cumLockTime = time.Duration(q.LockTime)
7778
s.cumRowsExamined = q.RowsExamined
7879
s.cumRowsSent = q.RowsSent
80+
s.cumQueryTime = time.Duration(q.QueryTime)
7981

8082
// getting those values is done only once: same hash == same fingerprint & schema
8183
s.schema = q.Schema

cmd/slowql-digest/main.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,27 @@ type statistics struct {
5858
stddevTime time.Duration
5959
}
6060

61+
var orders = []string{"random", "calls", "bytes_sent", "query_time", "lock_time",
62+
"rows_sent", "rows_examined", "killed"}
63+
6164
func main() {
6265
var o options
6366
flag.StringVar(&o.logfile, "f", "", "Slow query log file to digest")
6467
flag.StringVar(&o.loglevel, "l", "info", "Log level")
6568
flag.StringVar(&o.kind, "k", "", "Database kind")
6669
flag.IntVar(&o.top, "top", 3, "Top queries to show")
67-
flag.StringVar(&o.order, "order", "random", "How to order queries")
70+
flag.StringVar(&o.order, "sort-by", "random", "How to sort queries. use ? to see all the available values")
6871
flag.BoolVar(&o.dec, "dec", false, "Sort by decreasing order")
6972
flag.Parse()
7073

74+
if o.order == "?" {
75+
fmt.Println("Available values:")
76+
for _, val := range orders {
77+
fmt.Printf(" %s\n", val)
78+
}
79+
return
80+
}
81+
7182
errs := o.parse()
7283
if len(errs) != 0 {
7384
flag.Usage()
@@ -105,6 +116,8 @@ func main() {
105116
var q query.Query
106117
var wg sync.WaitGroup
107118
a.p = slowql.NewParser(a.kind, a.fd)
119+
a.logger.Debug("slowql parser created")
120+
a.logger.Debug("query analysis started")
108121
start := time.Now()
109122
for {
110123
q = a.p.GetNext()
@@ -140,11 +153,11 @@ func main() {
140153
}
141154

142155
func showResults(res []statistics, order string, count int) {
143-
fmt.Printf("\nOrdered by: %s\n", Bold(order))
144-
156+
fmt.Printf("\nSorted by: %s\n", Bold(order))
157+
fmt.Printf("Showing top %d queries\n", Bold(count))
145158
for i := 0; i < len(res); i++ {
146159
if count == 0 {
147-
return
160+
break
148161
}
149162

150163
fmt.Printf(`
@@ -158,6 +171,7 @@ Cum Rows Examined: %d
158171
Cum Rows Sent: %d
159172
Cum Killed: %d
160173
Cum Lock Time: %s
174+
Cum Query Time: %s
161175
`,
162176
Bold(Underline("Query #")),
163177
Bold(Underline(i+1)),
@@ -170,10 +184,12 @@ Cum Lock Time: %s
170184
res[i].cumRowsSent,
171185
res[i].cumKilled,
172186
res[i].cumLockTime,
187+
res[i].cumQueryTime,
173188
)
174189

175190
count--
176191
}
192+
fmt.Println()
177193
}
178194

179195
func lineCounter(r io.Reader) (int, error) {
@@ -206,10 +222,30 @@ func sortResults(res map[string]statistics, order string, dec bool) ([]statistic
206222
sort.SliceStable(s, func(i, j int) bool {
207223
return s[i].calls < s[j].calls
208224
})
209-
case "bytes":
225+
case "bytes_sent":
210226
sort.SliceStable(s, func(i, j int) bool {
211227
return s[i].cumBytesSent < s[j].cumBytesSent
212228
})
229+
case "query_time":
230+
sort.SliceStable(s, func(i, j int) bool {
231+
return s[i].cumQueryTime < s[j].cumQueryTime
232+
})
233+
case "lock_time":
234+
sort.SliceStable(s, func(i, j int) bool {
235+
return s[i].cumLockTime < s[j].cumLockTime
236+
})
237+
case "rows_sent":
238+
sort.SliceStable(s, func(i, j int) bool {
239+
return s[i].cumRowsSent < s[j].cumRowsSent
240+
})
241+
case "rows_examined":
242+
sort.SliceStable(s, func(i, j int) bool {
243+
return s[i].cumRowsExamined < s[j].cumRowsExamined
244+
})
245+
case "killed":
246+
sort.SliceStable(s, func(i, j int) bool {
247+
return s[i].cumKilled < s[j].cumKilled
248+
})
213249
default:
214250
return nil, errors.New("unknown order, using 'random'")
215251
}

cmd/slowql-digest/options.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "errors"
44

55
func (o *options) parse() []error {
66
var errs []error
7-
orders := []string{"random", "calls", "bytes"}
87
if o.logfile == "" {
98
errs = append(errs, errors.New("no slow query log file provided"))
109
} else if o.kind == "" {

0 commit comments

Comments
 (0)