Files
wazero/internal/sysfs/sysfs_bench_test.go
Crypt Keeper 2a584a8937 fs: renames internal syscallfs package to sysfs and notes RATIONALE (#1056)
It will help for us to rename earlier vs later, and syscallfs will be
laborious, especially after we introduce an FSConfig type and need to
declare a method name that differentiates from normal fs.FS. e.g. WithFS
vs WithSysFS reads nicer than WithSyscallFS, and meanwhile sys is
already a public package.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-23 11:11:35 +08:00

64 lines
1.2 KiB
Go

package sysfs
import (
"io"
"io/fs"
"os"
"testing"
)
func BenchmarkReaderAtOffset(b *testing.B) {
dirFS := os.DirFS("testdata")
embedFS, err := fs.Sub(readerAtFS, "testdata")
if err != nil {
b.Fatal(err)
}
benches := []struct {
name string
fs fs.FS
ra bool
}{
{name: "os.DirFS io.File", fs: dirFS, ra: false},
{name: "os.DirFS readerAtOffset", fs: dirFS, ra: true},
{name: "embed.FS embed.file", fs: embedFS, ra: false},
{name: "embed.FS seekToOffsetReader", fs: embedFS, ra: true},
}
buf := make([]byte, 3)
for _, bc := range benches {
bc := bc
b.Run(bc.name, func(b *testing.B) {
f, err := bc.fs.Open("wazero.txt")
if err != nil {
b.Fatal(err)
}
defer f.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
// Reset the read position back to the beginning of the file.
if _, err = f.(io.Seeker).Seek(0, io.SeekStart); err != nil {
b.Fatal(err)
}
// Get the reader we are benchmarking
var r io.Reader = f
if bc.ra {
r = ReaderAtOffset(f, 3)
}
b.StartTimer()
if _, err := r.Read(buf); err != nil {
b.Fatal(err)
}
b.StopTimer()
}
})
}
}