Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit b851b75

Browse files
shawntabriziarkpar
andauthored
Benchmarks Writer CLI (#6567)
* initial mockup * add and wipe * track writes * start to add to pipeline * return all reads/writes * Log reads and writes from bench db * causes panic * Allow multiple commits * commit before ending benchmark * doesn't work??? * fix * Update lib.rs * switch to struct for `BenchmarkResults` * add to output * fix test * line width * @kianenigma review * Add Whitelist to DB Tracking in Benchmarks Pipeline (#6405) * hardcoded whitelist * Add whitelist to pipeline * Remove whitelist pipeline from CLI, add to runtime * clean-up unused db initialized whitelist * Add regression analysis to DB Tracking (#6475) * Add selector * add tests * debug formatter for easy formula * initial idea * use all benchmarks * broken * working without trait * Make work for multiple pallets * Fix merge issues * writer appends to file * implement () for balances weight trait * update name of trait * Weights to WeightInfo * auto trait writer * Heap pages are configurable * clean out runtime changes * more clean up * Fix string generation * Update comments * Update bin/node/runtime/src/lib.rs Co-authored-by: arkpar <[email protected]>
1 parent 18334ee commit b851b75

File tree

8 files changed

+266
-36
lines changed

8 files changed

+266
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/runtime/src/lib.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,6 @@ impl_runtime_apis! {
11181118

11191119
let whitelist: Vec<Vec<u8>> = vec![
11201120
// Block Number
1121-
// frame_system::Number::<Runtime>::hashed_key().to_vec(),
11221121
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec(),
11231122
// Total Issuance
11241123
hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec(),
@@ -1137,25 +1136,25 @@ impl_runtime_apis! {
11371136
let mut batches = Vec::<BenchmarkBatch>::new();
11381137
let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat, &whitelist);
11391138

1140-
add_benchmark!(params, batches, b"babe", Babe);
1141-
add_benchmark!(params, batches, b"balances", Balances);
1142-
add_benchmark!(params, batches, b"collective", Council);
1143-
add_benchmark!(params, batches, b"democracy", Democracy);
1144-
add_benchmark!(params, batches, b"elections", Elections);
1145-
add_benchmark!(params, batches, b"identity", Identity);
1146-
add_benchmark!(params, batches, b"im-online", ImOnline);
1147-
add_benchmark!(params, batches, b"indices", Indices);
1148-
add_benchmark!(params, batches, b"multisig", Multisig);
1149-
add_benchmark!(params, batches, b"offences", OffencesBench::<Runtime>);
1150-
add_benchmark!(params, batches, b"proxy", Proxy);
1151-
add_benchmark!(params, batches, b"scheduler", Scheduler);
1152-
add_benchmark!(params, batches, b"session", SessionBench::<Runtime>);
1153-
add_benchmark!(params, batches, b"staking", Staking);
1154-
add_benchmark!(params, batches, b"system", SystemBench::<Runtime>);
1155-
add_benchmark!(params, batches, b"timestamp", Timestamp);
1156-
add_benchmark!(params, batches, b"treasury", Treasury);
1157-
add_benchmark!(params, batches, b"utility", Utility);
1158-
add_benchmark!(params, batches, b"vesting", Vesting);
1139+
add_benchmark!(params, batches, pallet_babe, Babe);
1140+
add_benchmark!(params, batches, pallet_balances, Balances);
1141+
add_benchmark!(params, batches, pallet_collective, Council);
1142+
add_benchmark!(params, batches, pallet_democracy, Democracy);
1143+
add_benchmark!(params, batches, pallet_elections_phragmen, Elections);
1144+
add_benchmark!(params, batches, pallet_identity, Identity);
1145+
add_benchmark!(params, batches, pallet_im_online, ImOnline);
1146+
add_benchmark!(params, batches, pallet_indices, Indices);
1147+
add_benchmark!(params, batches, pallet_multisig, Multisig);
1148+
add_benchmark!(params, batches, pallet_offences, OffencesBench::<Runtime>);
1149+
add_benchmark!(params, batches, pallet_proxy, Proxy);
1150+
add_benchmark!(params, batches, pallet_scheduler, Scheduler);
1151+
add_benchmark!(params, batches, pallet_session, SessionBench::<Runtime>);
1152+
add_benchmark!(params, batches, pallet_staking, Staking);
1153+
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
1154+
add_benchmark!(params, batches, pallet_timestamp, Timestamp);
1155+
add_benchmark!(params, batches, pallet_treasury, Treasury);
1156+
add_benchmark!(params, batches, pallet_utility, Utility);
1157+
add_benchmark!(params, batches, pallet_vesting, Vesting);
11591158

11601159
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
11611160
Ok(batches)

frame/benchmarking/src/analysis.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use linregress::{FormulaRegressionBuilder, RegressionDataBuilder, RegressionMode
2222
use crate::BenchmarkResults;
2323

2424
pub struct Analysis {
25-
base: u128,
26-
slopes: Vec<u128>,
27-
names: Vec<String>,
28-
value_dists: Option<Vec<(Vec<u32>, u128, u128)>>,
29-
model: Option<RegressionModel>,
25+
pub base: u128,
26+
pub slopes: Vec<u128>,
27+
pub names: Vec<String>,
28+
pub value_dists: Option<Vec<(Vec<u32>, u128, u128)>>,
29+
pub model: Option<RegressionModel>,
3030
}
3131

3232
pub enum BenchmarkSelector {

frame/benchmarking/src/lib.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,31 +1158,46 @@ macro_rules! impl_benchmark_test {
11581158
/// First create an object that holds in the input parameters for the benchmark:
11591159
///
11601160
/// ```ignore
1161-
/// let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat);
1161+
/// let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat, &whitelist);
11621162
/// ```
11631163
///
1164+
/// The `whitelist` is a `Vec<Vec<u8>>` of storage keys that you would like to skip for DB tracking. For example:
1165+
///
1166+
/// ```ignore
1167+
/// let whitelist: Vec<Vec<u8>> = vec![
1168+
/// // Block Number
1169+
/// hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec(),
1170+
/// // Total Issuance
1171+
/// hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec(),
1172+
/// // Execution Phase
1173+
/// hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec(),
1174+
/// // Event Count
1175+
/// hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec(),
1176+
/// ];
1177+
///
11641178
/// Then define a mutable local variable to hold your `BenchmarkBatch` object:
11651179
///
11661180
/// ```ignore
11671181
/// let mut batches = Vec::<BenchmarkBatch>::new();
11681182
/// ````
11691183
///
1170-
/// Then add the pallets you want to benchmark to this object, including the string
1171-
/// you want to use target a particular pallet:
1184+
/// Then add the pallets you want to benchmark to this object, using their crate name and generated
1185+
/// module struct:
11721186
///
11731187
/// ```ignore
1174-
/// add_benchmark!(params, batches, b"balances", Balances);
1175-
/// add_benchmark!(params, batches, b"identity", Identity);
1176-
/// add_benchmark!(params, batches, b"session", SessionBench::<Runtime>);
1188+
/// add_benchmark!(params, batches, pallet_balances, Balances);
1189+
/// add_benchmark!(params, batches, pallet_session, SessionBench::<Runtime>);
1190+
/// add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
11771191
/// ...
11781192
/// ```
11791193
///
11801194
/// At the end of `dispatch_benchmark`, you should return this batches object.
11811195
#[macro_export]
11821196
macro_rules! add_benchmark {
1183-
( $params:ident, $batches:ident, $name:literal, $( $location:tt )* ) => (
1197+
( $params:ident, $batches:ident, $name:ident, $( $location:tt )* ) => (
1198+
let name_string = stringify!($name).as_bytes();
11841199
let (pallet, benchmark, lowest_range_values, highest_range_values, steps, repeat, whitelist) = $params;
1185-
if &pallet[..] == &$name[..] || &pallet[..] == &b"*"[..] {
1200+
if &pallet[..] == &name_string[..] || &pallet[..] == &b"*"[..] {
11861201
if &pallet[..] == &b"*"[..] || &benchmark[..] == &b"*"[..] {
11871202
for benchmark in $( $location )*::benchmarks().into_iter() {
11881203
$batches.push($crate::BenchmarkBatch {
@@ -1194,7 +1209,7 @@ macro_rules! add_benchmark {
11941209
repeat,
11951210
whitelist,
11961211
)?,
1197-
pallet: $name.to_vec(),
1212+
pallet: name_string.to_vec(),
11981213
benchmark: benchmark.to_vec(),
11991214
});
12001215
}
@@ -1208,7 +1223,7 @@ macro_rules! add_benchmark {
12081223
repeat,
12091224
whitelist,
12101225
)?,
1211-
pallet: $name.to_vec(),
1226+
pallet: name_string.to_vec(),
12121227
benchmark: benchmark.clone(),
12131228
});
12141229
}

utils/frame/benchmarking-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ description = "CLI for benchmarking FRAME"
1212
targets = ["x86_64-unknown-linux-gnu"]
1313

1414
[dependencies]
15+
Inflector = "0.11.4"
1516
frame-benchmarking = { version = "2.0.0-rc4", path = "../../../frame/benchmarking" }
1617
sp-core = { version = "2.0.0-rc4", path = "../../../primitives/core" }
1718
sc-service = { version = "0.8.0-rc4", default-features = false, path = "../../../client/service" }

utils/frame/benchmarking-cli/src/command.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl BenchmarkCmd {
5555
let state = BenchmarkingState::<BB>::new(genesis_storage, cache_size)?;
5656
let executor = NativeExecutor::<ExecDispatch>::new(
5757
wasm_method,
58-
None, // heap pages
58+
self.heap_pages,
5959
2, // The runtime instances cache size.
6060
);
6161

@@ -89,6 +89,16 @@ impl BenchmarkCmd {
8989
let results = <std::result::Result<Vec<BenchmarkBatch>, String> as Decode>::decode(&mut &result[..])
9090
.map_err(|e| format!("Failed to decode benchmark results: {:?}", e))?;
9191

92+
if self.output {
93+
if self.weight_trait {
94+
let mut file = crate::writer::open_file("traits.rs")?;
95+
crate::writer::write_trait(&mut file, results.clone())?;
96+
} else {
97+
let mut file = crate::writer::open_file("benchmarks.rs")?;
98+
crate::writer::write_results(&mut file, results.clone())?;
99+
}
100+
}
101+
92102
match results {
93103
Ok(batches) => for batch in batches.into_iter() {
94104
// Print benchmark metadata

utils/frame/benchmarking-cli/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// limitations under the License.
1717

1818
mod command;
19+
mod writer;
1920

2021
use sc_cli::{ExecutionStrategy, WasmExecutionMethod};
2122
use std::fmt::Debug;
@@ -59,6 +60,18 @@ pub struct BenchmarkCmd {
5960
#[structopt(long)]
6061
pub no_min_squares: bool,
6162

63+
/// Output the benchmarks to a Rust file.
64+
#[structopt(long)]
65+
pub output: bool,
66+
67+
/// Output the trait definition to a Rust file.
68+
#[structopt(long)]
69+
pub weight_trait: bool,
70+
71+
/// Set the heap pages while running benchmarks.
72+
#[structopt(long)]
73+
pub heap_pages: Option<u64>,
74+
6275
#[allow(missing_docs)]
6376
#[structopt(flatten)]
6477
pub shared_params: sc_cli::SharedParams,

0 commit comments

Comments
 (0)