Skip to content

refactor bencher to track storage changes #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f308a0c
refactor bencher to track storage changes
ermalkaleci Jul 26, 2021
acf1edd
rename arguments
ermalkaleci Jul 26, 2021
6223bd9
move bench to separate file
ermalkaleci Aug 20, 2021
66f994e
update mod
ermalkaleci Aug 20, 2021
ad7139f
update weight-gen to read JSON files
ermalkaleci Sep 20, 2021
115c3b6
fix weight-gen stdin read
ermalkaleci Sep 22, 2021
0290695
add comments about storage access info
ermalkaleci Sep 22, 2021
7220ae2
new key tracker and comments about storage access
ermalkaleci Sep 23, 2021
b43d8e8
clippy
ermalkaleci Sep 23, 2021
6d5bd3c
update weight meter
ermalkaleci Sep 24, 2021
39a1863
Update the way we define and run benches. Benches now become part of …
ermalkaleci Sep 27, 2021
ec3e00a
print warning about killing prefix or child storage
ermalkaleci Sep 27, 2021
3a68d8c
Merge remote-tracking branch 'origin/master' into refactor_bencher
ermalkaleci Sep 27, 2021
5755e5b
cleanup
ermalkaleci Sep 27, 2021
39d3286
Merge remote-tracking branch 'origin/master' into refactor_bencher
ermalkaleci Oct 18, 2021
9f682ce
update test
ermalkaleci Oct 18, 2021
de0870d
refactor bench definition
ermalkaleci Oct 20, 2021
d2477d5
make black_box private
ermalkaleci Oct 20, 2021
3437798
update macros
ermalkaleci Oct 20, 2021
47d855f
reset before run bench
ermalkaleci Oct 21, 2021
f05b72b
whitelist keys
ermalkaleci Oct 28, 2021
f164443
whitelist_keys on std does nothing
ermalkaleci Nov 25, 2021
d719dff
cleanup
ermalkaleci Dec 5, 2021
bd6dee2
Merge master into refactor_bencher
ermalkaleci Dec 5, 2021
1b5fe94
update test cargo 2021
ermalkaleci Dec 8, 2021
b848a5e
cleanup
ermalkaleci Dec 8, 2021
fb7e552
add cfg for auto-generated tests
ermalkaleci Dec 8, 2021
fcc97af
update deps
ermalkaleci Dec 8, 2021
5c74351
clippy
ermalkaleci Dec 8, 2021
7f54c84
Merge remote-tracking branch 'origin/master' into refactor_bencher
ermalkaleci Dec 9, 2021
d7603bb
update test
ermalkaleci Dec 9, 2021
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
24 changes: 17 additions & 7 deletions bencher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ authors = ["Laminar Developers <[email protected]>"]
edition = "2021"

[dependencies]
paste = "1.0"
paste = "1.0.6"
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 }
cargo_metadata = { version = "0.14.1", optional = true }
tempfile = { version = "3.2.0", optional = true }
toml = { version = "0.5.8", 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.4", optional = true }
serde = { version = "1.0.119", optional = true, features = ['derive'] }
serde_json = {version = "1.0.64", optional = true }
parking_lot = { version = "0.11.2", optional = true }
serde = { version = "1.0.124", optional = true, features = ['derive'] }
serde_json = {version = "1.0.68", optional = true }
hash-db = { version = "0.15.2", default-features = false, optional = true }
bencher-procedural = { path = "bencher-procedural", default-features = false }
codec = { package = "parity-scale-codec", version = "2.3.1", features = ["derive"], default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
Expand All @@ -31,10 +34,13 @@ sc-executor-common = { git = "https://github.com/paritytech/substrate", branch =
sc-client-db = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false, features = ["with-kvdb-rocksdb"], optional = true }
sp-maybe-compressed-blob = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false, optional = true }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
sp-storage = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false, optional = true }

[features]
default = ["std"]
std = [
"bencher-procedural/std",
"build-helper",
"cargo_metadata",
"tempfile",
Expand All @@ -44,8 +50,10 @@ std = [
"wasm-gc-api",
"rand",
"linregress",
"parking_lot",
"serde/std",
"serde_json/std",
"hash-db/std",
"codec/std",
"sp-core/std",
"sp-std/std",
Expand All @@ -57,7 +65,9 @@ std = [
"sc-client-db",
"sp-maybe-compressed-blob",
"frame-benchmarking/std",
"sp-externalities/std",
"sp-storage/std",
]
bench = [
"sp-io/disable_panic_handler"
"sp-io/disable_panic_handler",
]
18 changes: 18 additions & 0 deletions bencher/bencher-procedural/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "bencher-procedural"
version = "0.1.0"
license = "Apache-2.0"
authors = ["Laminar Developers <[email protected]>"]
edition = "2021"

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.6"
quote = "1.0.3"
syn = { version = "1.0.58", features = ["full"] }

[features]
default = ["std"]
std = []
18 changes: 18 additions & 0 deletions bencher/bencher-procedural/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn benchmarkable(_attr: TokenStream, item: TokenStream) -> TokenStream {
let syn::ItemFn { attrs, vis, sig, block } = syn::parse(item).unwrap();
(quote::quote! {
#(#attrs)*
#vis #sig {
#[cfg(not(feature = "std"))]
::orml_bencher::bench::before_block();
let result = #block;
#[cfg(not(feature = "std"))]
::orml_bencher::bench::after_block();
result
}
})
.into()
}
23 changes: 18 additions & 5 deletions bencher/src/bench_runner.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
use super::{
ext::BenchExt,
tracker::{BenchTracker, BenchTrackerExt},
};
use frame_benchmarking::frame_support::sp_runtime::traits::Block;
use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutionMethod, WasmExecutor};
use sc_executor_common::runtime_blob::RuntimeBlob;
use sp_externalities::Extensions;
use sp_state_machine::{Ext, OverlayedChanges, StorageTransactionCache};
use sp_std::sync::Arc;

/// Run benches
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 =
sc_client_db::BenchmarkingState::<B>::new(Default::default(), Default::default(), false, false).unwrap();
let mut ext = Ext::new(&mut overlay, &mut cache, &state, None);
let state = sc_client_db::BenchmarkingState::<B>::new(Default::default(), Default::default(), false, true).unwrap();

let tracker = Arc::new(BenchTracker::new());
let tracker_ext = BenchTrackerExt(Arc::clone(&tracker));

let mut extensions = Extensions::default();
extensions.register(tracker_ext);

let ext = Ext::<_, _>::new(&mut overlay, &mut cache, &state, Some(&mut extensions));
let mut bench_ext = BenchExt::new(ext, tracker);

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());
host_functions.append(&mut super::bench::HostFunctions::host_functions());

let executor = WasmExecutor::new(
WasmExecutionMethod::Compiled,
Expand All @@ -25,5 +38,5 @@ pub fn run<B: Block>(wasm_code: Vec<u8>) -> std::result::Result<Vec<u8>, String>

let blob = RuntimeBlob::uncompress_if_needed(&wasm_code[..]).unwrap();

executor.uncached_call(blob, &mut ext, true, "run_benches", &[])
executor.uncached_call(blob, &mut bench_ext, false, "run_benches", &[])
}
86 changes: 86 additions & 0 deletions bencher/src/bencher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use codec::{Decode, Encode};
use sp_std::prelude::Vec;

#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct BenchResult {
pub method: Vec<u8>,
pub elapses: Vec<u128>,
pub reads: u32,
pub writes: u32,
pub keys: Vec<u8>,
}

impl BenchResult {
pub fn with_name(name: &str) -> Self {
Self {
method: name.as_bytes().to_vec(),
..Default::default()
}
}
}

#[derive(Default)]
pub struct Bencher {
pub current: BenchResult,
pub results: Vec<BenchResult>,
}

#[inline]
fn black_box<T>(dummy: T) -> T {
let ret = unsafe { sp_std::ptr::read_volatile(&dummy) };
sp_std::mem::forget(dummy);
ret
}

#[allow(unused_variables, clippy::let_and_return)]
impl Bencher {
pub fn whitelist(&mut self, key: Vec<u8>, read: bool, write: bool) {
#[cfg(not(feature = "std"))]
crate::bench::whitelist(key, read, write);
}

pub fn prepare(&self) {
#[cfg(not(feature = "std"))]
{
frame_benchmarking::benchmarking::commit_db();
frame_benchmarking::benchmarking::wipe_db();
}
}

pub fn bench<T, F>(&mut self, mut inner: F) -> T
where
F: FnMut() -> T,
{
#[cfg(not(feature = "std"))]
{
frame_benchmarking::benchmarking::commit_db();
frame_benchmarking::benchmarking::reset_read_write_count();
crate::bench::prepare();
crate::bench::instant();
}

let ret = black_box(inner());

#[cfg(not(feature = "std"))]
{
let elapsed = crate::bench::elapsed().saturating_sub(crate::bench::redundant_time());
self.current.elapses.push(elapsed);

frame_benchmarking::benchmarking::commit_db();
let (reads, _, written, _) = frame_benchmarking::benchmarking::read_write_count();

self.current.reads = reads;
self.current.writes = written;

// changed keys
self.current.keys = crate::bench::read_written_keys();
}

ret
}

pub fn print_warnings(&self, name: &str) {
#[cfg(not(feature = "std"))]
crate::bench::print_warnings(name.encode());
}
}
Loading