Skip to content

Commit

Permalink
add --random-seed param
Browse files Browse the repository at this point in the history
  • Loading branch information
librelois committed Apr 23, 2024
1 parent 6aca526 commit 6aa86d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
11 changes: 9 additions & 2 deletions substrate/utils/frame/benchmarking-cli/src/storage/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ pub struct StorageParams {
/// (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 @@ -197,11 +202,13 @@ impl StorageCmd {
{
let hash = client.usage_info().chain.best_hash;
let mut keys: Vec<_> = if let Some(keys_limit) = self.params.keys_limit {
client.storage_keys(hash, None, None)?.take(keys_limit).collect()
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(None);
let (mut rng, _) = new_rng(self.params.random_seed);
keys.shuffle(&mut rng);

for i in 0..self.params.warmups {
Expand Down
6 changes: 4 additions & 2 deletions substrate/utils/frame/benchmarking-cli/src/storage/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ impl StorageCmd {
info!("Preparing keys from block {}", best_hash);
// Load keys and randomly shuffle them.
let mut keys: Vec<_> = if let Some(keys_limit) = self.params.keys_limit {
client.storage_keys(best_hash, None, None)?.take(keys_limit).collect()
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(None);
let (mut rng, _) = new_rng(self.params.random_seed);
keys.shuffle(&mut rng);

let mut child_nodes = Vec::new();
Expand Down
7 changes: 5 additions & 2 deletions substrate/utils/frame/benchmarking-cli/src/storage/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ impl StorageCmd {
info!("Preparing keys from block {}", best_hash);
// Load KV pairs and randomly shuffle them.
let mut kvs: Vec<_> = if let Some(keys_limit) = self.params.keys_limit {
trie.pairs(Default::default())?.take(keys_limit).collect()
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(None);
let (mut rng, _) = new_rng(self.params.random_seed);
kvs.shuffle(&mut rng);
info!("Writing {} keys", kvs.len());

Expand Down

0 comments on commit 6aa86d7

Please sign in to comment.