diff --git a/ckb-bench/src/main.rs b/ckb-bench/src/main.rs index 415a402..8bba3bd 100644 --- a/ckb-bench/src/main.rs +++ b/ckb-bench/src/main.rs @@ -8,7 +8,7 @@ mod watcher; mod tests; use crate::bench::{LiveCellProducer, TransactionProducer}; -use crate::prepare::{collect, dispatch, generate_privkeys}; +use crate::prepare::{collect, dispatch, derive_privkeys}; use crate::utils::maybe_retry_send_transaction; use crate::watcher::Watcher; use ckb_crypto::secp::Privkey; @@ -152,7 +152,7 @@ pub fn entrypoint(clap_arg_match: ArgMatches<'static>) { err ) }); - let privkeys = generate_privkeys(owner_byte32_privkey, n_users); + let privkeys = derive_privkeys(owner_byte32_privkey, n_users); privkeys .into_iter() .map(|privkey| User::new(genesis_block.clone(), Some(privkey))) @@ -207,7 +207,7 @@ pub fn entrypoint(clap_arg_match: ArgMatches<'static>) { err ) }); - let privkeys = generate_privkeys(owner_byte32_privkey, n_users); + let privkeys = derive_privkeys(owner_byte32_privkey, n_users); privkeys .into_iter() .map(|privkey| User::new(genesis_block.clone(), Some(privkey))) @@ -238,7 +238,7 @@ pub fn entrypoint(clap_arg_match: ArgMatches<'static>) { .collect::>(); let n_users = value_t_or_exit!(arguments, "n-users", usize); let n_inout = value_t_or_exit!(arguments, "n-inout", usize); - let t_delay = { + let t_tx_interval = { let tx_interval_ms = value_t_or_exit!(arguments, "tx-interval-ms", u64); Duration::from_millis(tx_interval_ms) }; @@ -262,18 +262,22 @@ pub fn entrypoint(clap_arg_match: ArgMatches<'static>) { err ) }); - let privkeys = generate_privkeys(owner_byte32_privkey, n_users); + let privkeys = derive_privkeys(owner_byte32_privkey, n_users); privkeys .into_iter() .map(|privkey| User::new(genesis_block.clone(), Some(privkey))) .collect::>() }; let is_smoking_test = arguments.is_present("is-smoking-test"); - let (live_cell_sender, live_cell_receiver) = bounded(100000); - let (transaction_sender, transaction_receiver) = bounded(100000); + let (live_cell_sender, live_cell_receiver) = bounded(10000000); + let (transaction_sender, transaction_receiver) = bounded(1000000); wait_for_nodes_sync(&nodes); wait_for_indexer_synced(&nodes); + ckb_testkit::info!( + "bench with params --n-users {} --n-inout {} --tx-interval-ms {} --bench-time-ms {}", + users.len(), n_inout, t_tx_interval.as_millis(), t_bench.as_millis(), + ); let live_cell_producer = LiveCellProducer::new(users.clone(), nodes.clone()); spawn(move || { @@ -305,25 +309,18 @@ pub fn entrypoint(clap_arg_match: ArgMatches<'static>) { let start_time = Instant::now(); let mut last_log_time = Instant::now(); let mut benched_transactions = 0u64; - ckb_testkit::info!( - "bench start with params n_inout={}, t_delay={:?}, t_bench={:?}", - n_inout, - t_delay, - t_bench - ); loop { let tx = transaction_receiver .recv_timeout(Duration::from_secs(60 * 3)) .expect("timeout to wait transaction_receiver"); - if t_delay.as_millis() != 0 { - sleep(t_delay); + if t_tx_interval.as_millis() != 0 { + sleep(t_tx_interval); } loop { i = (i + 1) % nodes.len(); match maybe_retry_send_transaction(&nodes[i], &tx) { - Ok(hash) => { - ckb_testkit::debug!("sent transaction {:#x}", hash); + Ok(_hash) => { benched_transactions += 1; break; } @@ -363,7 +360,7 @@ pub fn entrypoint(clap_arg_match: ArgMatches<'static>) { zero_load_number + 1, fixed_tip_number, t_stat, - Some(t_delay), + Some(t_tx_interval), ); ckb_testkit::info!("metrics: {}", serde_json::json!(metrics)); } diff --git a/ckb-bench/src/prepare.rs b/ckb-bench/src/prepare.rs index 2de1f5b..e1a3da1 100644 --- a/ckb-bench/src/prepare.rs +++ b/ckb-bench/src/prepare.rs @@ -26,7 +26,7 @@ pub fn dispatch( capacity_per_cell: u64, ) { ckb_testkit::info!( - "dispatch to {} users, {} cells per user, {} capacity per cell", + "dispatch with params --n-users {} --cells-per-user {} --capacity-per-cell {}", users.len(), cells_per_user, capacity_per_cell @@ -54,11 +54,10 @@ pub fn dispatch( } let total_outs = users.len() * cells_per_user as usize; - let index_user = |out_i: usize| out_i % (cells_per_user as usize); + let index_user = |out_i: usize| out_i / (cells_per_user as usize); - ckb_testkit::info!("constructing transactions..."); + let mut last_logging_time = Instant::now(); let mut i_out = 0usize; - let mut txs = Vec::new(); while let Some(input) = live_cells.pop_front() { let input_capacity = input.capacity().as_u64(); // TODO estimate tx fee @@ -108,7 +107,18 @@ pub fn dispatch( .build() }; - txs.push(signed_tx.clone()); + let result = maybe_retry_send_transaction(&nodes[0], &signed_tx); + if last_logging_time.elapsed() > Duration::from_secs(30) { + last_logging_time = Instant::now(); + ckb_testkit::info!("dispatch {}/{} outputs", i_out + 1, total_outs) + } + assert!( + result.is_ok(), + "dispatch-transaction {:#x} should be ok but got {}", + signed_tx.hash(), + result.unwrap_err() + ); + i_out += n_outs; if i_out == total_outs { break; @@ -128,27 +138,15 @@ pub fn dispatch( } } - assert!(i_out == total_outs); - let total_txs = txs.len(); - let mut last_logging_time = Instant::now(); - for (i, tx) in txs.iter().enumerate() { - let result = maybe_retry_send_transaction(&nodes[0], tx); - if last_logging_time.elapsed() > Duration::from_secs(30) { - last_logging_time = Instant::now(); - ckb_testkit::info!("sent {}/{} collect-transactions", i + 1, total_txs) - } - assert!( - result.is_ok(), - "dispatch-transaction should be ok but got {}", - result.unwrap_err() - ); - } + ckb_testkit::info!("finished dispatch"); + assert!(i_out >= total_outs); } pub fn collect(nodes: &[Node], owner: &User, users: &[User]) { - ckb_testkit::info!("collect {} users' capacity", users.len()); - let mut txs = Vec::new(); - for user in users.iter() { + ckb_testkit::info!("collect with params --n-users {}", users.len()); + let n_users = users.len(); + let mut last_logging_time = Instant::now(); + for (i_user, user) in users.iter().enumerate() { let live_cells = user.get_spendable_single_secp256k1_cells(&nodes[0]); if live_cells.is_empty() { continue; @@ -180,27 +178,23 @@ pub fn collect(nodes: &[Node], owner: &User, users: &[User]) { .as_advanced_builder() .set_witnesses(vec![witness]) .build(); - txs.push(signed_tx); - } - } - - let total_txs = txs.len(); - let mut last_logging_time = Instant::now(); - for (i, tx) in txs.iter().enumerate() { - let result = maybe_retry_send_transaction(&nodes[0], tx); - if last_logging_time.elapsed() > Duration::from_secs(30) { - last_logging_time = Instant::now(); - ckb_testkit::info!("sent {}/{} collect-transactions", i + 1, total_txs) + let result = maybe_retry_send_transaction(&nodes[0], &signed_tx); + if last_logging_time.elapsed() > Duration::from_secs(30) { + last_logging_time = Instant::now(); + ckb_testkit::info!("already collected {}/{} users", i_user, n_users) + } + assert!( + result.is_ok(), + "collect-transaction {:#x} should be ok but got {}", + signed_tx.hash(), + result.unwrap_err() + ); } - assert!( - result.is_ok(), - "collect-transaction should be ok but got {}", - result.unwrap_err() - ); } + ckb_testkit::info!("finished collecting"); } -pub fn generate_privkeys(basic_raw_privkey: Byte32, n: usize) -> Vec { +pub fn derive_privkeys(basic_raw_privkey: Byte32, n: usize) -> Vec { let mut raw_privkeys = (0..n).fold(vec![basic_raw_privkey], |mut raw_privkeys, _| { let last_raw_privkey = raw_privkeys.last().unwrap(); let next_raw_privkey = ckb_hash::blake2b_256(last_raw_privkey.as_bytes()).pack(); diff --git a/ckb-bench/src/tests/bench.rs b/ckb-bench/src/tests/bench.rs index ddd66ac..bcb3b39 100644 --- a/ckb-bench/src/tests/bench.rs +++ b/ckb-bench/src/tests/bench.rs @@ -1,4 +1,4 @@ -use crate::prepare::generate_privkeys; +use crate::prepare::derive_privkeys; use crate::tests::node_options; use crate::{clap_app, entrypoint, init_logger}; use ckb_testkit::{Node, Nodes, User}; @@ -39,7 +39,7 @@ fn test_bench() { let h256 = H256::from_str(owner_raw_privkey).unwrap(); Byte32::from_slice(h256.as_bytes()).unwrap() }; - generate_privkeys(owner_byte32_privkey, n_users) + derive_privkeys(owner_byte32_privkey, n_users) .into_iter() .map(|privkey| User::new(genesis_block.clone(), Some(privkey))) .collect() diff --git a/ckb-bench/src/tests/prepare.rs b/ckb-bench/src/tests/prepare.rs index 6748ddd..1eaa8e5 100644 --- a/ckb-bench/src/tests/prepare.rs +++ b/ckb-bench/src/tests/prepare.rs @@ -1,4 +1,4 @@ -use crate::prepare::generate_privkeys; +use crate::prepare::derive_privkeys; use crate::tests::node_options; use crate::{clap_app, entrypoint, init_logger}; use ckb_testkit::{Node, Nodes, User}; @@ -36,7 +36,7 @@ fn test_prepare() { let h256 = H256::from_str(owner_raw_privkey).unwrap(); Byte32::from_slice(h256.as_bytes()).unwrap() }; - let users: Vec<_> = generate_privkeys(owner_byte32_privkey, n_users) + let users: Vec<_> = derive_privkeys(owner_byte32_privkey, n_users) .into_iter() .map(|privkey| User::new(genesis_block.clone(), Some(privkey))) .collect();