Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bencher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ authors = ["Laminar Developers <hello@laminar.one>"]
edition = "2018"

[dependencies]
paste = "1.0"
build-helper = { version = "0.1.1", optional = true }
cargo_metadata = { version = "0.13.1", optional = true }
tempfile = { version = "3.1.0", optional = true }
toml = { version = "0.5.4", optional = true }
walkdir = { version = "2.3.1", optional = true }
ansi_term = { version = "0.12.1", optional = true }
wasm-gc-api = { version = "0.1.11", optional = true }
rand = {version = "0.8.3", optional = true }
linregress = { version = "0.4.0", optional = true }
serde = { version = "1.0.119", optional = true, features = ['derive'] }
serde_json = {version = "1.0.64", optional = true }
Expand All @@ -20,11 +29,20 @@ sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.4", default-features = false, features = ["wasmtime"], optional = true }
sc-executor-common = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.4", optional = true }
sc-client-db = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.4", default-features = false, features = ["with-kvdb-rocksdb"], optional = true }
sp-maybe-compressed-blob = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.4", default-features = false, optional = true }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.4", default-features = false }

[features]
default = ["std"]
std = [
"build-helper",
"cargo_metadata",
"tempfile",
"toml",
"walkdir",
"ansi_term",
"wasm-gc-api",
"rand",
"linregress",
"serde/std",
"serde_json/std",
Expand All @@ -37,5 +55,9 @@ std = [
"sc-executor/std",
"sc-executor-common",
"sc-client-db",
"sp-maybe-compressed-blob",
"frame-benchmarking/std",
]
bench = [
"sp-io/disable_panic_handler"
]
28 changes: 9 additions & 19 deletions bencher/src/bench_runner.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
use frame_benchmarking::{
benchmarking,
frame_support::sp_runtime::traits::{Block, NumberFor},
};
use sc_client_db::BenchmarkingState;
use frame_benchmarking::frame_support::sp_runtime::traits::{Block, NumberFor};
use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutionMethod, WasmExecutor};
use sc_executor_common::runtime_blob::RuntimeBlob;
use sp_io::SubstrateHostFunctions;
use sp_state_machine::{Ext, OverlayedChanges, StorageTransactionCache};

/// Run benches
pub fn run<B: Block>(wasm_code: Vec<u8>) -> Vec<u8> {
pub fn run<B: Block>(wasm_code: Vec<u8>) -> std::result::Result<Vec<u8>, String> {
let mut overlay = OverlayedChanges::default();
let mut cache = StorageTransactionCache::default();
let state = BenchmarkingState::<B>::new(Default::default(), Default::default(), false).unwrap();
let state = sc_client_db::BenchmarkingState::<B>::new(Default::default(), Default::default(), false).unwrap();
let mut ext = Ext::<_, NumberFor<B>, _>::new(&mut overlay, &mut cache, &state, None, None);

let mut host_functions = benchmarking::HostFunctions::host_functions();
host_functions.append(&mut SubstrateHostFunctions::host_functions());
let mut host_functions = sp_io::SubstrateHostFunctions::host_functions();
host_functions.append(&mut frame_benchmarking::benchmarking::HostFunctions::host_functions());
host_functions.append(&mut super::bencher::HostFunctions::host_functions());

let executor = WasmExecutor::new(
WasmExecutionMethod::Compiled,
Expand All @@ -26,13 +22,7 @@ pub fn run<B: Block>(wasm_code: Vec<u8>) -> Vec<u8> {
None,
);

executor
.uncached_call(
RuntimeBlob::uncompress_if_needed(&wasm_code[..]).unwrap(),
&mut ext,
true,
"run_benches",
&[],
)
.unwrap()
let blob = RuntimeBlob::uncompress_if_needed(&wasm_code[..]).unwrap();

executor.uncached_call(blob, &mut ext, true, "run_benches", &[])
}
64 changes: 64 additions & 0 deletions bencher/src/build_wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use rand::{distributions::Alphanumeric, thread_rng, Rng};

pub mod prerequisites;
pub mod wasm_project;

/// Environment variable to disable color output of the wasm build.
const WASM_BUILD_NO_COLOR: &str = "WASM_BUILD_NO_COLOR";

/// Returns `true` when color output is enabled.
pub fn color_output_enabled() -> bool {
std::env::var(WASM_BUILD_NO_COLOR).is_err()
}

pub fn build() -> std::io::Result<Vec<u8>> {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let pkg_name = std::env::var("CARGO_PKG_NAME").unwrap();

let random = thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect::<String>();

let mut out_dir = std::path::PathBuf::from(manifest_dir);
out_dir.push(format!("target/release/build/{}-{}/out", pkg_name, random));

std::env::set_var("OUT_DIR", out_dir.display().to_string());

let mut project_cargo_toml = std::env::current_dir()?;
project_cargo_toml.push("Cargo.toml");

let default_rustflags = "-Clink-arg=--export=__heap_base -C link-arg=--import-memory";
let cargo_cmd = match prerequisites::check() {
Ok(cmd) => cmd,
Err(err_msg) => {
eprintln!("{}", err_msg);
std::process::exit(1);
}
};

let (wasm_binary, bloaty) = wasm_project::create_and_compile(
&project_cargo_toml,
&default_rustflags,
cargo_cmd,
vec!["bench".to_string()],
None,
);

let (wasm_binary, _wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary {
(
wasm_binary.wasm_binary_path_escaped(),
bloaty.wasm_binary_bloaty_path_escaped(),
)
} else {
(
bloaty.wasm_binary_bloaty_path_escaped(),
bloaty.wasm_binary_bloaty_path_escaped(),
)
};

let bytes = std::fs::read(wasm_binary)?;

Ok(bytes.to_vec())
}
Loading