Skip to content

Commit becc64f

Browse files
authored
feat: add get nonce tps
1 parent de32379 commit becc64f

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ clap = { version = "4.0", features = ["derive"] }
2121
tracing = "0.1"
2222
tracing-subscriber = "0.3"
2323
comfy-table = "7.1"
24+
indicatif = "0.17"
2425

2526
# Utilities
2627
uuid = { version = "1.0", features = ["v4"] }

src/main.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use alloy::{
66
use anyhow::Result;
77
use clap::Parser;
88
use futures::stream::{self, StreamExt};
9+
use indicatif::{ProgressBar, ProgressStyle};
910
use std::{
1011
collections::HashMap,
1112
process::{Command, Output},
@@ -424,25 +425,56 @@ async fn start_bench() -> Result<()> {
424425

425426
async fn init_nonce(accout_generator: &mut AccountGenerator, eth_client: Arc<EthHttpCli>) {
426427
tracing::info!("Initializing nonce...");
427-
let tasks = accout_generator
428-
.accouts_nonce_iter()
429-
.map(|(account, nonce)| {
430-
let client = eth_client.clone();
431-
let addr = account.clone();
432-
async move {
433-
let init_nonce = client.get_txn_count(addr).await;
434-
match init_nonce {
435-
Ok(init_nonce) => nonce.store(init_nonce, Ordering::Relaxed),
436-
Err(e) => tracing::error!("Failed to get nonce for address: {}: {}", addr, e),
428+
429+
// Collect all accounts first to get total count
430+
let accounts: Vec<_> = accout_generator.accouts_nonce_iter().collect();
431+
let total_accounts = accounts.len() as u64;
432+
433+
// Create progress bar
434+
let pb = ProgressBar::new(total_accounts);
435+
pb.set_style(
436+
ProgressStyle::default_bar()
437+
.template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({per_sec}, ETA: {eta})")
438+
.unwrap()
439+
.progress_chars("#>-"),
440+
);
441+
442+
let pb = Arc::new(pb);
443+
let start_time = Instant::now();
444+
445+
let tasks = accounts.into_iter().map(|(account, nonce)| {
446+
let client = eth_client.clone();
447+
let addr = account.clone();
448+
let pb = pb.clone();
449+
async move {
450+
let init_nonce = client.get_txn_count(addr).await;
451+
match init_nonce {
452+
Ok(init_nonce) => {
453+
nonce.store(init_nonce, Ordering::Relaxed);
454+
pb.inc(1);
455+
}
456+
Err(e) => {
457+
tracing::error!("Failed to get nonce for address: {}: {}", addr, e);
458+
pb.inc(1);
437459
}
438460
}
439-
});
461+
}
462+
});
440463

441464
stream::iter(tasks)
442465
.buffer_unordered(1024)
443466
.collect::<Vec<_>>()
444467
.await;
445-
tracing::info!("Nonce initialized");
468+
469+
pb.finish_with_message("Done");
470+
let elapsed = start_time.elapsed();
471+
let rate = total_accounts as f64 / elapsed.as_secs_f64();
472+
tracing::info!(
473+
"Nonce initialized: {} accounts in {:.2}s ({:.2} accounts/sec)",
474+
total_accounts,
475+
elapsed.as_secs_f64(),
476+
rate
477+
);
446478
}
447479

448480
#[cfg(feature = "dhat-heap")]

0 commit comments

Comments
 (0)