Skip to content

Commit

Permalink
improve TPS summary + add it to importer-online (#935)
Browse files Browse the repository at this point in the history
* improve precision of TPS summary (importer-offline)
* add TPS summary to importer-online
  • Loading branch information
marcospb19-cw authored May 27, 2024
1 parent 010e11d commit c34d3b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/bin/importer_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use stratus::eth::storage::InMemoryPermanentStorage;
use stratus::eth::storage::StratusStorage;
use stratus::eth::BlockMiner;
use stratus::eth::Executor;
use stratus::utils::calculate_tps_and_bpm;
use stratus::GlobalServices;
use stratus::GlobalState;
use tokio::runtime::Handle;
Expand Down Expand Up @@ -175,21 +176,15 @@ async fn execute_block_importer(
}
}

let seconds_elapsed = match instant_before_execution.elapsed().as_secs() as usize {
// avoid division by zero
0 => 1,
non_zero => non_zero,
};
let tps = transaction_count.checked_div(seconds_elapsed).unwrap_or(transaction_count);
let minutes_elapsed = seconds_elapsed as f64 / 60.0;
let blocks_per_minute = blocks_len as f64 / minutes_elapsed;
let duration = instant_before_execution.elapsed();
let (tps, bpm) = calculate_tps_and_bpm(duration, transaction_count, blocks_len);

tracing::info!(
tps,
blocks_per_minute = format_args!("{blocks_per_minute:.2}"),
seconds_elapsed,
blocks_per_minute = format_args!("{bpm:.2}"),
?duration,
%block_start,
%block_end,
transaction_count,
receipts = receipts.len(),
"reexecuted blocks batch",
);
Expand Down
17 changes: 16 additions & 1 deletion src/bin/importer_online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ use stratus::infra::metrics;
use stratus::infra::tracing::warn_task_rx_closed;
use stratus::infra::tracing::warn_task_tx_closed;
use stratus::infra::BlockchainClient;
use stratus::utils::calculate_tps;
use stratus::GlobalServices;
use stratus::GlobalState;
use tokio::sync::mpsc;
use tokio::task::yield_now;
use tokio::time::sleep;
use tokio::time::timeout;
use tokio::time::Instant;

// -----------------------------------------------------------------------------
// Globals
Expand Down Expand Up @@ -134,11 +136,24 @@ async fn start_block_executor(executor: Arc<Executor>, miner: Arc<BlockMiner>, m
// execute and mine
let receipts = ExternalReceipts::from(receipts);

tracing::info!(number = %block.number(), txs_len = block.transactions.len(), "reexecuting external block");
let instant_before_execution = Instant::now();

if executor.reexecute_external(&block, &receipts).await.is_err() {
GlobalState::shutdown_from(TASK_NAME, "failed to re-execute external block");
return;
};

let duration = instant_before_execution.elapsed();
let tps = calculate_tps(duration, block.transactions.len());

tracing::info!(
tps,
?duration,
block_number = ?block.number(),
receipts = receipts.len(),
"reexecuted external block",
);

if miner.mine_external_mixed_and_commit().await.is_err() {
GlobalState::shutdown_from(TASK_NAME, "failed to mine external block");
return;
Expand Down
14 changes: 14 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use tokio::select;
use tokio::signal::unix::signal;
use tokio::signal::unix::SignalKind;
Expand Down Expand Up @@ -36,3 +38,15 @@ pub async fn spawn_signal_handler() -> anyhow::Result<()> {

Ok(())
}

pub fn calculate_tps_and_bpm(duration: Duration, transaction_count: usize, block_count: usize) -> (f64, f64) {
let seconds_elapsed = duration.as_secs_f64() + f64::EPSILON;
let tps = transaction_count as f64 / seconds_elapsed;
let blocks_per_minute = block_count as f64 / (seconds_elapsed / 60.0);
(tps, blocks_per_minute)
}

pub fn calculate_tps(duration: Duration, transaction_count: usize) -> f64 {
let seconds_elapsed = duration.as_secs_f64() + f64::EPSILON;
transaction_count as f64 / seconds_elapsed
}

0 comments on commit c34d3b1

Please sign in to comment.