A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasi-http: Add benchmarks for individual wasi:http p3 APIs (#14961)
Adam Bratschi-Kayea6bae652bc56
diff --git a/crates/test-programs/src/bin/p3_http_api_bench.rs b/crates/test-programs/src/bin/p3_http_api_bench.rsnew file mode 100644index 0000000000..382a81747c--- /dev/null+++ b/crates/test-programs/src/bin/p3_http_api_bench.rs@@ -0,0 +1,153 @@+//! Guest for benchmarking individual `wasi:http` APIs.+//!+//! Usage: `p3_http_api_bench <api> <iters>`, where `<api>` is one of+//! `request.new`, `response.consume-body` or `client.send`.+//!+//! Each call will perform setup steps and then time itself calling the given+//! WASI api multiple times in a loop. The total time in nanoseconds is printed+//! to stdout.++use test_programs::p3::{+ wasi::cli::environment,+ wasi::clocks::monotonic_clock,+ wasi::http::{+ client,+ types::{ErrorCode, Fields, Request, Response, Scheme, Trailers},+ },+ wit_future, wit_stream,+};+use wit_bindgen::{FutureWriter, StreamWriter};++struct Component;++test_programs::p3::export!(Component);++/// Arguments are created, and results dropped, in chunks of this many calls+/// to bound usage of memory and other resources.+const CHUNK: u64 = 10_000;++type BodyWriters = (+ StreamWriter<u8>,+ FutureWriter<Result<Option<Trailers>, ErrorCode>>,+);++impl test_programs::p3::exports::wasi::cli::run::Guest for Component {+ async fn run() -> Result<(), ()> {+ let args = environment::get_arguments();+ let [_, api, iters] = &args[..] else {+ panic!("usage: p3_http_api_bench <api> <iters>");+ };+ let iters: u64 = iters.parse().unwrap();++ let mut elapsed = 0;+ let mut remaining = iters;+ while remaining > 0 {+ let n = remaining.min(CHUNK);+ remaining -= n;+ let n = usize::try_from(n).unwrap();+ elapsed += match api.as_str() {+ "request.new" => request_new(n),+ "response.consume-body" => response_consume_body(n).await,+ "client.send" => client_send(n).await,+ api => panic!("unknown API `{api}`"),+ };+ }+ println!("{elapsed}");+ Ok(())+ }+}++/// Time `n` calls to `request.new`, passing a body stream as a proxy would.+fn request_new(n: usize) -> u64 {+ // Vec to hold the write ends of futures/streams created until after timing+ // is done. This prevents accidentally including the sending of the default+ // values in the timing.+ let mut writers = Vec::with_capacity(n);+ let args = (0..n)+ .map(|_| {+ let (body_tx, body_rx) = wit_stream::new();+ let (trailers_tx, trailers_rx) = wit_future::new(|| Ok(None));+ writers.push((body_tx, trailers_tx));+ (Fields::new(), body_rx, trailers_rx)+ })+ .collect::<Vec<_>>();+ let mut results = Vec::with_capacity(n);++ let start = monotonic_clock::now();+ for (headers, body, trailers) in args {+ results.push(Request::new(headers, Some(body), trailers, None));+ }+ let elapsed = monotonic_clock::now() - start;++ drop(results);+ drop(writers);+ elapsed+}++/// Time `n` calls to `response.consume-body` on responses from+/// `client.send`, whose bodies are created by the host. This takes the same+/// path in the host as consuming the body of a proxy's incoming request.+async fn response_consume_body(n: usize) -> u64 {+ let (requests, body_writers) = new_outgoing_requests(n);+ // Vec to hold writers so sending their default value isn't timed.+ let mut writers = Vec::with_capacity(n);+ let mut args = Vec::with_capacity(n);+ for request in requests {+ let response = client::send(request).await.unwrap();+ let (result_tx, result_rx) = wit_future::new(|| Ok(()));+ writers.push(result_tx);+ args.push((response, result_rx));+ }+ let mut results = Vec::with_capacity(n);++ let start = monotonic_clock::now();+ for (response, fut) in args {+ results.push(Response::consume_body(response, fut));+ }+ let elapsed = monotonic_clock::now() - start;++ drop(results);+ drop(writers);+ drop(body_writers);+ elapsed+}++/// Time `n` sequential calls to `client.send`.+async fn client_send(n: usize) -> u64 {+ let (args, writers) = new_outgoing_requests(n);+ let mut results = Vec::with_capacity(n);++ let start = monotonic_clock::now();+ for request in args {+ results.push(client::send(request).await.unwrap());+ }+ let elapsed = monotonic_clock::now() - start;++ drop(results);+ drop(writers);+ elapsed+}++/// Create a request with a body stream, as a proxy would, returning it along+/// with the writable ends of its body and trailers.+fn new_request() -> (Request, BodyWriters) {+ let (body_tx, body_rx) = wit_stream::new();+ let (trailers_tx, trailers_rx) = wit_future::new(|| Ok(None));+ let request = Request::new(Fields::new(), Some(body_rx), trailers_rx, None).0;+ (request, (body_tx, trailers_tx))+}++fn new_outgoing_requests(n: usize) -> (Vec<Request>, Vec<BodyWriters>) {+ (0..n)+ .map(|_| {+ let (request, writers) = new_request();+ request.set_path_with_query(Some("/")).unwrap();+ request.set_scheme(Some(&Scheme::Http)).unwrap();+ request.set_authority(Some("p3-test")).unwrap();+ (request, writers)+ })+ .unzip()+}++// Unused function; required since this file is built as a `bin`:+fn main() {}diff --git a/crates/wasi-http/benches/p3_http_proxy.rs b/crates/wasi-http/benches/p3_http_proxy.rsindex a161aedbbf..50992ea3c4 100644--- a/crates/wasi-http/benches/p3_http_proxy.rs+++ b/crates/wasi-http/benches/p3_http_proxy.rs@@ -1,12 +1,15 @@//! Benchmark the `p3_http_proxy` test program against a host which answers-//! every outgoing request with a fixed short response.+//! every outgoing request with a fixed short response, and the individual+//! `wasi:http` APIs it uses with the `p3_http_api_bench` test program.use bytes::Bytes;use criterion::{Criterion, criterion_group, criterion_main};use http_body_util::{BodyExt, Empty, Full};-use std::time::Instant;-use wasmtime::component::{Component, Linker, ResourceTable};-use wasmtime::{Result, Store};+use std::time::{Duration, Instant};+use wasmtime::component::{Component, InstancePre, Linker, ResourceTable};+use wasmtime::{Engine, Result, Store};+use wasmtime_wasi::p2::pipe::MemoryOutputPipe;+use wasmtime_wasi::p3::bindings::CommandPre;use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};use wasmtime_wasi_http::p3::Request;use wasmtime_wasi_http::p3::bindings::{Service, ServicePre};@@ -14,7 +17,7 @@ use wasmtime_wasi_http::{Error, RequestOptions, WasiBody, WasiHttpCtx, WasiHttpCtxView, WasiHttpHooks, WasiHttpView,};-criterion_group!(benches, bench_p3_http_proxy);+criterion_group!(benches, bench_p3_http_proxy, bench_p3_http_api);criterion_main!(benches);const RESPONSE_BODY: &[u8] = b"hello";@@ -79,7 +82,8 @@ impl WasiHttpView for Ctx {}}-fn bench_p3_http_proxy(c: &mut Criterion) {+/// Compile the component at `path` and prepare it for instantiation.+fn instance_pre(path: &str) -> InstancePre<Ctx> {let _ = env_logger::try_init();let engine = test_programs_artifacts::engine(|config| {@@ -87,14 +91,31 @@ fn bench_p3_http_proxy(c: &mut Criterion) {// Emit symbols for profiling.config.profiler(wasmtime::ProfilingStrategy::PerfMap);});- let component =- Component::from_file(&engine, test_programs_artifacts::P3_HTTP_PROXY_COMPONENT).unwrap();+ let component = Component::from_file(&engine, path).unwrap();let mut linker = Linker::new(&engine);wasmtime_wasi::p2::add_to_linker_async(&mut linker).unwrap();wasmtime_wasi::p3::add_to_linker(&mut linker).unwrap();wasmtime_wasi_http::p3::add_to_linker(&mut linker).unwrap();- let pre = ServicePre::new(linker.instantiate_pre(&component).unwrap()).unwrap();+ linker.instantiate_pre(&component).unwrap()+}++fn new_store(engine: &Engine, wasi: WasiCtx) -> Store<Ctx> {+ Store::new(+ engine,+ Ctx {+ table: ResourceTable::default(),+ wasi,+ http: WasiHttpCtx::new(),+ hooks: FixedResponseHooks,+ },+ )+}+fn bench_p3_http_proxy(c: &mut Criterion) {+ let pre = ServicePre::new(instance_pre(+ test_programs_artifacts::P3_HTTP_PROXY_COMPONENT,+ ))+ .unwrap();let rt = tokio::runtime::Builder::new_current_thread().enable_all().build()@@ -105,15 +126,7 @@ fn bench_p3_http_proxy(c: &mut Criterion) {rt.block_on(async {// Instantiate once per batch, outside of the timed region, and// reuse the same instance for every request in the batch.- let mut store = Store::new(- &engine,- Ctx {- table: ResourceTable::default(),- wasi: WasiCtx::builder().build(),- http: WasiHttpCtx::new(),- hooks: FixedResponseHooks,- },- );+ let mut store = new_store(pre.engine(), WasiCtx::builder().build());let service = pre.instantiate_async(&mut store).await.unwrap();let start = Instant::now();@@ -160,3 +173,38 @@ async fn proxy_one_request(store: &mut Store<Ctx>, service: &Service) -> Result<assert_eq!(body.as_ref(), RESPONSE_BODY);Ok(())}++fn bench_p3_http_api(c: &mut Criterion) {+ let pre = CommandPre::new(instance_pre(+ test_programs_artifacts::P3_HTTP_API_BENCH_COMPONENT,+ ))+ .unwrap();+ let rt = tokio::runtime::Builder::new_current_thread()+ .enable_all()+ .build()+ .unwrap();++ for api in ["request.new", "response.consume-body", "client.send"] {+ c.bench_function(&format!("wasi-http/p3-api/{api}"), |b| {+ b.iter_custom(|iters| rt.block_on(run_api(&pre, api, iters)).unwrap())+ });+ }+}++/// Run the `p3_http_api_bench` guest to call `api` `iters` times, returning+/// the time it reports those calls took.+async fn run_api(pre: &CommandPre<Ctx>, api: &str, iters: u64) -> Result<Duration> {+ let stdout = MemoryOutputPipe::new(1024);+ let wasi = WasiCtx::builder()+ .args(&["p3_http_api_bench", api, &iters.to_string()])+ .stdout(stdout.clone())+ .build();+ let mut store = new_store(pre.engine(), wasi);+ let command = pre.instantiate_async(&mut store).await?;+ store+ .run_concurrent(async |store| command.wasi_cli_run().call_run(store).await)+ .await??+ .map_err(|()| wasmtime::format_err!("`{api}` failed"))?;+ let elapsed = std::str::from_utf8(&stdout.contents())?.trim().parse()?;+ Ok(Duration::from_nanos(elapsed))+}diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rsindex bea271dd79..981cd49249 100644--- a/crates/wasi-http/tests/all/p3/mod.rs+++ b/crates/wasi-http/tests/all/p3/mod.rs@@ -192,6 +192,46 @@ async fn run_cli(path: &str, server: &Server) -> wasmtime::Result<()> {.map_err(|()| format_err!("`wasi:cli/run#run` failed"))}+/// Run the `p3_http_api_bench` guest for a single iteration of each API to+/// make sure it keeps working.+#[test_log::test(tokio::test(flavor = "multi_thread"))]+async fn p3_http_api_bench() -> Result<()> {+ let engine = test_programs_artifacts::engine(|config| {+ config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);+ config.wasm_component_model_async(true);+ });+ let component = Component::from_file(&engine, P3_HTTP_API_BENCH_COMPONENT)?;+ let mut linker = Linker::new(&engine);+ wasmtime_wasi::p2::add_to_linker_async(&mut linker)+ .context("failed to link `wasi:[email protected]`")?;+ wasmtime_wasi::p3::add_to_linker(&mut linker).context("failed to link `wasi:[email protected]`")?;+ wasmtime_wasi_http::p3::add_to_linker(&mut linker)+ .context("failed to link `wasi:[email protected]`")?;++ for api in ["request.new", "response.consume-body", "client.send"] {+ // `TestHooks` only accepts a single request to `p3-test`, so use a+ // fresh store and a single iteration for each API.+ let mut store = Store::new(+ &engine,+ Ctx {+ wasi: WasiCtxBuilder::new()+ .args(&["p3_http_api_bench", api, "1"])+ .inherit_stdio()+ .build(),+ ..Ctx::new(oneshot::channel().0)+ },+ );+ let command = Command::instantiate_async(&mut store, &component, &linker).await?;+ store+ .run_concurrent(async |store| command.wasi_cli_run().call_run(store).await)+ .await+ .context("failed to call `wasi:cli/run#run`")?+ .context("guest trapped")?+ .map_err(|()| format_err!("`{api}` failed"))?;+ }+ Ok(())+}+async fn run_http<E: Into<Error> + 'static>(component_filename: &str,req: http::Request<impl Body<Data = Bytes, Error = E> + Send + Sync + 'static>,