Skip to content

Commit

Permalink
update benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ielashi committed Jul 19, 2023
1 parent 59582e7 commit 7bc13a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions benchmark-canisters/src/btreemap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{count_instructions, Random};
use ic_cdk_macros::query;
use ic_stable_structures::{storable::Blob, BTreeMap, BoundedStorable, DefaultMemoryImpl};
use ic_stable_structures::{storable::Blob, BTreeMap, DefaultMemoryImpl, Storable};
use tiny_rng::{Rand, Rng};

#[query]
Expand Down Expand Up @@ -216,7 +216,7 @@ fn insert_blob_helper<const K: usize, const V: usize>() -> u64 {
}

// Profiles inserting a large number of random blobs into a btreemap.
fn insert_helper<K: Clone + Ord + BoundedStorable + Random, V: BoundedStorable + Random>() -> u64 {
fn insert_helper<K: Clone + Ord + Storable + Random, V: Storable + Random>() -> u64 {
let mut btree: BTreeMap<K, V, _> = BTreeMap::new(DefaultMemoryImpl::default());
let num_keys = 10_000;
let mut rng = Rng::from_seed(0);
Expand All @@ -241,7 +241,7 @@ fn get_blob_helper<const K: usize, const V: usize>() -> u64 {
get_helper::<Blob<K>, Blob<V>>()
}

fn get_helper<K: Clone + Ord + BoundedStorable + Random, V: BoundedStorable + Random>() -> u64 {
fn get_helper<K: Clone + Ord + Storable + Random, V: Storable + Random>() -> u64 {
let mut btree: BTreeMap<K, V, _> = BTreeMap::new(DefaultMemoryImpl::default());
let num_keys = 10_000;
let mut rng = Rng::from_seed(0);
Expand Down Expand Up @@ -271,7 +271,7 @@ fn remove_blob_helper<const K: usize, const V: usize>() -> u64 {
remove_helper::<Blob<K>, Blob<V>>()
}

fn remove_helper<K: Clone + Ord + BoundedStorable + Random, V: BoundedStorable + Random>() -> u64 {
fn remove_helper<K: Clone + Ord + Storable + Random, V: Storable + Random>() -> u64 {
let mut btree: BTreeMap<K, V, _> = BTreeMap::new(DefaultMemoryImpl::default());
let num_keys = 10_000;
let mut rng = Rng::from_seed(0);
Expand Down
12 changes: 10 additions & 2 deletions benchmark-canisters/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ic_stable_structures::{storable::Blob, BoundedStorable};
use ic_stable_structures::storable::{Blob, Bound, Storable};
use tiny_rng::{Rand, Rng};

mod btreemap;
Expand All @@ -12,13 +12,21 @@ pub(crate) fn count_instructions<R>(f: impl FnOnce() -> R) -> u64 {
ic_cdk::api::performance_counter(0) - start
}

const fn max_size<A: Storable>() -> u32 {
if let Bound::Bounded { max_size, .. } = A::BOUND {
max_size
} else {
panic!("Cannot get max size of unbounded type.");
}
}

trait Random {
fn random(rng: &mut Rng) -> Self;
}

impl<const K: usize> Random for Blob<K> {
fn random(rng: &mut Rng) -> Self {
let size = rng.rand_u32() % Blob::<K>::MAX_SIZE;
let size = rng.rand_u32() % max_size::<Blob::<K>>();
Blob::try_from(
rng.iter(Rand::rand_u8)
.take(size as usize)
Expand Down
6 changes: 3 additions & 3 deletions benchmark-canisters/src/vec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{count_instructions, Random};
use ic_cdk_macros::query;
use ic_stable_structures::storable::Blob;
use ic_stable_structures::{BoundedStorable, DefaultMemoryImpl, StableVec};
use ic_stable_structures::{DefaultMemoryImpl, StableVec, Storable};
use tiny_rng::{Rand, Rng};

#[query]
Expand Down Expand Up @@ -78,7 +78,7 @@ fn vec_insert_blob<const N: usize>() -> u64 {
vec_insert::<Blob<N>>()
}

fn vec_insert<T: BoundedStorable + Random>() -> u64 {
fn vec_insert<T: Storable + Random>() -> u64 {
let num_items = 10_000;
let svec: StableVec<T, _> = StableVec::new(DefaultMemoryImpl::default()).unwrap();

Expand All @@ -100,7 +100,7 @@ fn vec_get_blob<const N: usize>() -> u64 {
vec_get::<Blob<N>>()
}

fn vec_get<T: BoundedStorable + Random>() -> u64 {
fn vec_get<T: Storable + Random>() -> u64 {
let num_items = 10_000;
let svec: StableVec<T, _> = StableVec::new(DefaultMemoryImpl::default()).unwrap();

Expand Down

0 comments on commit 7bc13a1

Please sign in to comment.