Skip to content

Commit 34965be

Browse files
chore(m.m/.,m
1 parent fc72e48 commit 34965be

File tree

3 files changed

+124
-79
lines changed

3 files changed

+124
-79
lines changed

crates/starknet_mempool/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rstest.workspace = true
4343
starknet-types-core.workspace = true
4444
starknet_api = { workspace = true, features = ["testing"] }
4545
starknet_mempool_p2p_types = { workspace = true, features = ["testing"] }
46+
starknet_sequencer_metrics = { workspace = true, features = ["testing"] }
4647

4748
[package.metadata.cargo-machete]
4849
ignored = ["starknet-types-core"]

crates/starknet_mempool/src/test_utils.rs

Lines changed: 21 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use starknet_api::transaction::TransactionHash;
99
use starknet_api::{contract_address, nonce};
1010
use starknet_mempool_types::errors::MempoolError;
1111
use starknet_mempool_types::mempool_types::{AddTransactionArgs, CommitBlockArgs};
12-
use starknet_sequencer_metrics::metrics::HistogramValue;
12+
use starknet_sequencer_metrics::metrics::{HistogramValue, LossyIntoF64};
1313

1414
use crate::mempool::Mempool;
1515
use crate::metrics::{
@@ -107,50 +107,6 @@ macro_rules! tx {
107107
};
108108
}
109109

110-
// TODO(Yael): Consider moving to a more general place.
111-
/// Compares a metric, by it's name and label if exists, from a metrics str with an expected value
112-
/// and asserts if false.
113-
#[macro_export]
114-
macro_rules! assert_metric_eq {
115-
($metrics:expr, $expected:expr, $metric:ident $(, $labels:expr)?) => {
116-
let return_value = $metric.parse_numeric_metric::<u64>($metrics $(, $labels)?).unwrap();
117-
assert_eq!(
118-
return_value,
119-
$expected,
120-
"Metric {} did not match expected value. expected value: {}, returned value: {}",
121-
stringify!($metric $(, $labels)?), $expected, return_value
122-
123-
);
124-
};
125-
}
126-
127-
/// Compares a histogram metric value to the expected value. Only the sum and count values are
128-
/// compared, not the quantiles.
129-
#[macro_export]
130-
macro_rules! assert_metric_histogram_eq {
131-
($metrics:expr, $expected:expr, $metric:ident) => {
132-
let return_value = $metric.parse_histogram_metric($metrics).unwrap();
133-
assert_eq!(
134-
return_value.sum,
135-
$expected.sum,
136-
"Histogram {} sum did not match expected value. expected value: {:?}, returned value: \
137-
{:?}",
138-
stringify!($metric),
139-
$expected.sum,
140-
return_value.sum
141-
);
142-
assert_eq!(
143-
return_value.count,
144-
$expected.count,
145-
"Histogram {} count did not match expected value. expected value: {:?}, returned \
146-
value: {:?}",
147-
stringify!($metric),
148-
$expected.count,
149-
return_value.count
150-
);
151-
};
152-
}
153-
154110
/// Creates an input for `add_tx` with the given field subset (the rest receive default values).
155111
#[macro_export]
156112
macro_rules! add_tx_input {
@@ -367,57 +323,43 @@ pub struct MempoolMetrics {
367323
impl MempoolMetrics {
368324
pub fn verify_metrics(&self, recorder: &PrometheusRecorder) {
369325
let metrics = &recorder.handle().render();
370-
assert_metric_eq!(
326+
MEMPOOL_TRANSACTIONS_RECEIVED.assert_eq(
371327
metrics,
372328
self.txs_received_invoke,
373-
MEMPOOL_TRANSACTIONS_RECEIVED,
374-
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Invoke.into())]
329+
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Invoke.into())],
375330
);
376-
assert_metric_eq!(
331+
MEMPOOL_TRANSACTIONS_RECEIVED.assert_eq(
377332
metrics,
378333
self.txs_received_declare,
379-
MEMPOOL_TRANSACTIONS_RECEIVED,
380-
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Declare.into())]
334+
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Declare.into())],
381335
);
382-
assert_metric_eq!(
336+
MEMPOOL_TRANSACTIONS_RECEIVED.assert_eq(
383337
metrics,
384338
self.txs_received_deploy_account,
385-
MEMPOOL_TRANSACTIONS_RECEIVED,
386-
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::DeployAccount.into())]
339+
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::DeployAccount.into())],
387340
);
388-
389-
assert_metric_eq!(metrics, self.txs_committed, MEMPOOL_TRANSACTIONS_COMMITTED);
390-
391-
assert_metric_eq!(
341+
MEMPOOL_TRANSACTIONS_COMMITTED.assert_eq(metrics, self.txs_committed);
342+
MEMPOOL_TRANSACTIONS_DROPPED.assert_eq(
392343
metrics,
393344
self.txs_dropped_expired,
394-
MEMPOOL_TRANSACTIONS_DROPPED,
395-
&[(LABEL_NAME_DROP_REASON, DropReason::Expired.into())]
345+
&[(LABEL_NAME_DROP_REASON, DropReason::Expired.into())],
396346
);
397-
398-
assert_metric_eq!(
347+
MEMPOOL_TRANSACTIONS_DROPPED.assert_eq(
399348
metrics,
400349
self.txs_dropped_failed_add_tx_checks,
401-
MEMPOOL_TRANSACTIONS_DROPPED,
402-
&[(LABEL_NAME_DROP_REASON, DropReason::FailedAddTxChecks.into())]
350+
&[(LABEL_NAME_DROP_REASON, DropReason::FailedAddTxChecks.into())],
403351
);
404-
405-
assert_metric_eq!(
352+
MEMPOOL_TRANSACTIONS_DROPPED.assert_eq(
406353
metrics,
407354
self.txs_dropped_rejected,
408-
MEMPOOL_TRANSACTIONS_DROPPED,
409-
&[(LABEL_NAME_DROP_REASON, DropReason::Rejected.into())]
410-
);
411-
412-
assert_metric_eq!(metrics, self.pool_size, MEMPOOL_POOL_SIZE);
413-
assert_metric_eq!(metrics, self.priority_queue_size, MEMPOOL_PRIORITY_QUEUE_SIZE);
414-
assert_metric_eq!(metrics, self.pending_queue_size, MEMPOOL_PENDING_QUEUE_SIZE);
415-
assert_metric_eq!(metrics, self.get_txs_size, MEMPOOL_GET_TXS_SIZE);
416-
assert_metric_eq!(metrics, self.delayed_declares_size, MEMPOOL_DELAYED_DECLARES_SIZE);
417-
assert_metric_histogram_eq!(
418-
metrics,
419-
self.transaction_time_spent_in_mempool,
420-
TRANSACTION_TIME_SPENT_IN_MEMPOOL
355+
&[(LABEL_NAME_DROP_REASON, DropReason::Rejected.into())],
421356
);
357+
MEMPOOL_POOL_SIZE.assert_eq(metrics, self.pool_size.into_f64());
358+
MEMPOOL_PRIORITY_QUEUE_SIZE.assert_eq(metrics, self.priority_queue_size.into_f64());
359+
MEMPOOL_PENDING_QUEUE_SIZE.assert_eq(metrics, self.pending_queue_size.into_f64());
360+
MEMPOOL_GET_TXS_SIZE.assert_eq(metrics, self.get_txs_size.into_f64());
361+
MEMPOOL_DELAYED_DECLARES_SIZE.assert_eq(metrics, self.delayed_declares_size.into_f64());
362+
TRANSACTION_TIME_SPENT_IN_MEMPOOL
363+
.assert_eq(metrics, &self.transaction_time_spent_in_mempool);
422364
}
423365
}

crates/starknet_sequencer_metrics/src/metrics.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ impl MetricCounter {
7373
pub fn parse_numeric_metric<T: Num + FromStr>(&self, metrics_as_string: &str) -> Option<T> {
7474
parse_numeric_metric::<T>(metrics_as_string, self.get_name(), None)
7575
}
76+
77+
#[cfg(any(feature = "testing", test))]
78+
pub fn assert_eq(&self, metrics_as_string: &str, expected_value: u64) {
79+
let metric_value = self.parse_numeric_metric::<u64>(metrics_as_string).unwrap();
80+
assert_eq!(
81+
metric_value,
82+
expected_value,
83+
"Metric counter {} did not match the expected value. expected value: {}, metric \
84+
value: {}",
85+
self.get_name(),
86+
expected_value,
87+
metric_value
88+
);
89+
}
7690
}
7791

7892
pub struct LabeledMetricCounter {
@@ -124,6 +138,26 @@ impl LabeledMetricCounter {
124138
) -> Option<T> {
125139
parse_numeric_metric::<T>(metrics_as_string, self.get_name(), Some(labels))
126140
}
141+
142+
#[cfg(any(feature = "testing", test))]
143+
pub fn assert_eq(
144+
&self,
145+
metrics_as_string: &str,
146+
expected_value: u64,
147+
label: &[(&'static str, &'static str)],
148+
) {
149+
let metric_value = self.parse_numeric_metric::<u64>(metrics_as_string, label).unwrap();
150+
assert_eq!(
151+
metric_value,
152+
expected_value,
153+
"Metric counter {} {:?} did not match the expected value. expected value: {}, metric
154+
value: {}",
155+
self.get_name(),
156+
label,
157+
expected_value,
158+
metric_value
159+
);
160+
}
127161
}
128162

129163
pub struct MetricGauge {
@@ -173,6 +207,20 @@ impl MetricGauge {
173207
pub fn set_lossy<T: LossyIntoF64>(&self, value: T) {
174208
gauge!(self.name).set(value.into_f64());
175209
}
210+
211+
#[cfg(any(feature = "testing", test))]
212+
pub fn assert_eq(&self, metrics_as_string: &str, expected_value: f64) {
213+
let metric_value = self.parse_numeric_metric::<f64>(metrics_as_string).unwrap();
214+
assert_eq!(
215+
metric_value,
216+
expected_value,
217+
"Metric gauge {} did not match the expected value. expected value: {}, metric
218+
value: {}",
219+
self.get_name(),
220+
expected_value,
221+
metric_value
222+
);
223+
}
176224
}
177225

178226
/// An object which can be lossy converted into a `f64` representation.
@@ -255,6 +303,26 @@ impl LabeledMetricGauge {
255303
pub fn set<T: IntoF64>(&self, value: T, label: &[(&'static str, &'static str)]) {
256304
gauge!(self.name, label).set(value.into_f64());
257305
}
306+
307+
#[cfg(any(feature = "testing", test))]
308+
pub fn assert_eq(
309+
&self,
310+
metrics_as_string: &str,
311+
expected_value: f64,
312+
label: &[(&'static str, &'static str)],
313+
) {
314+
let metric_value = self.parse_numeric_metric::<f64>(metrics_as_string, label).unwrap();
315+
assert_eq!(
316+
metric_value,
317+
expected_value,
318+
"Metric gauge {} {:?} did not match the expected value. expected value: {}, metric
319+
value: {}",
320+
self.get_name(),
321+
label,
322+
expected_value,
323+
metric_value
324+
);
325+
}
258326
}
259327

260328
pub struct MetricHistogram {
@@ -303,6 +371,20 @@ impl MetricHistogram {
303371
pub fn parse_histogram_metric(&self, metrics_as_string: &str) -> Option<HistogramValue> {
304372
parse_histogram_metric(metrics_as_string, self.get_name(), None)
305373
}
374+
375+
// Only the sum and count values are compared, not the quantiles.
376+
#[cfg(any(feature = "testing", test))]
377+
pub fn assert_eq(&self, metrics_as_string: &str, expected_value: &HistogramValue) {
378+
let metric_value = self.parse_histogram_metric(metrics_as_string).unwrap();
379+
assert!(
380+
metric_value.sum == expected_value.sum && metric_value.count == expected_value.count,
381+
"Metric histogram {} sum or count did not match the expected value. expected value: \
382+
{:?}, metric value: {:?}",
383+
self.get_name(),
384+
expected_value,
385+
metric_value
386+
);
387+
}
306388
}
307389

308390
pub struct LabeledMetricHistogram {
@@ -361,6 +443,26 @@ impl LabeledMetricHistogram {
361443
) -> Option<HistogramValue> {
362444
parse_histogram_metric(metrics_as_string, self.get_name(), Some(labels))
363445
}
446+
447+
// Only the sum and count values are compared, not the quantiles.
448+
#[cfg(any(feature = "testing", test))]
449+
pub fn assert_eq(
450+
&self,
451+
metrics_as_string: &str,
452+
expected_value: &HistogramValue,
453+
label: &[(&'static str, &'static str)],
454+
) {
455+
let metric_value = self.parse_histogram_metric(metrics_as_string, label).unwrap();
456+
assert!(
457+
metric_value.sum == expected_value.sum && metric_value.count == expected_value.count,
458+
"Metric histogram {} {:?} sum or count did not match the expected value. expected \
459+
value: {:?}, metric value: {:?}",
460+
self.get_name(),
461+
label,
462+
expected_value,
463+
metric_value
464+
);
465+
}
364466
}
365467

366468
/// Parses a specific numeric metric value from a metrics string.

0 commit comments

Comments
 (0)