Skip to content

Commit

Permalink
add benchmark for readLastLine method
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine committed Jul 8, 2020
1 parent 7480a11 commit b01afce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package sysutil
var (
ParseLogItem = parseLogItem
ReadLine = readLine
ReadLastLine = readLastLine
ParseTimeStamp = parseTimeStamp
ResolveFiles = resolveFiles
)
28 changes: 28 additions & 0 deletions search_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,31 @@ func (s *searchLogSuite) TestParseLogItem(c *C) {
c.Assert(item.Message, Equals, cas.message)
}
}

func (s *searchLogSuite) BenchmarkReadLastLine(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()

// step 3. start to benchmark
c.ResetTimer()
for i := 0; i < c.N; i++ {
sysutil.ReadLastLine(file)
}
}

// run benchmark by `go test -check.b`
// result:
// searchLogSuite.BenchmarkReadLastLine 10000 124423 ns/op
// searchLogSuite.BenchmarkReadLastLine 10000 126135 ns/op

0 comments on commit b01afce

Please sign in to comment.