use std::time::Instant;
use metrics::{Counter, Gauge, Histogram};
use reth_execution_types::BlockExecutionInput;
use reth_metrics::Metrics;
use reth_primitives::BlockWithSenders;
#[derive(Metrics, Clone)]
#[metrics(scope = "sync.execution")]
pub struct ExecutorMetrics {
pub gas_processed_total: Counter,
pub gas_per_second: Gauge,
pub execution_histogram: Histogram,
pub execution_duration: Gauge,
}
impl ExecutorMetrics {
pub fn metered<F, R>(&self, input: BlockExecutionInput<'_, BlockWithSenders>, f: F) -> R
where
F: FnOnce(BlockExecutionInput<'_, BlockWithSenders>) -> R,
{
let gas_used = input.block.gas_used;
let execute_start = Instant::now();
let output = f(input);
let execution_duration = execute_start.elapsed().as_secs_f64();
self.gas_processed_total.increment(gas_used);
self.gas_per_second.set(gas_used as f64 / execution_duration);
self.execution_histogram.record(execution_duration);
self.execution_duration.set(execution_duration);
output
}
}