-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
77 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::tracing::Instrument; | ||
use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
use tokio::runtime::{Builder, Runtime}; | ||
use xmtp_id::{ | ||
associations::{ | ||
builder::SignatureRequest, | ||
unverified::{UnverifiedRecoverableEcdsaSignature, UnverifiedSignature}, | ||
}, | ||
InboxOwner, | ||
}; | ||
use xmtp_mls::utils::bench::{bench_async_setup, BenchClient, BENCH_ROOT_SPAN}; | ||
use xmtp_mls::utils::bench::{clients, init_logging}; | ||
|
||
#[macro_use] | ||
extern crate tracing; | ||
|
||
fn setup() -> Runtime { | ||
Builder::new_multi_thread() | ||
.enable_time() | ||
.enable_io() | ||
.thread_name("xmtp-bencher") | ||
.build() | ||
.unwrap() | ||
} | ||
|
||
async fn ecdsa_signature(client: &BenchClient, owner: impl InboxOwner) -> SignatureRequest { | ||
let mut signature_request = client.context().signature_request().unwrap(); | ||
let signature_text = signature_request.signature_text(); | ||
let unverified_signature = UnverifiedSignature::RecoverableEcdsa( | ||
UnverifiedRecoverableEcdsaSignature::new(owner.sign(&signature_text).unwrap().into()), | ||
); | ||
signature_request | ||
.add_signature(unverified_signature, client.scw_verifier()) | ||
.await | ||
.unwrap(); | ||
|
||
signature_request | ||
} | ||
|
||
fn register_identity_eoa(c: &mut Criterion) { | ||
init_logging(); | ||
|
||
let runtime = setup(); | ||
|
||
let mut benchmark_group = c.benchmark_group("register_identity"); | ||
benchmark_group.sample_size(10); | ||
benchmark_group.bench_function("register_identity_eoa", |b| { | ||
let span = trace_span!(BENCH_ROOT_SPAN); | ||
b.to_async(&runtime).iter_batched( | ||
|| { | ||
bench_async_setup(|| async { | ||
let (client, wallet) = clients::new_unregistered_client(false).await; | ||
let signature_request = ecdsa_signature(&client, wallet).await; | ||
|
||
(client, signature_request, span.clone()) | ||
}) | ||
}, | ||
|(client, request, span)| async move { | ||
client | ||
.register_identity(request) | ||
.instrument(span) | ||
.await | ||
.unwrap() | ||
}, | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
|
||
benchmark_group.finish(); | ||
} | ||
|
||
criterion_group!( | ||
name = identity; | ||
config = Criterion::default().sample_size(10); | ||
targets = register_identity_eoa | ||
); | ||
criterion_main!(identity); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
static INIT: Once = Once::new(); | ||
|
||
static LOGGER: OnceCell<FlushGuard<std::io::BufWriter<std::fs::File>>> = OnceCell::new(); | ||
|
||
pub const BENCH_ROOT_SPAN: &str = "xmtp-trace-bench"; | ||
|
||
/// initializes logging for benchmarks | ||
/// - FMT logging is enabled by passing the normal `RUST_LOG` environment variable options. | ||
/// - Generate a flamegraph from tracing data by passing `XMTP_FLAMEGRAPH=trace` | ||
pub fn logger() { | ||
INIT.call_once(|| { | ||
let (flame_layer, guard) = FlameLayer::with_file("./tracing.folded").unwrap(); | ||
let flame_layer = flame_layer | ||
.with_threads_collapsed(true) | ||
.with_module_path(true); | ||
// .with_empty_samples(false); | ||
|
||
tracing_subscriber::registry() | ||
.with(tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env())) | ||
.with( | ||
flame_layer | ||
.with_filter(BenchFilter) | ||
.with_filter(EnvFilter::from_env("XMTP_FLAMEGRAPH")), | ||
) | ||
.init(); | ||
|
||
LOGGER.set(guard).unwrap(); | ||
}) | ||
} | ||
|
||
/// criterion `batch_iter` surrounds the closure in a `Runtime.block_on` despite being a sync | ||
/// function, even in the async 'to_async` setup. Therefore we do this (only _slightly_) hacky | ||
/// workaround to allow us to async setup some groups. | ||
pub fn bench_async_setup<F, T, Fut>(fun: F) -> T | ||
where | ||
F: Fn() -> Fut, | ||
Fut: futures::future::Future<Output = T>, | ||
{ | ||
use tokio::runtime::Handle; | ||
tokio::task::block_in_place(move || Handle::current().block_on(async move { fun().await })) | ||
} | ||
|
||
/// Filters for only spans where the root span name is "bench" | ||
pub struct BenchFilter; | ||
|
||
impl<S> Filter<S> for BenchFilter | ||
where | ||
S: Subscriber + for<'lookup> LookupSpan<'lookup> + std::fmt::Debug, | ||
for<'lookup> <S as LookupSpan<'lookup>>::Data: std::fmt::Debug, | ||
{ | ||
fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { | ||
if meta.name() == BENCH_ROOT_SPAN { | ||
return true; | ||
} | ||
if let Some(id) = cx.current_span().id() { | ||
if let Some(s) = cx.span_scope(id) { | ||
if let Some(s) = s.from_root().take(1).collect::<Vec<_>>().first() { | ||
return s.name() == BENCH_ROOT_SPAN; | ||
} | ||
} | ||
} | ||
false | ||
} | ||
} |
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
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
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
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