reth_primitives_traits/constants/
gas_units.rs

1use alloc::string::String;
2use core::time::Duration;
3
4/// Represents one Kilogas, or `1_000` gas.
5pub const KILOGAS: u64 = 1_000;
6
7/// Represents one Megagas, or `1_000_000` gas.
8pub const MEGAGAS: u64 = KILOGAS * 1_000;
9
10/// Represents one Gigagas, or `1_000_000_000` gas.
11pub const GIGAGAS: u64 = MEGAGAS * 1_000;
12
13/// Represents one Teragas, or `1_000_000_000_000` gas.
14pub const TERAGAS: u64 = GIGAGAS * 1_000;
15
16/// Returns a formatted gas throughput log, showing either:
17///  * "Kgas/s", or 1,000 gas per second
18///  * "Mgas/s", or 1,000,000 gas per second
19///  * "Ggas/s", or 1,000,000,000 gas per second
20///  * "Tgas/s", or 1,000,000,000,000 gas per second
21///
22/// Depending on the magnitude of the gas throughput.
23pub fn format_gas_throughput(gas: u64, execution_duration: Duration) -> String {
24    let gas_per_second = gas as f64 / execution_duration.as_secs_f64();
25    if gas_per_second < MEGAGAS as f64 {
26        format!("{:.2}Kgas/second", gas_per_second / KILOGAS as f64)
27    } else if gas_per_second < GIGAGAS as f64 {
28        format!("{:.2}Mgas/second", gas_per_second / MEGAGAS as f64)
29    } else if gas_per_second < TERAGAS as f64 {
30        format!("{:.2}Ggas/second", gas_per_second / GIGAGAS as f64)
31    } else {
32        format!("{:.2}Tgas/second", gas_per_second / TERAGAS as f64)
33    }
34}
35
36/// Returns a formatted gas log, showing either:
37///  * "Kgas", or 1,000 gas
38///  * "Mgas", or 1,000,000 gas
39///  * "Ggas", or 1,000,000,000 gas
40///  * "Tgas", or 1,000,000,000,000 gas
41///
42/// Depending on the magnitude of gas.
43pub fn format_gas(gas: u64) -> String {
44    let gas = gas as f64;
45    if gas < MEGAGAS as f64 {
46        format!("{:.2}Kgas", gas / KILOGAS as f64)
47    } else if gas < GIGAGAS as f64 {
48        format!("{:.2}Mgas", gas / MEGAGAS as f64)
49    } else if gas < TERAGAS as f64 {
50        format!("{:.2}Ggas", gas / GIGAGAS as f64)
51    } else {
52        format!("{:.2}Tgas", gas / TERAGAS as f64)
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_gas_fmt() {
62        let gas = 888;
63        let gas_unit = format_gas(gas);
64        assert_eq!(gas_unit, "0.89Kgas");
65
66        let gas = 100_000;
67        let gas_unit = format_gas(gas);
68        assert_eq!(gas_unit, "100.00Kgas");
69
70        let gas = 100_000_000;
71        let gas_unit = format_gas(gas);
72        assert_eq!(gas_unit, "100.00Mgas");
73
74        let gas = 100_000_000_000;
75        let gas_unit = format_gas(gas);
76        assert_eq!(gas_unit, "100.00Ggas");
77
78        let gas = 100_000_000_000_000;
79        let gas_unit = format_gas(gas);
80        assert_eq!(gas_unit, "100.00Tgas");
81    }
82
83    #[test]
84    fn test_gas_throughput_fmt() {
85        let duration = Duration::from_secs(1);
86        let gas = 100_000;
87        let throughput = format_gas_throughput(gas, duration);
88        assert_eq!(throughput, "100.00Kgas/second");
89
90        let gas = 100_000_000;
91        let throughput = format_gas_throughput(gas, duration);
92        assert_eq!(throughput, "100.00Mgas/second");
93
94        let gas = 100_000_000_000;
95        let throughput = format_gas_throughput(gas, duration);
96        assert_eq!(throughput, "100.00Ggas/second");
97    }
98}