|
| 1 | +use crate::tracing::Instrument; |
| 2 | +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; |
| 3 | +use ethers::signers::LocalWallet; |
| 4 | +use tokio::runtime::{Builder, Runtime}; |
| 5 | +use xmtp_id::{ |
| 6 | + associations::{ |
| 7 | + builder::SignatureRequest, |
| 8 | + generate_inbox_id, |
| 9 | + unverified::{UnverifiedRecoverableEcdsaSignature, UnverifiedSignature}, |
| 10 | + }, |
| 11 | + InboxOwner, |
| 12 | +}; |
| 13 | +use xmtp_mls::utils::{bench::init_logging, test::TestClient as TestApiClient}; |
| 14 | +use xmtp_mls::{ |
| 15 | + client::Client, |
| 16 | + identity::IdentityStrategy, |
| 17 | + utils::bench::{bench_async_setup, BENCH_ROOT_SPAN}, |
| 18 | +}; |
| 19 | +use xmtp_proto::api_client::XmtpTestClient; |
| 20 | + |
| 21 | +type BenchClient = Client<TestApiClient>; |
| 22 | + |
| 23 | +#[macro_use] |
| 24 | +extern crate tracing; |
| 25 | + |
| 26 | +fn setup() -> Runtime { |
| 27 | + Builder::new_multi_thread() |
| 28 | + .enable_time() |
| 29 | + .enable_io() |
| 30 | + .thread_name("xmtp-bencher") |
| 31 | + .build() |
| 32 | + .unwrap() |
| 33 | +} |
| 34 | + |
| 35 | +async fn new_client() -> (BenchClient, LocalWallet) { |
| 36 | + let nonce = 1; |
| 37 | + let wallet = xmtp_cryptography::utils::generate_local_wallet(); |
| 38 | + let inbox_id = generate_inbox_id(&wallet.get_address(), &nonce).unwrap(); |
| 39 | + |
| 40 | + let dev = std::env::var("DEV_GRPC"); |
| 41 | + let is_dev_network = matches!(dev, Ok(d) if d == "true" || d == "1"); |
| 42 | + |
| 43 | + let api_client = if is_dev_network { |
| 44 | + tracing::info!("Using Dev GRPC"); |
| 45 | + <TestApiClient as XmtpTestClient>::create_dev().await |
| 46 | + } else { |
| 47 | + tracing::info!("Using Local GRPC"); |
| 48 | + <TestApiClient as XmtpTestClient>::create_local().await |
| 49 | + }; |
| 50 | + |
| 51 | + let client = BenchClient::builder(IdentityStrategy::new( |
| 52 | + inbox_id, |
| 53 | + wallet.get_address(), |
| 54 | + nonce, |
| 55 | + None, |
| 56 | + )); |
| 57 | + |
| 58 | + let client = client |
| 59 | + .temp_store() |
| 60 | + .await |
| 61 | + .api_client(api_client) |
| 62 | + .build() |
| 63 | + .await |
| 64 | + .unwrap(); |
| 65 | + |
| 66 | + (client, wallet) |
| 67 | +} |
| 68 | + |
| 69 | +async fn ecdsa_signature(client: &BenchClient, owner: impl InboxOwner) -> SignatureRequest { |
| 70 | + let mut signature_request = client.context().signature_request().unwrap(); |
| 71 | + let signature_text = signature_request.signature_text(); |
| 72 | + let unverified_signature = UnverifiedSignature::RecoverableEcdsa( |
| 73 | + UnverifiedRecoverableEcdsaSignature::new(owner.sign(&signature_text).unwrap().into()), |
| 74 | + ); |
| 75 | + signature_request |
| 76 | + .add_signature(unverified_signature, client.scw_verifier()) |
| 77 | + .await |
| 78 | + .unwrap(); |
| 79 | + |
| 80 | + signature_request |
| 81 | +} |
| 82 | + |
| 83 | +fn register_identity_eoa(c: &mut Criterion) { |
| 84 | + init_logging(); |
| 85 | + |
| 86 | + let runtime = setup(); |
| 87 | + |
| 88 | + let mut benchmark_group = c.benchmark_group("register_identity"); |
| 89 | + benchmark_group.sample_size(10); |
| 90 | + benchmark_group.bench_function("register_identity_eoa", |b| { |
| 91 | + let span = trace_span!(BENCH_ROOT_SPAN); |
| 92 | + b.to_async(&runtime).iter_batched( |
| 93 | + || { |
| 94 | + bench_async_setup(|| async { |
| 95 | + let (client, wallet) = new_client().await; |
| 96 | + let signature_request = ecdsa_signature(&client, wallet).await; |
| 97 | + |
| 98 | + (client, signature_request, span.clone()) |
| 99 | + }) |
| 100 | + }, |
| 101 | + |(client, request, span)| async move { |
| 102 | + client |
| 103 | + .register_identity(request) |
| 104 | + .instrument(span) |
| 105 | + .await |
| 106 | + .unwrap() |
| 107 | + }, |
| 108 | + BatchSize::SmallInput, |
| 109 | + ) |
| 110 | + }); |
| 111 | + |
| 112 | + benchmark_group.finish(); |
| 113 | +} |
| 114 | + |
| 115 | +criterion_group!( |
| 116 | + name = identity; |
| 117 | + config = Criterion::default().sample_size(10); |
| 118 | + targets = register_identity_eoa |
| 119 | +); |
| 120 | +criterion_main!(identity); |
0 commit comments