Skip to content
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

add cli param keys_limit #7

Merged
merged 2 commits into from
Apr 24, 2024
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
20 changes: 18 additions & 2 deletions substrate/utils/frame/benchmarking-cli/src/storage/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ pub struct StorageParams {
/// Include child trees in benchmark.
#[arg(long)]
pub include_child_trees: bool,

/// Maximum number of keys to read
/// (All keys if not define)
#[arg(long)]
pub keys_limit: Option<usize>,

/// Seed to use for benchs randomness, the same seed allow to replay
/// becnhamrks under the same conditions.
#[arg(long)]
pub random_seed: Option<u64>,
}

impl StorageCmd {
Expand Down Expand Up @@ -191,8 +201,14 @@ impl StorageCmd {
BA: ClientBackend<B>,
{
let hash = client.usage_info().chain.best_hash;
let mut keys: Vec<_> = client.storage_keys(hash, None, None)?.collect();
let (mut rng, _) = new_rng(None);
let mut keys: Vec<_> = if let Some(keys_limit) = self.params.keys_limit {
use sp_core::blake2_256;
let first_key = self.params.random_seed.map(|seed| sp_storage::StorageKey(blake2_256(&seed.to_be_bytes()[..]).to_vec()));
client.storage_keys(hash, None, first_key.as_ref())?.take(keys_limit).collect()
} else {
client.storage_keys(hash, None, None)?.collect()
};
let (mut rng, _) = new_rng(self.params.random_seed);
keys.shuffle(&mut rng);

for i in 0..self.params.warmups {
Expand Down
12 changes: 9 additions & 3 deletions substrate/utils/frame/benchmarking-cli/src/storage/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ impl StorageCmd {
let best_hash = client.usage_info().chain.best_hash;

info!("Preparing keys from block {}", best_hash);
// Load all keys and randomly shuffle them.
let mut keys: Vec<_> = client.storage_keys(best_hash, None, None)?.collect();
let (mut rng, _) = new_rng(None);
// Load keys and randomly shuffle them.
let mut keys: Vec<_> = if let Some(keys_limit) = self.params.keys_limit {
use sp_core::blake2_256;
let first_key = self.params.random_seed.map(|seed| sp_storage::StorageKey(blake2_256(&seed.to_be_bytes()[..]).to_vec()));
client.storage_keys(best_hash, None, first_key.as_ref())?.take(keys_limit).collect()
} else {
client.storage_keys(best_hash, None, None)?.collect()
};
let (mut rng, _) = new_rng(self.params.random_seed);
keys.shuffle(&mut rng);

let mut child_nodes = Vec::new();
Expand Down
13 changes: 10 additions & 3 deletions substrate/utils/frame/benchmarking-cli/src/storage/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ impl StorageCmd {
let trie = DbStateBuilder::<Block>::new(storage.clone(), original_root).build();

info!("Preparing keys from block {}", best_hash);
// Load all KV pairs and randomly shuffle them.
let mut kvs: Vec<_> = trie.pairs(Default::default())?.collect();
let (mut rng, _) = new_rng(None);
// Load KV pairs and randomly shuffle them.
let mut kvs: Vec<_> = if let Some(keys_limit) = self.params.keys_limit {
let start_at = self.params.random_seed.map(|seed| sp_core::blake2_256(&seed.to_be_bytes()[..]).to_vec());
let mut iter_args = sp_state_machine::IterArgs::default();
iter_args.start_at = start_at.as_deref();
trie.pairs(iter_args)?.take(keys_limit).collect()
} else {
trie.pairs(Default::default())?.collect()
};
let (mut rng, _) = new_rng(self.params.random_seed);
kvs.shuffle(&mut rng);
info!("Writing {} keys", kvs.len());

Expand Down
Loading