Skip to content

Commit c4e2cb6

Browse files
authored
fix(openvm-prof): replace unwraps with better error strings (#1833)
- replaced `unwrap` calls with error strings so it's easy to identify what's missing
1 parent 4a2f95e commit c4e2cb6

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

crates/prof/src/aggregate.rs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ impl AggregateMetrics {
170170
if stats.is_none() {
171171
continue;
172172
}
173-
let stats = stats.unwrap();
173+
let stats = stats.unwrap_or_else(|| {
174+
panic!("Missing proof time statistics for group '{}'", group_name)
175+
});
174176
let mut sum = stats.sum;
175177
let mut max = stats.max;
176178
// convert ms to s
@@ -185,9 +187,17 @@ impl AggregateMetrics {
185187
if !group_name.contains("keygen") {
186188
// Proving time in keygen group is dummy and not part of total.
187189
total_proof_time.val += sum.val;
188-
*total_proof_time.diff.as_mut().unwrap() += sum.diff.unwrap_or(0.0);
190+
*total_proof_time
191+
.diff
192+
.as_mut()
193+
.expect("total_proof_time.diff should be initialized") +=
194+
sum.diff.unwrap_or(0.0);
189195
total_par_proof_time.val += max.val;
190-
*total_par_proof_time.diff.as_mut().unwrap() += max.diff.unwrap_or(0.0);
196+
*total_par_proof_time
197+
.diff
198+
.as_mut()
199+
.expect("total_par_proof_time.diff should be initialized") +=
200+
max.diff.unwrap_or(0.0);
191201

192202
// Account for the serial execute_metered and execute_e1 for app outside of segments
193203
if group_name != "leaf"
@@ -203,17 +213,33 @@ impl AggregateMetrics {
203213
total_proof_time.val += execute_metered_stats.avg.val / 1000.0;
204214
total_par_proof_time.val += execute_metered_stats.avg.val / 1000.0;
205215
if let Some(diff) = execute_metered_stats.avg.diff {
206-
*total_proof_time.diff.as_mut().unwrap() += diff / 1000.0;
207-
*total_par_proof_time.diff.as_mut().unwrap() += diff / 1000.0;
216+
*total_proof_time
217+
.diff
218+
.as_mut()
219+
.expect("total_proof_time.diff should be initialized") +=
220+
diff / 1000.0;
221+
*total_par_proof_time
222+
.diff
223+
.as_mut()
224+
.expect("total_par_proof_time.diff should be initialized") +=
225+
diff / 1000.0;
208226
}
209227
}
210228

211229
if let Some(execute_e1_stats) = execute_e1_stats {
212230
total_proof_time.val += execute_e1_stats.avg.val / 1000.0;
213231
total_par_proof_time.val += execute_e1_stats.avg.val / 1000.0;
214232
if let Some(diff) = execute_e1_stats.avg.diff {
215-
*total_proof_time.diff.as_mut().unwrap() += diff / 1000.0;
216-
*total_par_proof_time.diff.as_mut().unwrap() += diff / 1000.0;
233+
*total_proof_time
234+
.diff
235+
.as_mut()
236+
.expect("total_proof_time.diff should be initialized") +=
237+
diff / 1000.0;
238+
*total_par_proof_time
239+
.diff
240+
.as_mut()
241+
.expect("total_par_proof_time.diff should be initialized") +=
242+
diff / 1000.0;
217243
}
218244
}
219245
}
@@ -251,7 +277,13 @@ impl AggregateMetrics {
251277
.into_iter()
252278
.map(|group_name| {
253279
let key = group_name.clone();
254-
let value = self.by_group.get(group_name).unwrap().clone();
280+
let value = self
281+
.by_group
282+
.get(group_name)
283+
.unwrap_or_else(|| {
284+
panic!("Group '{}' should exist in by_group map", group_name)
285+
})
286+
.clone();
255287
(key, value)
256288
})
257289
.collect()
@@ -363,7 +395,9 @@ impl AggregateMetrics {
363395
if stats.is_none() {
364396
continue;
365397
}
366-
let stats = stats.unwrap();
398+
let stats = stats.unwrap_or_else(|| {
399+
panic!("Missing proof time statistics for group '{}'", group_name)
400+
});
367401
let mut sum = stats.sum;
368402
let mut max = stats.max;
369403
// convert ms to s
@@ -394,7 +428,12 @@ impl AggregateMetrics {
394428
self.by_group
395429
.keys()
396430
.find(|k| group_weight(k) == 0)
397-
.unwrap_or_else(|| self.by_group.keys().next().unwrap())
431+
.unwrap_or_else(|| {
432+
self.by_group
433+
.keys()
434+
.next()
435+
.expect("by_group should contain at least one group")
436+
})
398437
.clone()
399438
}
400439
}

crates/prof/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ impl MetricDb {
9595

9696
let label_values: Vec<String> = label_keys
9797
.iter()
98-
.map(|key| label_dict.get(key).unwrap().clone())
98+
.map(|key| {
99+
label_dict
100+
.get(key)
101+
.unwrap_or_else(|| panic!("Label key '{}' should exist in label_dict", key))
102+
.clone()
103+
})
99104
.collect();
100105

101106
// Add to dict_by_label_types

crates/prof/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ fn main() -> Result<()> {
8484
// If this is a new benchmark, prev_path will not exist
8585
if let Ok(prev_db) = MetricDb::new(&prev_path) {
8686
let prev_grouped = GroupedMetrics::new(&prev_db, "group")?;
87-
prev_aggregated = Some(prev_grouped.aggregate());
88-
aggregated.set_diff(prev_aggregated.as_ref().unwrap());
87+
let prev_grouped_aggregated = prev_grouped.aggregate();
88+
aggregated.set_diff(&prev_grouped_aggregated);
89+
prev_aggregated = Some(prev_grouped_aggregated);
8990
}
9091
}
9192
if name.is_empty() {

crates/prof/src/summary.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ impl GithubSummary {
5656
.zip_eq(md_paths.iter())
5757
.zip_eq(names)
5858
.map(|(((aggregated, prev_aggregated), md_path), name)| {
59-
let md_filename = md_path.file_name().unwrap().to_str().unwrap();
60-
let mut row = aggregated.get_summary_row(md_filename).unwrap();
59+
let md_filename = md_path
60+
.file_name()
61+
.expect("Path should have a filename")
62+
.to_str()
63+
.expect("Filename should be valid UTF-8");
64+
let mut row = aggregated.get_summary_row(md_filename).unwrap_or_else(|| {
65+
panic!("Failed to get summary row for file '{}'", md_filename)
66+
});
6167
if let Some(prev_aggregated) = prev_aggregated {
6268
// md_filename doesn't matter
6369
if let Some(prev_row) = prev_aggregated.get_summary_row(md_filename) {

0 commit comments

Comments
 (0)