diff --git a/search_log_test.go b/search_log_test.go index b1d88ce..ea37606 100644 --- a/search_log_test.go +++ b/search_log_test.go @@ -592,3 +592,34 @@ func (s *searchLogSuite) TestReadAndAppendLogFile(c *C) { time.Sleep(15 * time.Millisecond) } } + +func (s *searchLogSuite) BenchmarkReadLastLines(c *C) { + // step 1. initial a log file + s.writeTmpFile(c, "tidb.log", []string{ + `[2019/08/26 06:22:13.011 -04:00] [INFO] [printer.go:41] ["Welcome to TiDB."]`, + `[2019/08/26 06:22:14.011 -04:00] [INFO] [printer.go:41] ["Welcome to TiDB."]`, + `[2019/08/26 06:22:15.011 -04:00] [INFO] [printer.go:41] ["Welcome to TiDB."]`, + `[2019/08/26 06:22:16.011 -04:00] [INFO] [printer.go:41] ["Welcome to TiDB."]`, + `[2019/08/26 06:22:17.011 -04:00] [INFO] [printer.go:41] ["Welcome to TiDB."]`, + }) + + // step 2. open it as read only mode + path := filepath.Join(s.tmpDir, "tidb.log") + file, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm) + c.Assert(err, IsNil, Commentf("open file %s failed", path)) + defer file.Close() + + stat, _ := file.Stat() + filesize := stat.Size() + + // step 3. start to benchmark + c.ResetTimer() + for i := 0; i < c.N; i++ { + sysutil.ReadLastLines(file, filesize) + } +} + +// run benchmark by `go test -check.b` +// result: +// searchLogSuite.BenchmarkReadLastLines 1000000 1920 ns/op +// searchLogSuite.BenchmarkReadLastLines 1000000 1892 ns/op