|
| 1 | +//! An example of performance counters usage. |
| 2 | +//! |
| 3 | +//! dfx deploy && dfx canister call performance_counters get |
| 4 | +
|
| 5 | +// The following performance counters supported: |
| 6 | +// |
| 7 | +// - 0 : current execution instruction counter. |
| 8 | +// The number of WebAssembly instructions the canister has executed |
| 9 | +// since the beginning of the current Message execution. |
| 10 | +// |
| 11 | +// - 1 : call context instruction counter. |
| 12 | +// - For replicated message execution, it is the number of WebAssembly instructions |
| 13 | +// the canister has executed within the call context of the current Message execution |
| 14 | +// since Call context creation. The counter monotonically increases across all message |
| 15 | +// executions in the call context until the corresponding call context is removed. |
| 16 | +// - For non-replicated message execution, it is the number of WebAssembly instructions |
| 17 | +// the canister has executed within the corresponding `composite_query_helper` |
| 18 | +// in Query call. The counter monotonically increases across the executions |
| 19 | +// of the composite query method and the composite query callbacks |
| 20 | +// until the corresponding `composite_query_helper` returns |
| 21 | +// (ignoring WebAssembly instructions executed within any further downstream calls |
| 22 | +// of `composite_query_helper`). |
| 23 | +// |
| 24 | +// In the future, the IC might expose more performance counters. |
| 25 | +use ic_cdk::api::{call_context_instruction_counter, instruction_counter}; |
| 26 | + |
| 27 | +/// Pretty print the `title` and a corresponding `tuple` with counters. |
| 28 | +fn pretty_print<N: std::fmt::Display, T: std::fmt::Display>(title: N, counters: (T, T)) { |
| 29 | + ic_cdk::println!("{:40} {:<15} {:<15}", title, counters.0, counters.1); |
| 30 | +} |
| 31 | + |
| 32 | +/// Loop to simulate some amount of work. |
| 33 | +fn do_some_work() { |
| 34 | + for i in 0..1_000_000 { |
| 35 | + // The black box hint is to avoid compiler optimizations for the loop. |
| 36 | + std::hint::black_box(i); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Returns a tuple with all the performance counters. |
| 41 | +fn counters() -> (u64, u64) { |
| 42 | + (instruction_counter(), call_context_instruction_counter()) |
| 43 | +} |
| 44 | + |
| 45 | +/// Emulate a nested inter-canister update call. |
| 46 | +#[ic_cdk_macros::update] |
| 47 | +fn nested_update_call() -> (u64, u64) { |
| 48 | + counters() |
| 49 | +} |
| 50 | + |
| 51 | +/// Emulate a nested inter-canister composite query call. |
| 52 | +#[ic_cdk_macros::query(composite = true)] |
| 53 | +fn nested_composite_query_call() -> (u64, u64) { |
| 54 | + counters() |
| 55 | +} |
| 56 | + |
| 57 | +//////////////////////////////////////////////////////////////////////// |
| 58 | +// Canister interface |
| 59 | +//////////////////////////////////////////////////////////////////////// |
| 60 | + |
| 61 | +/// Example usage: `dfx canister call performance_counters for_update` |
| 62 | +#[ic_cdk_macros::update] |
| 63 | +async fn for_update() -> (u64, u64) { |
| 64 | + do_some_work(); |
| 65 | + let before = counters(); |
| 66 | + |
| 67 | + let inside_1st: (u64, u64) = ic_cdk::call(ic_cdk::id(), "nested_update_call", ()) |
| 68 | + .await |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + do_some_work(); |
| 72 | + let after_1st = counters(); |
| 73 | + |
| 74 | + let inside_2nd: (u64, u64) = ic_cdk::call(ic_cdk::id(), "nested_update_call", ()) |
| 75 | + .await |
| 76 | + .unwrap(); |
| 77 | + |
| 78 | + do_some_work(); |
| 79 | + let after_2nd = counters(); |
| 80 | + |
| 81 | + pretty_print( |
| 82 | + "Performance counters for update call:", |
| 83 | + ("current (0)", "call context (1)"), |
| 84 | + ); |
| 85 | + pretty_print(" before the nested call:", before); |
| 86 | + pretty_print(" > inside the 1st nested call:", inside_1st); |
| 87 | + pretty_print(" after the 1st nested call:", after_1st); |
| 88 | + pretty_print(" > inside the 2nd nested call:", inside_2nd); |
| 89 | + pretty_print(" after the 2nd nested call:", after_2nd); |
| 90 | + |
| 91 | + after_2nd |
| 92 | +} |
| 93 | + |
| 94 | +/// Example usage: `dfx canister call performance_counters for_composite_query` |
| 95 | +#[ic_cdk_macros::query(composite = true)] |
| 96 | +async fn for_composite_query() -> (u64, u64) { |
| 97 | + do_some_work(); |
| 98 | + let before = counters(); |
| 99 | + |
| 100 | + let inside_1st: (u64, u64) = ic_cdk::call(ic_cdk::id(), "nested_composite_query_call", ()) |
| 101 | + .await |
| 102 | + .unwrap(); |
| 103 | + |
| 104 | + do_some_work(); |
| 105 | + let after_1st = counters(); |
| 106 | + |
| 107 | + let inside_2nd: (u64, u64) = ic_cdk::call(ic_cdk::id(), "nested_composite_query_call", ()) |
| 108 | + .await |
| 109 | + .unwrap(); |
| 110 | + |
| 111 | + do_some_work(); |
| 112 | + let after_2nd = counters(); |
| 113 | + |
| 114 | + pretty_print( |
| 115 | + "Perf. counters for composite query call:", |
| 116 | + ("current (0)", "call context (1)"), |
| 117 | + ); |
| 118 | + pretty_print(" before the nested call:", before); |
| 119 | + pretty_print(" > inside the 1st nested call:", inside_1st); |
| 120 | + pretty_print(" after the 1st nested call:", after_1st); |
| 121 | + pretty_print(" > inside the 2nd nested call:", inside_2nd); |
| 122 | + pretty_print(" after the 2nd nested call:", after_2nd); |
| 123 | + |
| 124 | + after_2nd |
| 125 | +} |
0 commit comments