Skip to content

Instantly share code, notes, and snippets.

@notlesh
Created August 10, 2021 19:59
Show Gist options
  • Save notlesh/9a92c6ec49ff90f7767749d0918a1c83 to your computer and use it in GitHub Desktop.
Save notlesh/9a92c6ec49ff90f7767749d0918a1c83 to your computer and use it in GitHub Desktop.
test relationship between number of storage items and CPU overehead
#[test]
fn test_timing() {
use std::time::{Instant, Duration};
use frame_benchmarking::{account};
use frame_support::traits::ExistenceRequirement;
println!("test_timing() .........");
/// Create a funded user
fn create_funded_user<T: Config>(
string: &'static str,
n: u32,
amount: BalanceOf<T>
) -> T::AccountId {
const SEED: u32 = 0;
let user = account(string, n, SEED);
T::Currency::make_free_balance_be(&user, amount);
T::Currency::issue(amount);
user
}
new_test_ext().execute_with(|| {
let test_start = Instant::now();
let mut num_created = 0;
let first_acct: <mock::Test as frame_system::Config>::AccountId = create_funded_user::<Test>("first", u32::MAX.into(), 1u32.into());
for n in 2..30 {
let next_max = 2u32.pow(n);
let next_start = Instant::now();
let mut num_created_batch = 0u32;
let mut last_acct_created: Option<<mock::Test as frame_system::Config>::AccountId> = None;
for na in num_created..next_max {
let new_acct: <mock::Test as frame_system::Config>::AccountId = create_funded_user::<Test>("foo", na.into(), 100000000u32.into());
num_created += 1;
num_created_batch += 1;
last_acct_created = Some(new_acct);
}
let step_duration = next_start.elapsed();
let time_per_acct = num_created_batch as f64 / step_duration.as_secs_f64();
println!("step {} took {} micros ({} each)", n, step_duration.as_micros(), time_per_acct);
// try sending money to the initial account and time it
let send_start = Instant::now();
for i in 1..100000 {
let result = <mock::Test as Config>::Currency::force_transfer(
RawOrigin::Root.into(),
last_acct_created.expect("set above..."),
first_acct,
10u32.into());
if let Err(err) = result {
panic!("force_transfer failed");
}
}
let elapsed = send_start.elapsed();
println!("send after step {} took {} micros", n, elapsed.as_micros());
}
let test_duration = test_start.elapsed();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment