func TestParserSlowLog009(t *testing.T) {
	opt := opt
	opt.FilterAdminCommand = map[string]bool{
		"Quit": true,
	}
	got := parseSlowLog(t, "slow009.log", opt)
	expect := []log.Event{
		{
			Query:     "Refresh",
			Db:        "",
			Admin:     true,
			Host:      "localhost",
			User:      "root",
			Offset:    196,
			OffsetEnd: 562,
			Ts:        time.Date(2009, 03, 11, 18, 11, 50, 0, time.UTC),
			TimeMetrics: map[string]float64{
				"Query_time": 0.017850,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_affected": 0,
				"Rows_examined": 0,
				"Rows_read":     0,
				"Rows_sent":     0,
				"Thread_id":     47,
			},
			BoolMetrics: map[string]bool{
				"QC_Hit":            false,
				"Full_scan":         false,
				"Full_join":         false,
				"Tmp_table":         false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Filesort_on_disk":  false,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
Filter admin commands
package slow_test
import (
	l "log"
	"os"
	"path"
	"testing"
	"time"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/percona/go-mysql/log"
	parser "github.com/percona/go-mysql/log/slow"
	"github.com/percona/go-mysql/test"
)
var (
	sample = test.RootDir() + "/test/slow-logs"
	opt    = log.Options{
		Debug:           false,
		DefaultLocation: time.UTC,
	}
)
func parseSlowLog(t *testing.T, filename string, o log.Options) []log.Event {
	file, err := os.Open(path.Join(sample, "/", filename))
	require.NoError(t, err)
	p := parser.NewSlowLogParser(file, o)
	require.NoError(t, err)
	got := []log.Event{}
	go p.Start()
	for e := range p.EventChan() {
		got = append(got, *e)
	}
	return got
}
func TestParserEmptySlowLog(t *testing.T) {
	got := parseSlowLog(t, "empty.log", opt)
	expect := []log.Event{}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog001(t *testing.T) {
	got := parseSlowLog(t, "slow001.log", opt)
	expect := []log.Event{
		{
			Ts:        time.Date(2007, 10, 15, 21, 43, 52, 0, time.UTC),
			Admin:     false,
			Query:     `select sleep(2) from n`,
			User:      "root",
			Host:      "localhost",
			Db:        "test",
			Offset:    199,
			OffsetEnd: 358,
			TimeMetrics: map[string]float64{
				"Query_time": 2,
				"Lock_time":  0,
			},
			NumberMetrics: map[string]uint64{
				"Rows_sent":     1,
				"Rows_examined": 0,
			},
			BoolMetrics: map[string]bool{},
		},
		{
			Ts:        time.Date(2007, 10, 15, 21, 45, 10, 0, time.UTC),
			Admin:     false,
			Query:     `select sleep(2) from test.n`,
			User:      "root",
			Host:      "localhost",
			Db:        "sakila",
			Offset:    358,
			OffsetEnd: 524,
			TimeMetrics: map[string]float64{
				"Query_time": 2,
				"Lock_time":  0,
			},
			NumberMetrics: map[string]uint64{
				"Rows_sent":     1,
				"Rows_examined": 0,
			},
			BoolMetrics: map[string]bool{},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParseSlowLog002(t *testing.T) {
	got := parseSlowLog(t, "slow002.log", opt)
	expect := []log.Event{
		{
			Query:     "BEGIN",
			Ts:        time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    0,
			OffsetEnd: 337,
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Thread_id":     10,
				"Rows_examined": 0,
				"Rows_sent":     0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Db: "db1",
			Query: `update db2.tuningdetail_21_265507 n
      inner join db1.gonzo a using(gonzo)
      set n.column1 = a.column1, n.word3 = a.word3`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    337,
			OffsetEnd: 813,
			TimeMetrics: map[string]float64{
				"Query_time": 0.726052,
				"Lock_time":  0.000091,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Thread_id":     10,
				"Rows_examined": 62951,
				"Rows_sent":     0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         true,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Query: `INSERT INTO db3.vendor11gonzo (makef, bizzle)
VALUES ('', 'Exact')`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    813,
			OffsetEnd: 1332,
			TimeMetrics: map[string]float64{
				"InnoDB_queue_wait":    0.000000,
				"Lock_time":            0.000077,
				"InnoDB_rec_lock_wait": 0.000000,
				"Query_time":           0.000512,
				"InnoDB_IO_r_wait":     0.000000,
			},
			NumberMetrics: map[string]uint64{
				"InnoDB_IO_r_bytes":     0,
				"Merge_passes":          0,
				"InnoDB_pages_distinct": 24,
				"Rows_sent":             0,
				"Thread_id":             10,
				"Rows_examined":         0,
				"InnoDB_IO_r_ops":       0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Query: `UPDATE db4.vab3concept1upload
SET    vab3concept1id = '91848182522'
WHERE  vab3concept1upload='6994465'`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    1332,
			OffsetEnd: 1862,
			TimeMetrics: map[string]float64{
				"Query_time":           0.033384,
				"InnoDB_IO_r_wait":     0.000000,
				"InnoDB_queue_wait":    0.000000,
				"Lock_time":            0.000028,
				"InnoDB_rec_lock_wait": 0.000000,
			},
			NumberMetrics: map[string]uint64{
				"InnoDB_IO_r_bytes":     0,
				"Merge_passes":          0,
				"InnoDB_pages_distinct": 11,
				"Rows_sent":             0,
				"Thread_id":             10,
				"Rows_examined":         0,
				"InnoDB_IO_r_ops":       0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Query: `INSERT INTO db1.conch (word3, vid83)
VALUES ('211', '18')`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    1862,
			OffsetEnd: 2391,
			TimeMetrics: map[string]float64{
				"InnoDB_queue_wait":    0.000000,
				"Query_time":           0.000530,
				"InnoDB_IO_r_wait":     0.000000,
				"Lock_time":            0.000027,
				"InnoDB_rec_lock_wait": 0.000000,
			},
			NumberMetrics: map[string]uint64{
				"InnoDB_IO_r_bytes":     0,
				"Merge_passes":          0,
				"InnoDB_pages_distinct": 18,
				"Rows_sent":             0,
				"Thread_id":             10,
				"Rows_examined":         0,
				"InnoDB_IO_r_ops":       0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Query: `UPDATE foo.bar
SET    biz = '91848182522'`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    2391,
			OffsetEnd: 2859,
			TimeMetrics: map[string]float64{
				"Lock_time":            0.000027,
				"InnoDB_rec_lock_wait": 0.000000,
				"InnoDB_queue_wait":    0.000000,
				"Query_time":           0.000530,
				"InnoDB_IO_r_wait":     0.000000,
			},
			NumberMetrics: map[string]uint64{
				"InnoDB_IO_r_bytes":     0,
				"Merge_passes":          0,
				"InnoDB_pages_distinct": 18,
				"Rows_sent":             0,
				"Thread_id":             10,
				"Rows_examined":         0,
				"InnoDB_IO_r_ops":       0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Query: `UPDATE bizzle.bat
SET    boop='bop: 899'
WHERE  fillze='899'`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    2859,
			OffsetEnd: 3372,
			TimeMetrics: map[string]float64{
				"Query_time":           0.000530,
				"InnoDB_IO_r_wait":     0.000000,
				"InnoDB_queue_wait":    0.000000,
				"Lock_time":            0.000027,
				"InnoDB_rec_lock_wait": 0.000000,
			},
			NumberMetrics: map[string]uint64{
				"InnoDB_IO_r_bytes":     0,
				"Merge_passes":          0,
				"InnoDB_pages_distinct": 18,
				"Rows_sent":             0,
				"Thread_id":             10,
				"Rows_examined":         0,
				"InnoDB_IO_r_ops":       0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
		{
			Query: `UPDATE foo.bar
SET    biz = '91848182522'`,
			Admin:     false,
			User:      "[SQL_SLAVE]",
			Host:      "",
			Offset:    3372,
			OffsetEnd: 3840,
			TimeMetrics: map[string]float64{
				"Query_time":           0.000530,
				"Lock_time":            0.000027,
				"InnoDB_rec_lock_wait": 0.000000,
				"InnoDB_queue_wait":    0.000000,
				"InnoDB_IO_r_wait":     0.000000,
			},
			NumberMetrics: map[string]uint64{
				"InnoDB_IO_r_bytes":     0,
				"Merge_passes":          0,
				"InnoDB_pages_distinct": 18,
				"Rows_sent":             0,
				"Thread_id":             10,
				"Rows_examined":         0,
				"InnoDB_IO_r_ops":       0,
			},
			BoolMetrics: map[string]bool{
				"Filesort":          false,
				"Full_scan":         false,
				"Filesort_on_disk":  false,
				"Full_join":         false,
				"Tmp_table_on_disk": false,
				"Tmp_table":         false,
				"QC_Hit":            false,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog003(t *testing.T) {
	got := parseSlowLog(t, "slow003.log", opt)
	expect := []log.Event{
		{
			Query:     "BEGIN",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    1,
			OffsetEnd: 338,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Lock_time":  0.000000,
				"Query_time": 0.000012,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     10,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog004(t *testing.T) {
	got := parseSlowLog(t, "slow004.log", opt)
	expect := []log.Event{
		{
			Query:       "select 12_13_foo from (select 12foo from 123_bar) as 123baz",
			Admin:       false,
			Host:        "localhost",
			Ts:          time.Date(2007, 10, 15, 21, 43, 52, 0, time.UTC),
			User:        "root",
			Offset:      199,
			OffsetEnd:   385,
			BoolMetrics: map[string]bool{},
			TimeMetrics: map[string]float64{
				"Lock_time":  0.000000,
				"Query_time": 2.000000,
			},
			NumberMetrics: map[string]uint64{
				"Rows_sent":     1,
				"Rows_examined": 0,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog005(t *testing.T) {
	got := parseSlowLog(t, "slow005.log", opt)
	expect := []log.Event{
		{
			Query:     "foo\nbar\n\t\t\t0 AS counter\nbaz",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    0,
			OffsetEnd: 359,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     10,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog006(t *testing.T) {
	got := parseSlowLog(t, "slow006.log", opt)
	expect := []log.Event{
		{
			Query:     "SELECT col FROM foo_tbl",
			Db:        "foo",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    0,
			OffsetEnd: 368,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     10,
			},
		},
		{
			Query:     "SELECT col FROM foo_tbl",
			Db:        "foo",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 48, 57, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    368,
			OffsetEnd: 736,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     10,
			},
		},
		{
			Query:     "SELECT col FROM bar_tbl",
			Db:        "bar",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 48, 57, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    736,
			OffsetEnd: 1100,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     20,
			},
		},
		{
			Query:     "SELECT col FROM bar_tbl",
			Db:        "bar",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 49, 05, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    1100,
			OffsetEnd: 1468,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     10,
			},
		},
		{
			Query:     "SELECT col FROM bar_tbl",
			Db:        "bar",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 49, 07, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    1468,
			OffsetEnd: 1832,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     20,
			},
		},
		{
			Query:     "SELECT col FROM foo_tbl",
			Db:        "foo",
			Admin:     false,
			Host:      "",
			Ts:        time.Date(2007, 12, 18, 11, 49, 30, 0, time.UTC),
			User:      "[SQL_SLAVE]",
			Offset:    1832,
			OffsetEnd: 2200,
			BoolMetrics: map[string]bool{
				"Filesort_on_disk":  false,
				"Tmp_table_on_disk": false,
				"Filesort":          false,
				"Full_join":         false,
				"Full_scan":         false,
				"QC_Hit":            false,
				"Tmp_table":         false,
			},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Merge_passes":  0,
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     30,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog007(t *testing.T) {
	got := parseSlowLog(t, "slow007.log", opt)
	expect := []log.Event{
		{
			Query:       "SELECT fruit FROM trees",
			Db:          "db2",
			Admin:       false,
			Host:        "",
			Ts:          time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
			User:        "[SQL_SLAVE]",
			Offset:      0,
			OffsetEnd:   193,
			BoolMetrics: map[string]bool{},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000012,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     3,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}
func TestParserSlowLog008(t *testing.T) {
	got := parseSlowLog(t, "slow008.log", opt)
	expect := []log.Event{
		{
			Query:       "Quit",
			Db:          "db1",
			Admin:       true,
			Host:        "",
			User:        "meow",
			Offset:      0,
			OffsetEnd:   220,
			BoolMetrics: map[string]bool{},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000002,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     5,
			},
		},
		{
			Query:       "SET NAMES utf8",
			Db:          "db",
			Admin:       false,
			Host:        "",
			User:        "meow",
			Offset:      220,
			OffsetEnd:   434,
			BoolMetrics: map[string]bool{},
			TimeMetrics: map[string]float64{
				"Query_time": 0.000899,
				"Lock_time":  0.000000,
			},
			NumberMetrics: map[string]uint64{
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     6,
			},
		},
		{
			Query:       "SELECT MIN(id),MAX(id) FROM tbl",
			Db:          "db2",
			Admin:       false,
			Host:        "",
			User:        "meow",
			Offset:      434,
			OffsetEnd:   656,
			BoolMetrics: map[string]bool{},
			TimeMetrics: map[string]float64{
				"Query_time": 0.018799,
				"Lock_time":  0.009453,
			},
			NumberMetrics: map[string]uint64{
				"Rows_examined": 0,
				"Rows_sent":     0,
				"Thread_id":     6,
			},
		},
	}
	assert.EqualValues(t, expect, got)
}