Skip to content

Commit cdc0bb8

Browse files
authored
fix(core): more accurate throughput (#3412)
Throughput in the logged metrics was computed over a truncated number of seconds, which meant the same block taking 1999ms or 1000ms reports the same throughput, when one is indeed twice as slow as the other. This fixes it by asking for the `as_secs_f64` directly rather than taking an integer number of millis, dividing (with integer semantics) by 1000 and then casting.
1 parent 8940f50 commit cdc0bb8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

crates/blockchain/blockchain.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use mempool::Mempool;
3131
use std::collections::HashMap;
3232
use std::sync::atomic::{AtomicBool, Ordering};
3333
use std::sync::{Arc, Mutex};
34-
use std::{ops::Div, time::Instant};
34+
use std::time::Instant;
3535
use tokio_util::sync::CancellationToken;
3636

3737
use vm::StoreVmDatabase;
@@ -543,11 +543,11 @@ impl Blockchain {
543543
.await
544544
.map_err(|e| (e.into(), None))?;
545545

546-
let elapsed_seconds = interval.elapsed().as_millis() / 1000;
546+
let elapsed_seconds = interval.elapsed().as_secs_f64();
547547
let mut throughput = 0.0;
548-
if elapsed_seconds != 0 && total_gas_used != 0 {
549-
let as_gigas = (total_gas_used as f64).div(10_f64.powf(9_f64));
550-
throughput = (as_gigas) / (elapsed_seconds as f64);
548+
if elapsed_seconds > 0.0 && total_gas_used != 0 {
549+
let as_gigas = (total_gas_used as f64) / 1e9;
550+
throughput = as_gigas / elapsed_seconds;
551551
}
552552

553553
metrics!(

0 commit comments

Comments
 (0)