Skip to content

Commit

Permalink
Merge pull request #626 from dfinity/andriy/update-performance-counters
Browse files Browse the repository at this point in the history
Update performance counter examples
  • Loading branch information
dfinity-berestovskyy authored Nov 30, 2023
2 parents b47a971 + 7cee047 commit 01be856
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions rust/performance_counters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
candid = "0.9.11"
ic-cdk = "0.11.3"
candid = "0.10.0"
ic-cdk = "0.12.0"
ic-cdk-macros = "0.8.1"
1 change: 1 addition & 0 deletions rust/performance_counters/performance_counters.did
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
service : {
"for_update" : () -> (nat64, nat64);
"for_composite_query" : () -> (nat64, nat64) query;
"example" : () -> (nat64, nat64) query;
};
26 changes: 23 additions & 3 deletions rust/performance_counters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
// of `composite_query_helper`).
//
// In the future, the IC might expose more performance counters.
use ic_cdk::api::{call_context_instruction_counter, instruction_counter};
use ic_cdk::{
api::{call_context_instruction_counter, instruction_counter, performance_counter},
call, id,
};

/// Pretty print the `title` and a corresponding `tuple` with counters.
fn pretty_print<N: std::fmt::Display, T: std::fmt::Display>(title: N, counters: (T, T)) {
Expand Down Expand Up @@ -54,11 +57,15 @@ fn nested_composite_query_call() -> (u64, u64) {
counters()
}

/// Emulate a nested inter-canister update call.
#[ic_cdk_macros::query]
fn nested_call() {}

////////////////////////////////////////////////////////////////////////
// Canister interface
////////////////////////////////////////////////////////////////////////

/// Example usage: `dfx canister call performance_counters for_update`
/// Example usage: `dfx deploy && dfx canister call performance_counters for_update`
#[ic_cdk_macros::update]
async fn for_update() -> (u64, u64) {
do_some_work();
Expand Down Expand Up @@ -91,7 +98,7 @@ async fn for_update() -> (u64, u64) {
after_2nd
}

/// Example usage: `dfx canister call performance_counters for_composite_query`
/// Example usage: `dfx deploy && dfx canister call performance_counters for_composite_query`
#[ic_cdk_macros::query(composite = true)]
async fn for_composite_query() -> (u64, u64) {
do_some_work();
Expand Down Expand Up @@ -123,3 +130,16 @@ async fn for_composite_query() -> (u64, u64) {

after_2nd
}

/// Example usage: `dfx deploy && dfx canister call performance_counters example`
#[ic_cdk_macros::query(composite = true)]
async fn example() -> (u64, u64) {
do_some_work();
call::<(), ()>(id(), "nested_call", ()).await.unwrap();

do_some_work();
call::<(), ()>(id(), "nested_call", ()).await.unwrap();

do_some_work();
(performance_counter(0), performance_counter(1))
}

0 comments on commit 01be856

Please sign in to comment.