Skip to content

Commit

Permalink
Merge pull request #7 from moonbeam-foundation/bench-storage-keys-limit
Browse files Browse the repository at this point in the history
Add a command line option --keys-limit that limit the number of keys to read/write on storage benchmarks
  • Loading branch information
RomarQ authored and gonzamontiel committed Aug 14, 2024
1 parent a038b2b commit e61261e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
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::<HashingFor<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

0 comments on commit e61261e

Please sign in to comment.