Skip to content

Commit 5792fd2

Browse files
authored
some bencher improvements (#758)
* some bencher improvements * fmt * clippy
1 parent 0217ab5 commit 5792fd2

File tree

11 files changed

+147
-144
lines changed

11 files changed

+147
-144
lines changed

bencher/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ wasm-gc-api = { version = "0.1.11", optional = true }
1919
rand = {version = "0.8.3", optional = true }
2020
linregress = { version = "0.4.4", optional = true }
2121
parking_lot = { version = "0.12.0", optional = true }
22+
thiserror = { version = "1.0", optional = true }
2223
serde = { version = "1.0.136", optional = true, features = ['derive'] }
2324
serde_json = {version = "1.0.68", optional = true }
2425
hash-db = { version = "0.15.2", default-features = false, optional = true }
@@ -52,6 +53,7 @@ std = [
5253
"rand",
5354
"linregress",
5455
"parking_lot",
56+
"thiserror",
5557
"serde/std",
5658
"serde_json/std",
5759
"hash-db/std",
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,57 +37,57 @@ where
3737
}
3838

3939
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
40-
self.tracker.reading_key(key.to_vec());
40+
self.tracker.on_read_storage(key.to_vec());
4141
self.ext.storage(key)
4242
}
4343

4444
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
45-
self.tracker.reading_key(key.to_vec());
45+
self.tracker.on_read_storage(key.to_vec());
4646
self.ext.storage_hash(key)
4747
}
4848

4949
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
50-
self.tracker.reading_child_key(child_info, key.to_vec());
50+
self.tracker.on_read_child_storage(child_info, key.to_vec());
5151
self.ext.child_storage_hash(child_info, key)
5252
}
5353

5454
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
55-
self.tracker.reading_child_key(child_info, key.to_vec());
55+
self.tracker.on_read_child_storage(child_info, key.to_vec());
5656
self.ext.child_storage(child_info, key)
5757
}
5858

5959
fn next_storage_key(&self, key: &[u8]) -> Option<Vec<u8>> {
60-
self.tracker.reading_key(key.to_vec());
60+
self.tracker.on_read_storage(key.to_vec());
6161
self.ext.next_storage_key(key)
6262
}
6363

6464
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
65-
self.tracker.reading_child_key(child_info, key.to_vec());
65+
self.tracker.on_read_child_storage(child_info, key.to_vec());
6666
self.ext.next_child_storage_key(child_info, key)
6767
}
6868

6969
fn kill_child_storage(&mut self, child_info: &ChildInfo, limit: Option<u32>) -> (bool, u32) {
70-
self.tracker.warn_child_prefix_removal();
70+
self.tracker.on_kill_child_storage(child_info, limit);
7171
self.ext.kill_child_storage(child_info, limit)
7272
}
7373

7474
fn clear_prefix(&mut self, prefix: &[u8], limit: Option<u32>) -> (bool, u32) {
75-
self.tracker.warn_child_prefix_removal();
75+
self.tracker.on_clear_prefix(prefix, limit);
7676
self.ext.clear_prefix(prefix, limit)
7777
}
7878

7979
fn clear_child_prefix(&mut self, child_info: &ChildInfo, prefix: &[u8], limit: Option<u32>) -> (bool, u32) {
80-
self.tracker.warn_child_prefix_removal();
80+
self.tracker.on_clear_child_prefix(child_info, prefix, limit);
8181
self.ext.clear_child_prefix(child_info, prefix, limit)
8282
}
8383

8484
fn place_storage(&mut self, key: Vec<u8>, value: Option<Vec<u8>>) {
85-
self.tracker.changing_key(key.clone());
85+
self.tracker.on_update_storage(key.clone());
8686
self.ext.place_storage(key, value);
8787
}
8888

8989
fn place_child_storage(&mut self, child_info: &ChildInfo, key: Vec<u8>, value: Option<Vec<u8>>) {
90-
self.tracker.changing_child_key(child_info, key.clone());
90+
self.tracker.on_update_child_storage(child_info, key.clone());
9191
self.ext.place_child_storage(child_info, key, value);
9292
}
9393

@@ -100,7 +100,8 @@ where
100100
}
101101

102102
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
103-
self.tracker.changing_key(key.clone());
103+
self.tracker.on_read_storage(key.clone());
104+
self.tracker.on_update_storage(key.clone());
104105
self.ext.storage_append(key, value);
105106
}
106107

bencher/src/bench_runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
ext::BenchExt,
2+
bench_ext::BenchExt,
33
tracker::{BenchTracker, BenchTrackerExt},
44
};
55
use frame_benchmarking::frame_support::sp_runtime::traits::Block;

bencher/src/bencher.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use sp_std::prelude::Vec;
55
pub struct BenchResult {
66
pub method: Vec<u8>,
77
pub elapses: Vec<u128>,
8-
pub reads: u32,
9-
pub writes: u32,
108
pub keys: Vec<u8>,
119
}
1210

@@ -39,7 +37,7 @@ impl Bencher {
3937
crate::bench::whitelist(key, read, write);
4038
}
4139

42-
pub fn prepare(&self) {
40+
pub fn before_run(&self) {
4341
#[cfg(not(feature = "std"))]
4442
{
4543
frame_benchmarking::benchmarking::commit_db();
@@ -55,22 +53,17 @@ impl Bencher {
5553
{
5654
frame_benchmarking::benchmarking::commit_db();
5755
frame_benchmarking::benchmarking::reset_read_write_count();
58-
crate::bench::prepare();
59-
crate::bench::instant();
56+
crate::bench::start_timer();
6057
}
6158

6259
let ret = black_box(inner());
6360

6461
#[cfg(not(feature = "std"))]
6562
{
66-
let elapsed = crate::bench::elapsed().saturating_sub(crate::bench::redundant_time());
63+
let elapsed = crate::bench::end_timer().saturating_sub(crate::bench::redundant_time());
6764
self.current.elapses.push(elapsed);
6865

6966
frame_benchmarking::benchmarking::commit_db();
70-
let (reads, _, written, _) = frame_benchmarking::benchmarking::read_write_count();
71-
72-
self.current.reads = reads;
73-
self.current.writes = written;
7467

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

bencher/src/handler.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use codec::Decode;
66
use frame_benchmarking::frame_support::traits::StorageInfo;
77
use linregress::{FormulaRegressionBuilder, RegressionDataBuilder};
88
use serde::{Deserialize, Serialize};
9+
use sp_core::hexdisplay::HexDisplay;
910
use std::io::Write;
1011
use std::time::Duration;
1112

@@ -46,34 +47,38 @@ pub fn handle(output: Vec<u8>, storage_infos: Vec<StorageInfo>) {
4647
let mut total_writes = 0u32;
4748
let mut comments = Vec::<String>::new();
4849
let keys = <Vec<(Vec<u8>, u32, u32)> as Decode>::decode(&mut &result.keys[..]).unwrap();
49-
keys.iter().for_each(|(prefix, reads, writes)| {
50+
keys.into_iter().for_each(|(prefix, reads, writes)| {
5051
total_reads += reads;
5152
total_writes += writes;
52-
if let Some(info) = storage_infos.iter().find(|x| x.prefix.eq(prefix)) {
53+
if let Some(info) = storage_infos.iter().find(|x| x.prefix.eq(&prefix)) {
5354
let pallet = String::from_utf8(info.pallet_name.clone()).unwrap();
5455
let name = String::from_utf8(info.storage_name.clone()).unwrap();
55-
let comment = format!("{}::{} (r: {}, w: {})", pallet, name, reads, writes);
56-
comments.push(comment);
56+
comments.push(format!("{}::{} (r: {}, w: {})", pallet, name, reads, writes));
5757
} else {
58-
let comment = format!("Unknown (r: {}, w: {})", reads, writes);
59-
comments.push(comment);
58+
comments.push(format!(
59+
"Unknown 0x{} (r: {}, w: {})",
60+
HexDisplay::from(&prefix),
61+
reads,
62+
writes
63+
));
6064
}
6165
});
6266

67+
comments.sort();
68+
6369
println!(
64-
"{} {:<40} {:>20} {:<20} {:<20}",
70+
"{} {:<40} {:>20} storage: {:<20}",
6571
green_bold("Bench"),
6672
cyan(&name),
6773
green_bold(&format!(
6874
"{:?}",
6975
Duration::from_nanos(model.parameters.intercept_value as u64)
7076
)),
7177
green_bold(&format!(
72-
"tracked: [r: {}, w: {}]",
78+
"[r: {}, w: {}]",
7379
&total_reads.to_string(),
7480
&total_writes.to_string()
7581
)),
76-
green_bold(&format!("total: [r: {}, w: {}]", result.reads, result.writes))
7782
);
7883

7984
BenchData {

bencher/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ pub mod build_wasm;
2626
pub mod handler;
2727

2828
#[cfg(feature = "std")]
29-
mod colorize;
29+
mod bench_ext;
3030
#[cfg(feature = "std")]
31-
mod ext;
31+
mod colorize;
3232
#[cfg(feature = "std")]
3333
mod tracker;
3434

bencher/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
/// use crate::mock::*;
3636
///
3737
/// fn foo(b: &mut Bencher) {
38-
/// // Run any before code here
38+
/// // Run anything before code here
3939
/// let ret = b.bench(|| {
4040
/// // foo must have macro `[orml_weight_meter::weight(..)]` to measure correct redundant info
4141
/// YourModule::foo()
4242
/// });
43-
/// // Run any after code here
43+
/// // Run anything after code here
4444
/// }
4545
///
4646
/// fn bar(b: &mut Bencher) {
@@ -71,13 +71,13 @@ macro_rules! benches {
7171
$crate::bench::print_info("\nRunning benches ...\n".as_bytes().to_vec());
7272
let mut bencher = $crate::Bencher::default();
7373
$(
74-
$crate::bench::reset();
74+
$crate::bench::init_bench();
7575

7676
let name = stringify!($method);
7777
bencher.current = $crate::BenchResult::with_name(name);
7878

7979
for _ in 0..1_000 {
80-
bencher.prepare();
80+
bencher.before_run();
8181
$method(&mut bencher);
8282
}
8383

0 commit comments

Comments
 (0)