Signed-off-by: Adrian Cole <adrian@tetrate.io> Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com> Co-authored-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
package sysfs
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func BenchmarkFsFileUtimesNs(b *testing.B) {
|
|
f, errno := OpenOSFile(path.Join(b.TempDir(), "file"), syscall.O_CREAT, 0)
|
|
if errno != 0 {
|
|
b.Fatal(errno)
|
|
}
|
|
defer f.Close()
|
|
|
|
times := &[2]syscall.Timespec{
|
|
{Sec: 123, Nsec: 4 * 1e3},
|
|
{Sec: 123, Nsec: 4 * 1e3},
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
if errno := f.Utimens(times); errno != 0 {
|
|
b.Fatal(errno)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkFsFileRead(b *testing.B) {
|
|
dirFS := os.DirFS("testdata")
|
|
embedFS, err := fs.Sub(testdata, "testdata")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
benches := []struct {
|
|
name string
|
|
fs fs.FS
|
|
pread bool
|
|
}{
|
|
{name: "os.DirFS Read", fs: dirFS, pread: false},
|
|
{name: "os.DirFS Pread", fs: dirFS, pread: true},
|
|
{name: "embed.api.FS Read", fs: embedFS, pread: false},
|
|
{name: "embed.api.FS Pread", fs: embedFS, pread: true},
|
|
}
|
|
|
|
buf := make([]byte, 3)
|
|
|
|
for _, bc := range benches {
|
|
bc := bc
|
|
|
|
b.Run(bc.name, func(b *testing.B) {
|
|
name := "wazero.txt"
|
|
f, errno := OpenFSFile(bc.fs, name, syscall.O_RDONLY, 0)
|
|
if errno != 0 {
|
|
b.Fatal(errno)
|
|
}
|
|
defer f.Close()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
b.StopTimer()
|
|
|
|
var n int
|
|
var errno syscall.Errno
|
|
|
|
// Reset the read position back to the beginning of the file.
|
|
if _, errno = f.Seek(0, io.SeekStart); errno != 0 {
|
|
b.Fatal(errno)
|
|
}
|
|
|
|
b.StartTimer()
|
|
if bc.pread {
|
|
n, errno = f.Pread(buf, 3)
|
|
} else {
|
|
n, errno = f.Read(buf)
|
|
}
|
|
b.StopTimer()
|
|
|
|
if errno != 0 {
|
|
b.Fatal(errno)
|
|
} else if bufLen := len(buf); n < bufLen {
|
|
b.Fatalf("nread %d less than capacity %d", n, bufLen)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|