Skip to content

Commit

Permalink
chore: calculate the tps amount (#565)
Browse files Browse the repository at this point in the history
* chore: calculate the tps amount

* chore: using mutex in order to reset tps calc
  • Loading branch information
renancloudwalk authored Apr 11, 2024
1 parent 4dafaf6 commit 79b84ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rand = "=0.8.5"
strum = "=0.25.0"
thiserror = "=1.0.56"
url = "=2.5.0"
once_cell = "1.19.0"

# async
tokio = { version = "=1.36.0", features = ["rt-multi-thread", "macros", "signal"] }
Expand Down
33 changes: 33 additions & 0 deletions src/eth/storage/rocks/rocks_permanent.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Instant;

use anyhow::Context;
use async_trait::async_trait;
use futures::future::join_all;
use once_cell::sync::Lazy;

use super::rocks_state::RocksStorageState;
use crate::eth::primitives::Account;
Expand All @@ -27,6 +31,12 @@ use crate::eth::storage::rocks::rocks_state::AccountInfo;
use crate::eth::storage::PermanentStorage;
use crate::eth::storage::StorageError;

/// used for multiple purposes, such as TPS counting and backup management
const TRANSACTION_LOOP_THRESHOLD: usize = 210_000;

static TRANSACTIONS_COUNT: AtomicUsize = AtomicUsize::new(0);
static START_TIME: Lazy<Mutex<Instant>> = Lazy::new(|| Mutex::new(Instant::now()));

#[derive(Debug)]
pub struct RocksPermanentStorage {
state: RocksStorageState,
Expand Down Expand Up @@ -184,6 +194,29 @@ impl PermanentStorage for RocksPermanentStorage {
.context("failed to update state with execution changes")?,
);

// TPS Calculation and Printing
futures.push(tokio::task::spawn_blocking(move || {
let previous_count = TRANSACTIONS_COUNT.load(Ordering::Relaxed);
let current_count = TRANSACTIONS_COUNT.fetch_add(block.transactions.len(), Ordering::Relaxed);
let elapsed_time = START_TIME.lock().unwrap().elapsed().as_secs_f64();
let multiple_to_print = TRANSACTION_LOOP_THRESHOLD / 8;

// for every multiple of transactions, print the TPS
if previous_count % multiple_to_print > current_count % multiple_to_print {
let total_transactions = TRANSACTIONS_COUNT.load(Ordering::Relaxed);
let tps = total_transactions as f64 / elapsed_time;
//TODO replace this with metrics or do a cfg feature to enable/disable
println!("Transactions per second: {:.2} @ block {}", tps, block.number());
}

// for every multiple of TRANSACTION_LOOP_THRESHOLD transactions, reset the counter
if previous_count % TRANSACTION_LOOP_THRESHOLD > current_count % TRANSACTION_LOOP_THRESHOLD {
TRANSACTIONS_COUNT.store(0, Ordering::Relaxed);
let mut start_time = START_TIME.lock().unwrap();
*start_time = Instant::now();
}
}));

join_all(futures).await;
Ok(())
}
Expand Down

0 comments on commit 79b84ac

Please sign in to comment.