From dcfa51c756d66c76ddc1692a6e90a70138814002 Mon Sep 17 00:00:00 2001 From: Ivan Boldyrev Date: Thu, 12 May 2022 19:29:08 +0700 Subject: [PATCH] Use pooled AVMRunner for tests Use pooled `avm::server::AVMRunner` instances of air_iterpreter_server.wasm to reduce tests' running time. It avoids repeated WASM loading and compilation. On my hardware, `cargo test --release` execution time (precompiled) decreases from almost 6 minutes to 1.5 minutes. --- Cargo.lock | 11 ++++++ .../auth_module/Cargo.lock | 34 +++++++++++++++- .../log_storage/Cargo.lock | 34 +++++++++++++++- avm/server/src/runner.rs | 5 +++ crates/air-lib/test-utils/Cargo.toml | 2 + crates/air-lib/test-utils/src/test_runner.rs | 39 +++++++++++++------ 6 files changed, 110 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4117d2ee..f6c25d12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,8 @@ dependencies = [ "avm-server", "fstrings", "marine-rs-sdk", + "object-pool", + "once_cell", "serde_json", ] @@ -1494,6 +1496,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object-pool" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee9a3e7196d09ec86002b939f1576e8e446d58def8fd48fe578e2c72d5328d68" +dependencies = [ + "parking_lot 0.11.2", +] + [[package]] name = "once_cell" version = "1.10.0" diff --git a/air/tests/test_module/features/tetraplets/security_tetraplets/auth_module/Cargo.lock b/air/tests/test_module/features/tetraplets/security_tetraplets/auth_module/Cargo.lock index 1fb5d73a..88d92051 100644 --- a/air/tests/test_module/features/tetraplets/security_tetraplets/auth_module/Cargo.lock +++ b/air/tests/test_module/features/tetraplets/security_tetraplets/auth_module/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "air" -version = "0.21.0" +version = "0.24.0" dependencies = [ "air-execution-info-collector", "air-interpreter-data", @@ -25,9 +25,11 @@ dependencies = [ "air-trace-handler", "boolinator", "concat-idents", + "fstrings", "log", "maplit", "marine-rs-sdk", + "non-empty-vec", "polyplets 0.2.0", "serde", "serde_json", @@ -53,7 +55,7 @@ dependencies = [ [[package]] name = "air-interpreter-interface" -version = "0.8.0" +version = "0.10.0" dependencies = [ "fluence-it-types", "marine-rs-sdk", @@ -306,6 +308,28 @@ dependencies = [ "serde", ] +[[package]] +name = "fstrings" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7845a0f15da505ac36baad0486612dab57f8b8d34e19c5470a265bbcdd572ae6" +dependencies = [ + "fstrings-proc-macro", + "proc-macro-hack", +] + +[[package]] +name = "fstrings-proc-macro" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b58c0e7581dc33478a32299182cbe5ae3b8c028be26728a47fb0a113c92d9d" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "getrandom" version = "0.2.2" @@ -594,6 +618,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "1.0.26" diff --git a/air/tests/test_module/features/tetraplets/security_tetraplets/log_storage/Cargo.lock b/air/tests/test_module/features/tetraplets/security_tetraplets/log_storage/Cargo.lock index e5e6d3c6..10d7bb6a 100644 --- a/air/tests/test_module/features/tetraplets/security_tetraplets/log_storage/Cargo.lock +++ b/air/tests/test_module/features/tetraplets/security_tetraplets/log_storage/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "air" -version = "0.21.0" +version = "0.24.0" dependencies = [ "air-execution-info-collector", "air-interpreter-data", @@ -25,9 +25,11 @@ dependencies = [ "air-trace-handler", "boolinator", "concat-idents", + "fstrings", "log", "maplit", "marine-rs-sdk", + "non-empty-vec", "polyplets 0.2.0", "serde", "serde_json", @@ -53,7 +55,7 @@ dependencies = [ [[package]] name = "air-interpreter-interface" -version = "0.8.0" +version = "0.10.0" dependencies = [ "fluence-it-types", "marine-rs-sdk", @@ -298,6 +300,28 @@ dependencies = [ "serde", ] +[[package]] +name = "fstrings" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7845a0f15da505ac36baad0486612dab57f8b8d34e19c5470a265bbcdd572ae6" +dependencies = [ + "fstrings-proc-macro", + "proc-macro-hack", +] + +[[package]] +name = "fstrings-proc-macro" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b58c0e7581dc33478a32299182cbe5ae3b8c028be26728a47fb0a113c92d9d" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "getrandom" version = "0.2.2" @@ -594,6 +618,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "1.0.26" diff --git a/avm/server/src/runner.rs b/avm/server/src/runner.rs index 116bb162..46d7df64 100644 --- a/avm/server/src/runner.rs +++ b/avm/server/src/runner.rs @@ -110,6 +110,11 @@ impl AVMRunner { max_memory_size: stats[0].max_memory_size, } } + + #[inline] + pub fn set_peer_id(&mut self, current_peer_id: impl Into) { + self.current_peer_id = current_peer_id.into(); + } } fn prepare_args( diff --git a/crates/air-lib/test-utils/Cargo.toml b/crates/air-lib/test-utils/Cargo.toml index b618a37c..8fd95877 100644 --- a/crates/air-lib/test-utils/Cargo.toml +++ b/crates/air-lib/test-utils/Cargo.toml @@ -20,4 +20,6 @@ avm-server = { path = "../../../avm/server" } marine-rs-sdk = "0.6.15" fstrings = "0.2.3" +object-pool = "0.5.4" +once_cell = "1.10.0" serde_json = "1.0.61" diff --git a/crates/air-lib/test-utils/src/test_runner.rs b/crates/air-lib/test-utils/src/test_runner.rs index 2c4bc7cb..e9b33f47 100644 --- a/crates/air-lib/test-utils/src/test_runner.rs +++ b/crates/air-lib/test-utils/src/test_runner.rs @@ -17,18 +17,33 @@ use super::CallServiceClosure; use avm_server::avm_runner::*; +use once_cell::sync::OnceCell; use std::collections::HashMap; use std::collections::HashSet; use std::path::PathBuf; // 10 Mb const AVM_MAX_HEAP_SIZE: u64 = 10 * 1024 * 1024; +const AIR_WASM_PATH: &str = "../target/wasm32-wasi/debug/air_interpreter_server.wasm"; pub struct TestRunner { - pub runner: AVMRunner, + pub runner: object_pool::Reusable<'static, AVMRunner>, pub call_service: CallServiceClosure, } +fn make_pooled_avm_runner() -> AVMRunner { + let fake_current_peer_id = ""; + let logging_mask = i32::MAX; + + AVMRunner::new( + PathBuf::from(AIR_WASM_PATH), + fake_current_peer_id, + Some(AVM_MAX_HEAP_SIZE), + logging_mask, + ) + .expect("vm should be created") +} + #[derive(Debug, Default, Clone)] pub struct TestRunParameters { pub init_peer_id: String, @@ -97,17 +112,19 @@ pub fn create_avm( call_service: CallServiceClosure, current_peer_id: impl Into, ) -> TestRunner { - let air_wasm_path = PathBuf::from("../target/wasm32-wasi/debug/air_interpreter_server.wasm"); - let current_peer_id = current_peer_id.into(); - let logging_mask = i32::MAX; + static POOL_CELL: OnceCell> = OnceCell::new(); + + let pool = POOL_CELL.get_or_init(|| { + object_pool::Pool::new( + // we create an empty pool and let it fill on demand + 0, + || unreachable!(), + ) + }); + + let mut runner = pool.pull(make_pooled_avm_runner); + runner.set_peer_id(current_peer_id); - let runner = AVMRunner::new( - air_wasm_path, - current_peer_id, - Some(AVM_MAX_HEAP_SIZE), - logging_mask, - ) - .expect("vm should be created"); TestRunner { runner, call_service,