bench: add more WASI benchmarks (#5309)

* bench: add more WASI benchmarks

This follows up on #5274 to add several more scenarios with which to
benchmark WASI performance:
- `open-file.wat`: opens and closes a file
- `read-file.wat`: opens a file, reads 4K bytes from it, then closes it
- `read-dir.wat`: reads a directory's entries

Each benchmark is hand-crafted WAT to more clearly control what WASI
calls are made. As with #5274, these modules' sole entry point takes a
parameter indicating the number of iterations to run in order to use
`criterion`'s `iter_custom` feature.

* fix: reduce expected size of directory entries
This commit is contained in:
Andrew Brown
2022-11-21 16:02:06 -08:00
committed by GitHub
parent d0d3245a35
commit 4899537328
5 changed files with 189 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
//! Measure some common WASI call scenarios.
use criterion::{criterion_group, criterion_main, Criterion};
use std::time::Instant;
use std::{fs::File, path::Path, time::Instant};
use wasmtime::{Engine, Linker, Module, Store, TypedFunc};
use wasmtime_wasi::{sync::WasiCtxBuilder, WasiCtx};
@@ -9,6 +9,15 @@ criterion_group!(benches, bench_wasi);
criterion_main!(benches);
fn bench_wasi(c: &mut Criterion) {
let _ = env_logger::try_init();
// Build a zero-filled test file if it does not yet exist.
let test_file = Path::new("benches/wasi/test.bin");
if !test_file.is_file() {
let file = File::create(test_file).unwrap();
file.set_len(4096).unwrap();
}
// Benchmark each `*.wat` file in the `wasi` directory.
for file in std::fs::read_dir("benches/wasi").unwrap() {
let path = file.unwrap().path();
@@ -67,5 +76,11 @@ fn wasi_context() -> WasiCtx {
"--flag4".to_string(),
])
.unwrap()
.preopened_dir(
wasmtime_wasi::Dir::open_ambient_dir("benches/wasi", wasmtime_wasi::ambient_authority())
.unwrap(),
"/",
)
.unwrap()
.build()
}