Skip to content

Instantly share code, notes, and snippets.

@REASY
Created November 25, 2025 11:19
Show Gist options
  • Select an option

  • Save REASY/736b165cfd93918aa6c746dcdcfffa02 to your computer and use it in GitHub Desktop.

Select an option

Save REASY/736b165cfd93918aa6c746dcdcfffa02 to your computer and use it in GitHub Desktop.
fory_contention.rs
/*
[dependencies]
fory = "0.13.1"
fory-derive = "0.13.1"
serde = { version = "1", features = ["derive"] }
[dev-dependencies]
criterion = { version = "0.7", features = ["async_tokio"] }
*/
use criterion::{criterion_group, criterion_main, Criterion};
use fory::Fory;
use fory_derive::ForyObject;
use serde::{Deserialize, Serialize};
use std::hint::black_box;
use std::sync::Arc;
use std::thread;
/// Value type used with Fory
#[derive(Debug, Deserialize, Serialize, Hash, Eq, PartialEq, ForyObject)]
pub struct ForyMlUseridV360Value {
pub count_requests: u64,
pub count_distinct_ips: u64,
pub count_distinct_useragents: u64,
pub count_distinct_requesturls: u64,
pub count_distinct_resources: u64,
pub count_session_active_second: u64,
pub min_event_time: u64,
pub max_event_time: u64,
pub last_seen_event_time: u64,
}
/// Creates a Fory instance registered for `ForyMlUseridV360Value`
pub fn create_fory_ml() -> Fory {
let mut fory = Fory::default();
fory
.register::<ForyMlUseridV360Value>(2)
.expect("register ForyMlUseridV360Value");
fory
}
/// Sample value used in the benchmark
pub fn sample_ml_userid_v360() -> ForyMlUseridV360Value {
ForyMlUseridV360Value {
count_requests: 256,
count_distinct_ips: 32,
count_distinct_useragents: 12,
count_distinct_requesturls: 64,
count_distinct_resources: 48,
count_session_active_second: 90,
min_event_time: 1_699_999_900_000,
max_event_time: 1_700_000_000_000,
last_seen_event_time: 1_700_000_000_000,
}
}
fn bench_fory_ml_userid_v360_contention(group: &mut Criterion) {
const SERIALIZATION_COUNT: usize = 200_000;
const THREAD_COUNT: usize = 8;
let mut bench = group.benchmark_group("ForyMlUseridV360Contention");
// Single-thread benchmark
let single_thread_fory = create_fory_ml();
let single_thread_value = sample_ml_userid_v360();
bench.bench_function("single_thread_200k_serializations", |b| {
b.iter(|| {
for _ in 0..SERIALIZATION_COUNT {
let bytes = single_thread_fory
.serialize(black_box(&single_thread_value))
.expect("serialize ForyMlUseridV360Value with Fory");
black_box(bytes);
}
});
});
// Shared serializer across multiple threads
let shared_fory = Arc::new(create_fory_ml());
let shared_value = Arc::new(sample_ml_userid_v360());
bench.bench_function("shared_serializer_8_threads_200k_serializations", |b| {
b.iter(|| {
thread::scope(|scope| {
for _ in 0..THREAD_COUNT {
let fory = Arc::clone(&shared_fory);
let value = Arc::clone(&shared_value);
scope.spawn(move || {
for _ in 0..(SERIALIZATION_COUNT / THREAD_COUNT) {
let bytes = fory
.serialize(value.as_ref())
.expect("serialize ForyMlUseridV360Value with Fory");
black_box(bytes);
}
});
}
});
});
});
bench.finish();
}
fn criterion_benchmark(c: &mut Criterion) {
bench_fory_ml_userid_v360_contention(c);
}
criterion_group!(fory_contention, criterion_benchmark);
criterion_main!(fory_contention);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment