mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-10 15:14:09 +00:00
lib/fs: add a benchmark for ReaderAt.MustReadAt
This commit is contained in:
parent
01719f4949
commit
2358d9e41d
1 changed files with 56 additions and 0 deletions
56
lib/fs/reader_at_timing_test.go
Normal file
56
lib/fs/reader_at_timing_test.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkReaderAtMustReadAt(b *testing.B) {
|
||||||
|
b.Run("mmap_on", func(b *testing.B) {
|
||||||
|
benchmarkReaderAtMustReadAt(b, true)
|
||||||
|
})
|
||||||
|
b.Run("mmap_off", func(b *testing.B) {
|
||||||
|
benchmarkReaderAtMustReadAt(b, false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkReaderAtMustReadAt(b *testing.B, isMmap bool) {
|
||||||
|
prevDisableMmap := *disableMmap
|
||||||
|
*disableMmap = !isMmap
|
||||||
|
defer func() {
|
||||||
|
*disableMmap = prevDisableMmap
|
||||||
|
}()
|
||||||
|
|
||||||
|
path := "BenchmarkReaderAtMustReadAt"
|
||||||
|
const fileSize = 1024 * 1024
|
||||||
|
data := make([]byte, fileSize)
|
||||||
|
if err := ioutil.WriteFile(path, data, 0600); err != nil {
|
||||||
|
b.Fatalf("cannot create %q: %s", path, err)
|
||||||
|
}
|
||||||
|
defer MustRemoveAll(path)
|
||||||
|
r, err := OpenReaderAt(path)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("error in OpenReaderAt(%q): %s", path, err)
|
||||||
|
}
|
||||||
|
defer r.MustClose()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for _, bufSize := range []int{1, 10, 100, 1000, 10000} {
|
||||||
|
b.Run(fmt.Sprintf("%d", bufSize), func(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
b.SetBytes(int64(bufSize))
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
buf := make([]byte, bufSize)
|
||||||
|
var offset int64
|
||||||
|
for pb.Next() {
|
||||||
|
if len(buf)+int(offset) > fileSize {
|
||||||
|
offset = 0
|
||||||
|
}
|
||||||
|
r.MustReadAt(buf, offset)
|
||||||
|
offset += int64(len(buf))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue