wasi: fixes fd_readdir bench (#1554)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-07-04 11:47:44 +08:00
committed by GitHub
parent 511b032fa6
commit c93bbf29fb

View File

@@ -133,39 +133,35 @@ func Benchmark_fdReaddir(b *testing.B) {
fs fs.FS
// dirMount ensures direct use of syscall.FS
dirMount string
// continued is true to test performance on a follow-up call. The
// preceding will call fd_read with 24 bytes, which is enough to read
// the initial entry's size, but not enough to read its name. This
// ensures the next fd_read is allowed to pass a cookie, because it
// read fd_next, while ensuring it will write all the entries.
continued bool
// twoCalls tests performance of reading a directory in two calls.
twoCalls bool
}{
{
name: "embed.FS",
fs: embedFS,
},
{
name: "embed.FS - continued",
fs: embedFS,
continued: true,
name: "embed.FS - two calls",
fs: embedFS,
twoCalls: true,
},
{
name: "os.DirFS",
fs: os.DirFS("testdata"),
},
{
name: "os.DirFS - continued",
fs: os.DirFS("testdata"),
continued: true,
name: "os.DirFS - two calls",
fs: os.DirFS("testdata"),
twoCalls: true,
},
{
name: "sysfs.DirFS",
dirMount: "testdata",
},
{
name: "sysfs.DirFS - continued",
dirMount: "testdata",
continued: true,
name: "sysfs.DirFS - two calls",
dirMount: "testdata",
twoCalls: true,
},
}
@@ -190,18 +186,6 @@ func Benchmark_fdReaddir(b *testing.B) {
fn := mod.ExportedFunction(wasip1.FdReaddirName)
// Open the root directory as a file-descriptor.
fsc := mod.(*wasm.ModuleInstance).Sys.FS()
fd, errno := fsc.OpenFile(fsc.RootFS(), ".", os.O_RDONLY, 0)
if errno != 0 {
b.Fatal(errno)
}
f, ok := fsc.LookupFile(fd)
if !ok {
b.Fatal("couldn't open fd ", fd)
}
defer fsc.CloseFile(fd) //nolint
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
@@ -212,26 +196,29 @@ func Benchmark_fdReaddir(b *testing.B) {
buf := 8 // where to start the dirents
bufLen := 8096 // allow up to 8KB buffer usage
// Recreate the file under the file-descriptor
if errno = f.File.Close(); errno != 0 {
// Open the root directory as a file-descriptor.
fsc := mod.(*wasm.ModuleInstance).Sys.FS()
fd, errno := fsc.OpenFile(fsc.RootFS(), ".", os.O_RDONLY, 0)
if errno != 0 {
b.Fatal(errno)
}
if f.File, errno = fsc.RootFS().OpenFile(".", os.O_RDONLY, 0); errno != 0 {
b.Fatal(errno)
}
// Make an initial call to build the state of an unread directory
if bc.continued {
results, err := fn.Call(testCtx, uint64(fd), uint64(buf), uint64(24), uint64(cookie), uint64(resultBufused))
if err != nil {
b.Fatal(err)
}
requireESuccess(b, results)
cookie = 1 // WASI doesn't document this, but we write the first d_next as 1
}
// Time the call to write the dirents
b.StartTimer()
if bc.twoCalls {
// Read the dot entries
bufLen := wasip1.DirentSize + 1 // size of "."
bufLen += wasip1.DirentSize + 2 // size of ".."
results, err := fn.Call(testCtx, uint64(fd), uint64(buf), uint64(bufLen), uint64(cookie), uint64(resultBufused))
if err != nil {
b.Fatal(err)
}
requireESuccess(b, results)
cookie = 2 // d_next of "..", the real file we couldn't read.
}
results, err := fn.Call(testCtx, uint64(fd), uint64(buf), uint64(bufLen), uint64(cookie), uint64(resultBufused))
if err != nil {
b.Fatal(err)
@@ -239,6 +226,9 @@ func Benchmark_fdReaddir(b *testing.B) {
b.StopTimer()
requireESuccess(b, results)
if errno = fsc.CloseFile(fd); errno != 0 {
b.Fatal(errno)
}
}
})
}
@@ -355,6 +345,7 @@ func Benchmark_pathFilestat(b *testing.B) {
}
func requireESuccess(b *testing.B, results []uint64) {
b.Helper()
if errno := wasip1.Errno(results[0]); errno != 0 {
b.Fatal(wasip1.ErrnoName(errno))
}