|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +//! Criterion benchmark for `CosmosDriver::execute_operation` — point read. |
| 5 | +//! |
| 6 | +//! By default the reqwest transport is replaced with an in-memory mock so that |
| 7 | +//! the benchmark measures driver overhead (routing, signing, retry state, |
| 8 | +//! response parsing, session token management) without any network I/O. |
| 9 | +//! |
| 10 | +//! Set `AZURE_BENCH_MODE=live` to run against a real Cosmos DB endpoint. See |
| 11 | +//! `azure_data_cosmos_benchmarks::setup_live` for the required environment |
| 12 | +//! variables. |
| 13 | +//! |
| 14 | +//! Cache priming (account metadata, container metadata) is performed in setup, |
| 15 | +//! outside the measured iteration loop. |
| 16 | +//! |
| 17 | +//! # CPU flamegraph profiling |
| 18 | +//! |
| 19 | +//! Run with `--profile-time` to generate a flamegraph SVG via pprof: |
| 20 | +//! |
| 21 | +//! ```text |
| 22 | +//! cargo bench -p azure_data_cosmos_benchmarks --bench point_read -- --profile-time 30 |
| 23 | +//! ``` |
| 24 | +//! |
| 25 | +//! Output: `target/criterion/point_read/profile/flamegraph.svg` |
| 26 | +
|
| 27 | +use azure_data_cosmos_benchmarks::{self as common, BenchConfig}; |
| 28 | + |
| 29 | +use std::time::Duration; |
| 30 | + |
| 31 | +use azure_data_cosmos_driver::{models::CosmosOperation, options::OperationOptions}; |
| 32 | +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; |
| 33 | +use tokio::runtime::Builder; |
| 34 | + |
| 35 | +fn bench_point_read(c: &mut Criterion) { |
| 36 | + let rt = Builder::new_current_thread() |
| 37 | + .enable_all() |
| 38 | + .build() |
| 39 | + .expect("failed to create tokio runtime"); |
| 40 | + |
| 41 | + let mut group = c.benchmark_group("point_read"); |
| 42 | + group.throughput(Throughput::Elements(1)); |
| 43 | + |
| 44 | + match common::load_bench_config() { |
| 45 | + BenchConfig::Mock => { |
| 46 | + let (driver, item_ref) = rt.block_on(common::setup()); |
| 47 | + group.bench_function("mock", |b| { |
| 48 | + b.to_async(&rt).iter(|| async { |
| 49 | + driver |
| 50 | + .execute_operation( |
| 51 | + CosmosOperation::read_item(item_ref.clone()), |
| 52 | + OperationOptions::default(), |
| 53 | + ) |
| 54 | + .await |
| 55 | + .expect("execute_operation failed") |
| 56 | + }); |
| 57 | + }); |
| 58 | + } |
| 59 | + BenchConfig::Live => { |
| 60 | + let (driver, item_ref) = rt.block_on(common::setup_live()); |
| 61 | + group |
| 62 | + .sample_size(50) |
| 63 | + .measurement_time(Duration::from_secs(30)); |
| 64 | + group.bench_function("live", |b| { |
| 65 | + b.to_async(&rt).iter(|| async { |
| 66 | + driver |
| 67 | + .execute_operation( |
| 68 | + CosmosOperation::read_item(item_ref.clone()), |
| 69 | + OperationOptions::default(), |
| 70 | + ) |
| 71 | + .await |
| 72 | + .expect("execute_operation failed") |
| 73 | + }); |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + group.finish(); |
| 79 | +} |
| 80 | + |
| 81 | +criterion_group!(benches, bench_point_read); |
| 82 | +criterion_main!(benches); |
0 commit comments