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.
This commit is contained in:
Ivan Boldyrev 2022-05-12 19:29:08 +07:00
parent da95089513
commit dcfa51c756
6 changed files with 110 additions and 15 deletions

11
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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<String>) {
self.current_peer_id = current_peer_id.into();
}
}
fn prepare_args(

View File

@ -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"

View File

@ -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<String>,
) -> 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<object_pool::Pool<AVMRunner>> = OnceCell::new();
let runner = AVMRunner::new(
air_wasm_path,
current_peer_id,
Some(AVM_MAX_HEAP_SIZE),
logging_mask,
let pool = POOL_CELL.get_or_init(|| {
object_pool::Pool::new(
// we create an empty pool and let it fill on demand
0,
|| unreachable!(),
)
.expect("vm should be created");
});
let mut runner = pool.pull(make_pooled_avm_runner);
runner.set_peer_id(current_peer_id);
TestRunner {
runner,
call_service,