Files
wasmtime/benches/wasi/read-environment.wat
Andrew Brown 8426904129 bench: benchmark several common WASI scenarios (#5274)
In order to properly understand the impact of providing thread-safe
implmentations of WASI contexts (#5235), we need benchmarks that measure
the current performance of WASI calls using Wiggle. This change adds
several common WASI scenarios as WAT files (see `benches/wasi/*.wat`)
and benchmarks them with `criterion`. Using `criterion`'s `iter_custom`,
the WAT file runs the desired number of benchmark iterations internally
and the total duration of the runs is divided to get the average time
for each loop iteration.

Why WAT? When compiling these benchmarks from Rust to `wasm32-wasi`, the
output files are large, contain other WASI imports than the desired
ones, and overall it is difficult to tell if we are measuring what we
expect. By hand-writing the WAT, it is (slightly) more clear what each
benchmark is doing.
2022-11-15 17:02:35 -08:00

46 lines
2.0 KiB
Plaintext

(module
(import "wasi_snapshot_preview1" "environ_get"
(func $__wasi_environ_get (param i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "environ_sizes_get"
(func $__wasi_environ_sizes_get (param i32 i32) (result i32)))
(func (export "run") (param $iters i64) (result i64)
(local $i i64)
(local.set $i (i64.const 0))
(loop $cont
;; Read the current environment key-value pairs by:
;; 1) retrieving the environment sizes and then
;; 2) retrieving the environment data itself.
;; Retrieve the sizes of the environment with parameters:
;; - the address at which to write the number of environment
;; variables
;; - the address at which to write the size of the environment
;; buffer
;; Returns an error code.
(call $__wasi_environ_sizes_get (i32.const 0) (i32.const 4))
(drop)
;; Read the environment with parameters:
;; - the address at which to write the array of environment pointers
;; (i.e., one pointer per key-value pair); here we overwrite
;; the size written at address 0
;; - the address at which to write the buffer of key-value pairs
;; (pointed to by the items written to the first address); we
;; calculate where to start the buffer based on the size of the
;; pointer list (i.e., number of key-value pairs * 4 bytes per
;; pointer)
;; Returns an error code.
(call $__wasi_environ_get
(i32.const 0)
(i32.mul (i32.load (i32.const 0)) (i32.const 4)))
(drop)
;; Continue looping until $i reaches $iters.
(local.set $i (i64.add (local.get $i) (i64.const 1)))
(br_if $cont (i64.lt_u (local.get $i) (local.get $iters)))
)
(local.get $i)
)
(memory (export "memory") 1)
)