Skip to content

Commit

Permalink
Go 版で -dump オプションに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Jul 28, 2015
1 parent 1c9cf8c commit 9009c79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
35 changes: 18 additions & 17 deletions myprofiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os/user"
"regexp"
"sort"
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -32,17 +31,16 @@ func (p *NormalizePattern) Normalize(q string) string {
return p.re.ReplaceAllString(q, p.subs)
}

var (
normalizePatterns []NormalizePattern = []NormalizePattern{
NormalizePattern{regexp.MustCompile(`[+\-]{0,1}\b\d+\b`), "N"},
NormalizePattern{regexp.MustCompile(`\b0x[0-9A-Fa-f]+\b`), "0xN"},
NormalizePattern{regexp.MustCompile(`(\\')`), ""},
NormalizePattern{regexp.MustCompile(`(\\")`), ""},
NormalizePattern{regexp.MustCompile(`'[^']+'`), "S"},
NormalizePattern{regexp.MustCompile(`"[^"]+"`), "S"},
NormalizePattern{regexp.MustCompile(`(([NS]\s*,\s*){4,})`), "..."},
}
)
var normalizePatterns = []NormalizePattern{
NormalizePattern{regexp.MustCompile(` +`), " "},
NormalizePattern{regexp.MustCompile(`[+\-]{0,1}\b\d+\b`), "N"},
NormalizePattern{regexp.MustCompile(`\b0x[0-9A-Fa-f]+\b`), "0xN"},
NormalizePattern{regexp.MustCompile(`(\\')`), ""},
NormalizePattern{regexp.MustCompile(`(\\")`), ""},
NormalizePattern{regexp.MustCompile(`'[^']+'`), "S"},
NormalizePattern{regexp.MustCompile(`"[^"]+"`), "S"},
NormalizePattern{regexp.MustCompile(`(([NS]\s*,\s*){4,})`), "..."},
}

func processList(db *sql.DB) []string {
procList := "SHOW FULL PROCESSLIST"
Expand All @@ -68,8 +66,6 @@ func processList(db *sql.DB) []string {
}

func normalizeQuery(query string) string {
parts := strings.Split(query, " ")
query = strings.Join(parts, " ")
for _, pat := range normalizePatterns {
query = pat.Normalize(query)
}
Expand All @@ -95,7 +91,7 @@ func (pl pairList) Swap(i, j int) {
}

func showSummary(sum map[string]int64, n int) {
counts := []pair{}
counts := make([]pair, 0, len(sum))
for q, c := range sum {
counts = append(counts, pair{q, c})
}
Expand All @@ -114,9 +110,14 @@ func profile(db *sql.DB, cfg *Config) {
count := make(map[string]int64)
for {
queries := processList(db)
if cfg.dump != nil {
for _, q := range queries {
cfg.dump.Write([]byte(q))
cfg.dump.Write([]byte{'\n'})
}
}
for _, q := range queries {
q = normalizeQuery(q)
count[q]++
count[normalizeQuery(q)]++
}
showSummary(count, cfg.numSummary)
time.Sleep(time.Duration(float64(time.Second) * cfg.interval))
Expand Down
20 changes: 20 additions & 0 deletions myprofiler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"testing"
)

func TestNormalize(t *testing.T) {
var data = []struct{ input, expected string }{
{"IN ('a', 'b', 'c')", "IN (S, S, S)"},
{"IN ('a', 'b', 'c', 'd', 'e')", "IN (...S)"},
{"IN (1, 2, 3)", "IN (N, N, N)"},
{"IN (1, 2, 3, 4, 5)", "IN (...N)"},
}

for _, d := range data {
if a := normalizeQuery(d.input); a != d.expected {
t.Errorf("data=%v, actual=%q", d, a)
}
}
}

0 comments on commit 9009c79

Please sign in to comment.