Created
February 12, 2025 12:21
-
-
Save sandersaares/0f55001566e7451b1180542a189ef5c5 to your computer and use it in GitHub Desktop.
OTel synchronization benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use opentelemetry::metrics::MeterProvider; | |
use opentelemetry_sdk::metrics::{ManualReader, SdkMeterProvider}; | |
use std::env::args; | |
use std::fmt::Debug; | |
use std::str::FromStr; | |
use std::thread::{spawn, JoinHandle}; | |
use std::time::{Duration, Instant}; | |
const NUM_THREADS: usize = 10; | |
const RUN_TIME: Duration = Duration::from_secs(120); | |
fn main() { | |
let mode: Mode = args() | |
.nth(1) | |
.unwrap_or("shared".to_string()) | |
.parse() | |
.unwrap(); | |
println!( | |
"Running with mode: {:?}, Duration: {}s, Threads: {}", | |
mode, | |
RUN_TIME.as_secs(), | |
NUM_THREADS | |
); | |
let handles: Vec<JoinHandle<_>> = match mode { | |
Mode::Shared => { | |
let provider = create_meter_provider(); | |
(0..NUM_THREADS) | |
.map(move |_| start_work(provider.clone())) | |
.collect() | |
} | |
Mode::PerThread => (0..NUM_THREADS) | |
.map(move |_| start_work(create_meter_provider())) | |
.collect(), | |
}; | |
let sum = handles | |
.into_iter() | |
.map(|h| h.join().unwrap()) | |
.sum::<usize>(); | |
println!("Reported Metrics: {} millions", (sum / 1_000_000)); | |
} | |
fn start_work(meter_provider: SdkMeterProvider) -> JoinHandle<usize> { | |
const BATCH_SIZE: usize = 10_000; | |
let histogram = meter_provider | |
.meter("dummy") | |
.f64_histogram("histogram") | |
.build(); | |
spawn(move || { | |
let mut count = 0_usize; | |
let now = Instant::now(); | |
loop { | |
for _ in 0..BATCH_SIZE { | |
histogram.record(10.5, &[]); | |
} | |
count = count.checked_add(BATCH_SIZE).unwrap(); | |
if now.elapsed() > RUN_TIME { | |
break; | |
} | |
} | |
count | |
}) | |
} | |
#[derive(Debug, Clone, Copy)] | |
enum Mode { | |
Shared, | |
PerThread, | |
} | |
impl FromStr for Mode { | |
type Err = &'static str; | |
fn from_str(s: &str) -> Result<Self, Self::Err> { | |
match s { | |
"shared" => Ok(Mode::Shared), | |
"per-thread" => Ok(Mode::PerThread), | |
_ => Err("invalid mode"), | |
} | |
} | |
} | |
fn create_meter_provider() -> SdkMeterProvider { | |
SdkMeterProvider::builder() | |
.with_reader(ManualReader::default()) | |
.build() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use opentelemetry::metrics::MeterProvider; | |
use opentelemetry_sdk::metrics::{ManualReader, SdkMeterProvider}; | |
use std::env::args; | |
use std::fmt::Debug; | |
use std::str::FromStr; | |
use std::thread::{spawn, JoinHandle}; | |
use std::time::{Duration, Instant}; | |
const NUM_THREADS: usize = 10; | |
const RUN_TIME: Duration = Duration::from_secs(120); | |
fn main() { | |
let mode: Mode = args() | |
.nth(1) | |
.unwrap_or("shared".to_string()) | |
.parse() | |
.unwrap(); | |
println!( | |
"Running with mode: {:?}, Duration: {}s, Threads: {}", | |
mode, | |
RUN_TIME.as_secs(), | |
NUM_THREADS | |
); | |
let handles: Vec<JoinHandle<_>> = (0..NUM_THREADS).map(move |_| start_work()).collect(); | |
let sum = handles | |
.into_iter() | |
.map(|h| h.join().unwrap()) | |
.sum::<usize>(); | |
println!("Reported Metrics: {} millions", (sum / 1_000_000)); | |
} | |
fn start_work() -> JoinHandle<usize> { | |
const BATCH_SIZE: usize = 10_000; | |
spawn(move || { | |
let meter_provider = create_meter_provider(); | |
let histogram = meter_provider | |
.meter("dummy") | |
.f64_histogram("histogram") | |
.build(); | |
let mut count = 0_usize; | |
let now = Instant::now(); | |
loop { | |
for _ in 0..BATCH_SIZE { | |
histogram.record(10.5, &[]); | |
} | |
count = count.checked_add(BATCH_SIZE).unwrap(); | |
if now.elapsed() > RUN_TIME { | |
break; | |
} | |
} | |
count | |
}) | |
} | |
#[derive(Debug, Clone, Copy)] | |
enum Mode { | |
Shared, | |
PerThread, | |
} | |
impl FromStr for Mode { | |
type Err = &'static str; | |
fn from_str(s: &str) -> Result<Self, Self::Err> { | |
match s { | |
"per-thread" => Ok(Mode::PerThread), | |
_ => Err("unsupported mode"), | |
} | |
} | |
} | |
fn create_meter_provider() -> SdkMeterProvider { | |
SdkMeterProvider::builder() | |
.with_reader(ManualReader::default()) | |
.build() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment