Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine committed Jul 8, 2020
1 parent f628718 commit 508ad40
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions search_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 508ad40

Please sign in to comment.