Skip to content

Commit 874aafa

Browse files
committed
Rename fuzzing gas info
1 parent 718c2eb commit 874aafa

File tree

3 files changed

+94
-84
lines changed

3 files changed

+94
-84
lines changed

crates/forge-runner/src/gas.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use cheatnet::state::ExtendedStateReader;
1616
use starknet_api::execution_resources::{GasAmount, GasVector};
1717
use starknet_api::transaction::fields::GasVectorComputationMode;
1818

19+
pub mod stats;
20+
1921
#[tracing::instrument(skip_all, level = "debug")]
2022
pub fn calculate_used_gas(
2123
transaction_context: &TransactionContext,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use num_traits::Pow;
2+
3+
#[derive(Debug, PartialEq, Clone, Default)]
4+
pub struct GasStats {
5+
pub min: u64,
6+
pub max: u64,
7+
pub mean: f64,
8+
pub std_deviation: f64,
9+
}
10+
11+
impl GasStats {
12+
#[must_use]
13+
pub fn new(gas_usages: &[u64]) -> Self {
14+
let mean = mean(gas_usages);
15+
Self {
16+
min: *gas_usages.iter().min().unwrap(),
17+
max: *gas_usages.iter().max().unwrap(),
18+
mean,
19+
std_deviation: std_deviation(mean, gas_usages),
20+
}
21+
}
22+
}
23+
24+
#[expect(clippy::cast_precision_loss)]
25+
fn mean(values: &[u64]) -> f64 {
26+
let sum: f64 = values.iter().map(|&x| x as f64).sum();
27+
sum / values.len() as f64
28+
}
29+
30+
#[expect(clippy::cast_precision_loss)]
31+
fn std_deviation(mean: f64, values: &[u64]) -> f64 {
32+
let sum_squared_diff = values
33+
.iter()
34+
.map(|&x| (x as f64 - mean).pow(2))
35+
.sum::<f64>();
36+
37+
(sum_squared_diff / values.len() as f64).sqrt()
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::{mean, std_deviation};
43+
44+
const FLOAT_ERROR: f64 = 0.01;
45+
46+
#[test]
47+
fn test_mean_basic() {
48+
let data = [1, 2, 3, 4, 5];
49+
let result = mean(&data);
50+
assert!((result - 3.0).abs() < FLOAT_ERROR);
51+
}
52+
53+
#[test]
54+
fn test_mean_single_element() {
55+
let data = [42];
56+
let result = mean(&data);
57+
assert!((result - 42.0).abs() < FLOAT_ERROR);
58+
}
59+
60+
#[test]
61+
fn test_std_deviation_basic() {
62+
let data = [1, 2, 3, 4, 5];
63+
let mean_value = mean(&data);
64+
let result = std_deviation(mean_value, &data);
65+
assert!((result - 1.414).abs() < FLOAT_ERROR);
66+
}
67+
68+
#[test]
69+
fn test_std_deviation_single_element() {
70+
let data = [10];
71+
let mean_value = mean(&data);
72+
let result = std_deviation(mean_value, &data);
73+
assert!(result.abs() < FLOAT_ERROR);
74+
}
75+
}

crates/forge-runner/src/test_case_summary.rs

Lines changed: 17 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::build_trace_data::build_profiler_call_trace;
33
use crate::debugging::{TraceArgs, build_debugging_trace};
44
use crate::expected_result::{ExpectedPanicValue, ExpectedTestResult};
55
use crate::gas::check_available_gas;
6+
use crate::gas::stats::GasStats;
67
use crate::package_tests::with_config_resolved::TestCaseWithResolvedConfig;
78
use crate::running::{RunCompleted, RunStatus};
89
use cairo_annotations::trace_data::VersionedCallTrace as VersionedProfilerCallTrace;
@@ -11,21 +12,20 @@ use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::Use
1112
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
1213
use conversions::byte_array::ByteArray;
1314
use conversions::felt::ToShortString;
14-
use num_traits::Pow;
1515
use shared::utils::build_readable_text;
1616
use starknet_api::execution_resources::GasVector;
1717
use starknet_types_core::felt::Felt;
1818
use std::fmt;
1919
use std::option::Option;
2020

2121
#[derive(Debug, PartialEq, Clone, Default)]
22-
pub struct GasStatistics {
23-
pub l1_gas: GasStatisticsComponent,
24-
pub l1_data_gas: GasStatisticsComponent,
25-
pub l2_gas: GasStatisticsComponent,
22+
pub struct GasFuzzingInfo {
23+
pub l1_gas: GasStats,
24+
pub l1_data_gas: GasStats,
25+
pub l2_gas: GasStats,
2626
}
2727

28-
impl fmt::Display for GasStatistics {
28+
impl fmt::Display for GasFuzzingInfo {
2929
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3030
write!(
3131
f,
@@ -35,28 +35,7 @@ impl fmt::Display for GasStatistics {
3535
}
3636
}
3737

38-
#[derive(Debug, PartialEq, Clone, Default)]
39-
pub struct GasStatisticsComponent {
40-
pub min: u64,
41-
pub max: u64,
42-
pub mean: f64,
43-
pub std_deviation: f64,
44-
}
45-
46-
impl GasStatisticsComponent {
47-
#[must_use]
48-
pub fn new(gas_usages: &[u64]) -> Self {
49-
let mean = GasStatistics::mean(gas_usages);
50-
Self {
51-
min: *gas_usages.iter().min().unwrap(),
52-
max: *gas_usages.iter().max().unwrap(),
53-
mean,
54-
std_deviation: GasStatistics::std_deviation(mean, gas_usages),
55-
}
56-
}
57-
}
58-
59-
impl fmt::Display for GasStatisticsComponent {
38+
impl fmt::Display for GasStats {
6039
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6140
write!(
6241
f,
@@ -66,35 +45,19 @@ impl fmt::Display for GasStatisticsComponent {
6645
}
6746
}
6847

69-
impl GasStatistics {
48+
impl GasFuzzingInfo {
7049
#[must_use]
7150
pub fn new(gas_usages: &[GasVector]) -> Self {
7251
let l1_gas_values: Vec<u64> = gas_usages.iter().map(|gv| gv.l1_gas.0).collect();
7352
let l1_data_gas_values: Vec<u64> = gas_usages.iter().map(|gv| gv.l1_data_gas.0).collect();
7453
let l2_gas_values: Vec<u64> = gas_usages.iter().map(|gv| gv.l2_gas.0).collect();
7554

76-
GasStatistics {
77-
l1_gas: { GasStatisticsComponent::new(l1_gas_values.as_ref()) },
78-
l1_data_gas: { GasStatisticsComponent::new(l1_data_gas_values.as_ref()) },
79-
l2_gas: { GasStatisticsComponent::new(l2_gas_values.as_ref()) },
55+
GasFuzzingInfo {
56+
l1_gas: { GasStats::new(l1_gas_values.as_ref()) },
57+
l1_data_gas: { GasStats::new(l1_data_gas_values.as_ref()) },
58+
l2_gas: { GasStats::new(l2_gas_values.as_ref()) },
8059
}
8160
}
82-
83-
#[expect(clippy::cast_precision_loss)]
84-
fn mean(gas_usages: &[u64]) -> f64 {
85-
let sum: f64 = gas_usages.iter().map(|&x| x as f64).sum();
86-
sum / gas_usages.len() as f64
87-
}
88-
89-
#[expect(clippy::cast_precision_loss)]
90-
fn std_deviation(mean: f64, gas_usages: &[u64]) -> f64 {
91-
let sum_squared_diff = gas_usages
92-
.iter()
93-
.map(|&x| (x as f64 - mean).pow(2))
94-
.sum::<f64>();
95-
96-
(sum_squared_diff / gas_usages.len() as f64).sqrt()
97-
}
9861
}
9962

10063
#[derive(Debug, PartialEq, Clone)]
@@ -111,7 +74,7 @@ pub trait TestType {
11174
#[derive(Debug, PartialEq, Clone)]
11275
pub struct Fuzzing;
11376
impl TestType for Fuzzing {
114-
type GasInfo = GasStatistics;
77+
type GasInfo = GasFuzzingInfo;
11578
type TestStatistics = FuzzingStatistics;
11679
type TraceData = ();
11780
}
@@ -240,7 +203,7 @@ impl TestCaseSummary<Fuzzing> {
240203
TestCaseSummary::Passed {
241204
name,
242205
msg,
243-
gas_info: GasStatistics::new(gas_usages.as_ref()),
206+
gas_info: GasFuzzingInfo::new(gas_usages.as_ref()),
244207
used_resources: UsedResources::default(),
245208
test_statistics: FuzzingStatistics { runs },
246209
trace_data: (),
@@ -495,41 +458,11 @@ impl AnyTestCaseSummary {
495458

496459
#[cfg(test)]
497460
mod tests {
498-
use super::*;
499-
use starknet_api::execution_resources::GasAmount;
461+
use crate::test_case_summary::*;
462+
use starknet_api::execution_resources::{GasAmount, GasVector};
500463

501464
const FLOAT_ERROR: f64 = 0.01;
502465

503-
#[test]
504-
fn test_mean_basic() {
505-
let data = [1, 2, 3, 4, 5];
506-
let result = GasStatistics::mean(&data);
507-
assert!((result - 3.0).abs() < FLOAT_ERROR);
508-
}
509-
510-
#[test]
511-
fn test_mean_single_element() {
512-
let data = [42];
513-
let result = GasStatistics::mean(&data);
514-
assert!((result - 42.0).abs() < FLOAT_ERROR);
515-
}
516-
517-
#[test]
518-
fn test_std_deviation_basic() {
519-
let data = [1, 2, 3, 4, 5];
520-
let mean_value = GasStatistics::mean(&data);
521-
let result = GasStatistics::std_deviation(mean_value, &data);
522-
assert!((result - 1.414).abs() < FLOAT_ERROR);
523-
}
524-
525-
#[test]
526-
fn test_std_deviation_single_element() {
527-
let data = [10];
528-
let mean_value = GasStatistics::mean(&data);
529-
let result = GasStatistics::std_deviation(mean_value, &data);
530-
assert!(result.abs() < FLOAT_ERROR);
531-
}
532-
533466
#[test]
534467
fn test_gas_statistics_new() {
535468
let gas_usages = vec![
@@ -550,7 +483,7 @@ mod tests {
550483
},
551484
];
552485

553-
let stats = GasStatistics::new(&gas_usages);
486+
let stats = GasFuzzingInfo::new(&gas_usages);
554487

555488
assert_eq!(stats.l1_gas.min, 10);
556489
assert_eq!(stats.l1_data_gas.min, 20);

0 commit comments

Comments
 (0)