2020-06-05 14:09:20 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-21 20:51:13 +00:00
|
|
|
"os"
|
2020-06-05 14:09:20 +00:00
|
|
|
"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"
|
2020-06-05 16:07:57 +00:00
|
|
|
const fileSize = 8 * 1024 * 1024
|
2020-06-05 14:09:20 +00:00
|
|
|
data := make([]byte, fileSize)
|
2022-08-21 20:51:13 +00:00
|
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
2020-06-05 14:09:20 +00:00
|
|
|
b.Fatalf("cannot create %q: %s", path, err)
|
|
|
|
}
|
|
|
|
defer MustRemoveAll(path)
|
2020-11-23 07:55:38 +00:00
|
|
|
r := MustOpenReaderAt(path)
|
2020-06-05 14:09:20 +00:00
|
|
|
defer r.MustClose()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2020-06-05 16:07:57 +00:00
|
|
|
for _, bufSize := range []int{1, 1e1, 1e2, 1e3, 1e4, 1e5} {
|
2020-06-05 14:09:20 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|