Skip to content

Commit

Permalink
perf: add benchmark for the get_metrics endpoint. (#238)
Browse files Browse the repository at this point in the history
Adds a benchmark to count how many instructions it takes to run the
`get_metrics` endpoint when there is a large number of unstable blocks.

The output of `benchmarks/run.sh` is currently:

```
(1_519_453_070 : nat64) <-- benchmark for `insert_300_blocks`
(163_059_419 : nat64)   <-- benchmark for `get_metrics`
```
  • Loading branch information
ielashi authored Jul 26, 2023
1 parent 5be193a commit 6c5dfda
Show file tree
Hide file tree
Showing 6 changed files with 3,734 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions benchmarks/drun.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
create
install rwlgt-iiaaa-aaaaa-aaaaa-cai ../target/wasm32-unknown-unknown/release/benchmarks.wasm.gz ""
query rwlgt-iiaaa-aaaaa-aaaaa-cai insert_300_blocks "DIDL\x00\x00"
query rwlgt-iiaaa-aaaaa-aaaaa-cai get_metrics "DIDL\x00\x00"
2 changes: 1 addition & 1 deletion benchmarks/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ bash ../scripts/build-canister.sh benchmarks
drun ./drun.txt --instruction-limit 99999999999999 \
| awk '{ print $3 }' \
| grep "44.*" -o \
| xargs didc decode
| xargs -L 1 didc decode
36 changes: 30 additions & 6 deletions benchmarks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ thread_local! {

#[init]
fn init() {
ic_btc_canister::init(Config {
network: Network::Testnet,
stability_threshold: 144,
..Config::default()
});

// Load the testnet blocks.
TESTNET_BLOCKS.with(|blocks| {
blocks.replace(
Expand All @@ -35,6 +29,12 @@ fn init() {
// Benchmarks inserting the first 300 blocks of the Bitcoin testnet.
#[query]
fn insert_300_blocks() -> u64 {
ic_btc_canister::init(Config {
network: Network::Testnet,
stability_threshold: 144,
..Config::default()
});

count_instructions(|| {
with_state_mut(|s| {
for i in 0..300 {
Expand All @@ -48,6 +48,30 @@ fn insert_300_blocks() -> u64 {
})
}

// Benchmarks gettings the metrics when there are many unstable blocks..
#[query]
fn get_metrics() -> u64 {
ic_btc_canister::init(Config {
network: Network::Testnet,
stability_threshold: 3000,
..Config::default()
});

with_state_mut(|s| {
for i in 0..3000 {
ic_btc_canister::state::insert_block(
s,
TESTNET_BLOCKS.with(|b| b.borrow()[i as usize].clone()),
)
.unwrap();
}
});

count_instructions(|| {
ic_btc_canister::get_metrics();
})
}

// Returns the number of instructions consumed by the given function.
fn count_instructions<R>(f: impl FnOnce() -> R) -> u64 {
let start = ic_cdk::api::performance_counter(0);
Expand Down
Loading

0 comments on commit 6c5dfda

Please sign in to comment.