VictoriaMetrics/lib/fs/reader_at_timing_test.go
Aliaksandr Valialkin 06f6de6d47
all: use os.{Read|Write}File instead of ioutil.{Read|Write}File
The ioutil.{Read|Write}File is deprecated since Go1.16 -
see https://tip.golang.org/doc/go1.16#ioutil

VictoriaMetrics needs at least Go1.18, so it is safe to remove ioutil usage
from source code.

This is a follow-up for 02ca2342ab
2022-08-21 23:55:20 +03:00

53 lines
1.1 KiB
Go

package fs
import (
"fmt"
"os"
"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 = 8 * 1024 * 1024
data := make([]byte, fileSize)
if err := os.WriteFile(path, data, 0600); err != nil {
b.Fatalf("cannot create %q: %s", path, err)
}
defer MustRemoveAll(path)
r := MustOpenReaderAt(path)
defer r.MustClose()
b.ResetTimer()
for _, bufSize := range []int{1, 1e1, 1e2, 1e3, 1e4, 1e5} {
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))
}
})
})
}
}