Skip to content

Commit bc8efb4

Browse files
daanschuttegrishasobol
authored andcommitted
Additional benchmark-storage flags (paritytech#11004)
* Fix typos * Enable overwriting handlebars template * Optionally name json output or disable json altogether * Don't write to json by default * Include block id in handlebars output * Include warmups for write benchmarks * PR comments * Drop unnecessary file extension * Use more appropriate types * Use more appropriate error message * More use of more appropriate types * Rework write benchmark warmups * Run same benchmark for both read and write
1 parent 5fb2cae commit bc8efb4

File tree

6 files changed

+93
-30
lines changed

6 files changed

+93
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub struct BenchmarkCmd {
6767
#[clap(long = "json")]
6868
pub json_output: bool,
6969

70-
/// Write the raw results in JSON format into the give file.
70+
/// Write the raw results in JSON format into the given file.
7171
#[clap(long, conflicts_with = "json-output")]
7272
pub json_file: Option<PathBuf>,
7373

utils/frame/benchmarking-cli/src/storage/cmd.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use sc_client_api::{Backend as ClientBackend, StorageProvider, UsageProvider};
2020
use sc_client_db::DbHash;
2121
use sc_service::Configuration;
2222
use sp_blockchain::HeaderBackend;
23+
use sp_core::storage::StorageKey;
2324
use sp_database::{ColumnId, Database};
2425
use sp_runtime::traits::{Block as BlockT, HashFor};
2526
use sp_state_machine::Storage;
@@ -29,7 +30,8 @@ use clap::{Args, Parser};
2930
use log::info;
3031
use rand::prelude::*;
3132
use serde::Serialize;
32-
use std::{fmt::Debug, sync::Arc};
33+
use sp_runtime::generic::BlockId;
34+
use std::{fmt::Debug, path::PathBuf, sync::Arc};
3335

3436
use super::{record::StatSelect, template::TemplateData};
3537

@@ -58,8 +60,8 @@ pub struct StorageCmd {
5860
pub struct StorageParams {
5961
/// Path to write the *weight* file to. Can be a file or directory.
6062
/// For substrate this should be `frame/support/src/weights`.
61-
#[clap(long, default_value = ".")]
62-
pub weight_path: String,
63+
#[clap(long)]
64+
pub weight_path: Option<PathBuf>,
6365

6466
/// Select a specific metric to calculate the final weight output.
6567
#[clap(long = "metric", default_value = "average")]
@@ -83,8 +85,19 @@ pub struct StorageParams {
8385
#[clap(long)]
8486
pub skip_write: bool,
8587

88+
/// Specify the Handlebars template to use for outputting benchmark results.
89+
#[clap(long)]
90+
pub template_path: Option<PathBuf>,
91+
92+
/// Path to write the raw 'read' results in JSON format to. Can be a file or directory.
93+
#[clap(long)]
94+
pub json_read_path: Option<PathBuf>,
95+
96+
/// Path to write the raw 'write' results in JSON format to. Can be a file or directory.
97+
#[clap(long)]
98+
pub json_write_path: Option<PathBuf>,
99+
86100
/// Rounds of warmups before measuring.
87-
/// Only supported for `read` benchmarks.
88101
#[clap(long, default_value = "1")]
89102
pub warmups: u32,
90103

@@ -115,23 +128,32 @@ impl StorageCmd {
115128
{
116129
let mut template = TemplateData::new(&cfg, &self.params);
117130

131+
let block_id = BlockId::<Block>::Number(client.usage_info().chain.best_number);
132+
template.set_block_number(block_id.to_string());
133+
118134
if !self.params.skip_read {
135+
self.bench_warmup(&client)?;
119136
let record = self.bench_read(client.clone())?;
120-
record.save_json(&cfg, "read")?;
137+
if let Some(path) = &self.params.json_read_path {
138+
record.save_json(&cfg, path, "read")?;
139+
}
121140
let stats = record.calculate_stats()?;
122141
info!("Time summary [ns]:\n{:?}\nValue size summary:\n{:?}", stats.0, stats.1);
123142
template.set_stats(Some(stats), None)?;
124143
}
125144

126145
if !self.params.skip_write {
146+
self.bench_warmup(&client)?;
127147
let record = self.bench_write(client, db, storage)?;
128-
record.save_json(&cfg, "write")?;
148+
if let Some(path) = &self.params.json_write_path {
149+
record.save_json(&cfg, path, "write")?;
150+
}
129151
let stats = record.calculate_stats()?;
130152
info!("Time summary [ns]:\n{:?}\nValue size summary:\n{:?}", stats.0, stats.1);
131153
template.set_stats(None, Some(stats))?;
132154
}
133155

134-
template.write(&self.params.weight_path)
156+
template.write(&self.params.weight_path, &self.params.template_path)
135157
}
136158

137159
/// Returns the specified state version.
@@ -149,6 +171,33 @@ impl StorageCmd {
149171
info!("Using seed {}", seed);
150172
StdRng::seed_from_u64(seed)
151173
}
174+
175+
/// Run some rounds of the (read) benchmark as warmup.
176+
/// See `frame_benchmarking_cli::storage::read::bench_read` for detailed comments.
177+
fn bench_warmup<B, BA, C>(&self, client: &Arc<C>) -> Result<()>
178+
where
179+
C: UsageProvider<B> + StorageProvider<B, BA>,
180+
B: BlockT + Debug,
181+
BA: ClientBackend<B>,
182+
{
183+
let block = BlockId::Number(client.usage_info().chain.best_number);
184+
let empty_prefix = StorageKey(Vec::new());
185+
let mut keys = client.storage_keys(&block, &empty_prefix)?;
186+
let mut rng = Self::setup_rng();
187+
keys.shuffle(&mut rng);
188+
189+
for i in 0..self.params.warmups {
190+
info!("Warmup round {}/{}", i + 1, self.params.warmups);
191+
for key in keys.clone() {
192+
let _ = client
193+
.storage(&block, &key)
194+
.expect("Checked above to exist")
195+
.ok_or("Value unexpectedly empty");
196+
}
197+
}
198+
199+
Ok(())
200+
}
152201
}
153202

154203
// Boilerplate

utils/frame/benchmarking-cli/src/storage/read.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ impl StorageCmd {
4949
let mut rng = Self::setup_rng();
5050
keys.shuffle(&mut rng);
5151

52-
// Run some rounds of the benchmark as warmup.
53-
for i in 0..self.params.warmups {
54-
info!("Warmup round {}/{}", i + 1, self.params.warmups);
55-
for key in keys.clone() {
56-
let _ = client
57-
.storage(&block, &key)
58-
.expect("Checked above to exist")
59-
.ok_or("Value unexpectedly empty")?;
60-
}
61-
}
62-
6352
// Interesting part here:
6453
// Read all the keys in the database and measure the time it takes to access each.
6554
info!("Reading {} keys", keys.len());

utils/frame/benchmarking-cli/src/storage/record.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use sc_service::Configuration;
2222

2323
use log::info;
2424
use serde::Serialize;
25-
use std::{fmt, fs, result, str::FromStr, time::Duration};
25+
use std::{fmt, fs, path::PathBuf, result, str::FromStr, time::Duration};
2626

2727
/// Raw output of a Storage benchmark.
2828
#[derive(Debug, Default, Clone, Serialize)]
@@ -95,12 +95,18 @@ impl BenchRecord {
9595
Ok((time, size)) // The swap of time/size here is intentional.
9696
}
9797

98-
/// Saves the raw results in a json file in the current directory.
98+
/// Unless a path is specified, saves the raw results in a json file in the current directory.
9999
/// Prefixes it with the DB name and suffixed with `path_suffix`.
100-
pub fn save_json(&self, cfg: &Configuration, path_suffix: &str) -> Result<()> {
101-
let path = format!("{}_{}.json", cfg.database, path_suffix).to_lowercase();
100+
pub fn save_json(&self, cfg: &Configuration, out_path: &PathBuf, suffix: &str) -> Result<()> {
101+
let mut path = PathBuf::from(out_path);
102+
if path.is_dir() || path.as_os_str().is_empty() {
103+
path.push(&format!("{}_{}", cfg.database, suffix).to_lowercase());
104+
path.set_extension("json");
105+
}
106+
102107
let json = serde_json::to_string_pretty(&self)
103108
.map_err(|e| format!("Serializing as JSON: {:?}", e))?;
109+
104110
fs::write(&path, json)?;
105111
info!("Raw data written to {:?}", fs::canonicalize(&path)?);
106112
Ok(())

utils/frame/benchmarking-cli/src/storage/template.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ static TEMPLATE: &str = include_str!("./weights.hbs");
3232
pub(crate) struct TemplateData {
3333
/// Name of the database used.
3434
db_name: String,
35+
/// Block number that was used.
36+
block_number: String,
3537
/// Name of the runtime. Taken from the chain spec.
3638
runtime_name: String,
3739
/// Version of the benchmarking CLI used.
@@ -85,28 +87,44 @@ impl TemplateData {
8587
Ok(())
8688
}
8789

88-
/// Filles out the `weights.hbs` HBS template with its own data.
90+
/// Sets the block id that was used.
91+
pub fn set_block_number(&mut self, block_number: String) {
92+
self.block_number = block_number
93+
}
94+
95+
/// Fills out the `weights.hbs` or specified HBS template with its own data.
8996
/// Writes the result to `path` which can be a directory or file.
90-
pub fn write(&self, path: &str) -> Result<()> {
97+
pub fn write(&self, path: &Option<PathBuf>, hbs_template: &Option<PathBuf>) -> Result<()> {
9198
let mut handlebars = handlebars::Handlebars::new();
9299
// Format large integers with underscore.
93100
handlebars.register_helper("underscore", Box::new(crate::writer::UnderscoreHelper));
94101
// Don't HTML escape any characters.
95102
handlebars.register_escape_fn(|s| -> String { s.to_string() });
103+
// Use custom template if provided.
104+
let template = match hbs_template {
105+
Some(template) if template.is_file() => fs::read_to_string(template)?,
106+
Some(_) => return Err("Handlebars template is not a valid file!".into()),
107+
None => TEMPLATE.to_string(),
108+
};
96109

97110
let out_path = self.build_path(path);
98111
let mut fd = fs::File::create(&out_path)?;
99112
info!("Writing weights to {:?}", fs::canonicalize(&out_path)?);
113+
100114
handlebars
101-
.render_template_to_write(&TEMPLATE, &self, &mut fd)
115+
.render_template_to_write(&template, &self, &mut fd)
102116
.map_err(|e| format!("HBS template write: {:?}", e).into())
103117
}
104118

105119
/// Builds a path for the weight file.
106-
fn build_path(&self, weight_out: &str) -> PathBuf {
107-
let mut path = PathBuf::from(weight_out);
108-
if path.is_dir() {
109-
path.push(format!("{}_weights.rs", self.db_name.to_lowercase()));
120+
fn build_path(&self, weight_out: &Option<PathBuf>) -> PathBuf {
121+
let mut path = match weight_out {
122+
Some(p) => PathBuf::from(p),
123+
None => PathBuf::new(),
124+
};
125+
126+
if path.is_dir() || path.as_os_str().is_empty() {
127+
path.push(format!("{}_weights", self.db_name.to_lowercase()));
110128
path.set_extension("rs");
111129
}
112130
path

utils/frame/benchmarking-cli/src/storage/weights.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! DATE: {{date}}
2020
//!
2121
//! DATABASE: `{{db_name}}`, RUNTIME: `{{runtime_name}}`
22+
//! BLOCK-NUM: `{{block_number}}`
2223
//! SKIP-WRITE: `{{params.skip_write}}`, SKIP-READ: `{{params.skip_read}}`, WARMUPS: `{{params.warmups}}`
2324
//! STATE-VERSION: `V{{params.state_version}}`, STATE-CACHE-SIZE: `{{params.state_cache_size}}`
2425
//! WEIGHT-PATH: `{{params.weight_path}}`

0 commit comments

Comments
 (0)