Skip to main content

ic_testkit/
performance.rs

1//! Canister-side benchmark marker emission.
2
3use crate::benchmark::{BenchmarkCounters, DEFAULT_PREFIX, format_marker};
4
5const WASM_PAGE_BYTES: u128 = 65_536;
6
7pub struct Performance;
8
9impl Performance {
10    pub fn measure(label: &str) {
11        ic_cdk::api::debug_print(format_marker(DEFAULT_PREFIX, label, Self::counters()));
12    }
13
14    #[must_use]
15    pub fn counters() -> BenchmarkCounters {
16        BenchmarkCounters {
17            instructions: u128::from(ic_cdk::api::call_context_instruction_counter()),
18            heap_bytes: wasm_memory_size_bytes(),
19            memory_bytes: u128::from(ic_cdk::api::stable_size()) * WASM_PAGE_BYTES,
20            total_allocation: 0,
21        }
22    }
23}
24
25#[cfg(target_arch = "wasm32")]
26fn wasm_memory_size_bytes() -> u128 {
27    u128::try_from(core::arch::wasm32::memory_size(0)).expect("usize fits into u128")
28        * WASM_PAGE_BYTES
29}
30
31#[cfg(not(target_arch = "wasm32"))]
32const fn wasm_memory_size_bytes() -> u128 {
33    0
34}