Skip to content

Commit f322900

Browse files
committed
Add metric selection
1 parent 201096a commit f322900

File tree

1 file changed

+57
-26
lines changed

1 file changed

+57
-26
lines changed

collector/src/compare/mod.rs

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ use ratatui::{
88
prelude::*,
99
widgets::Block,
1010
};
11-
use std::fmt::Display;
1211
use std::future::Future;
1312
use std::pin::Pin;
1413
use std::sync::Arc;
15-
use tabled::settings::object::{Column, Columns};
16-
use tabled::settings::Modify;
1714
use tabled::Tabled;
1815

1916
static ALL_METRICS: &[Metric] = &[
2017
Metric::InstructionsUser,
21-
Metric::Cycles,
18+
Metric::CyclesUser,
2219
Metric::WallTime,
2320
Metric::MaxRSS,
2421
Metric::LinkedArtifactSize,
@@ -31,7 +28,7 @@ static ALL_METRICS: &[Metric] = &[
3128
Metric::CpuClock,
3229
Metric::CpuClockUser,
3330
Metric::CrateMetadataSize,
34-
Metric::CyclesUser,
31+
Metric::Cycles,
3532
Metric::DepGraphSize,
3633
Metric::DocByteSize,
3734
Metric::DwoFileSize,
@@ -70,14 +67,14 @@ pub async fn compare_artifacts(
7067
commit: Option<String>,
7168
label: &str,
7269
) -> anyhow::Result<Option<Commit>> {
73-
Ok(commit
70+
commit
7471
.map(|commit| {
7572
aids.iter()
7673
.find(|c| c.sha == commit)
7774
.cloned()
7875
.ok_or_else(|| anyhow::anyhow!("{label} commit {commit} not found"))
7976
})
80-
.transpose()?)
77+
.transpose()
8178
}
8279

8380
let base: Option<Commit> = check_commit(&aids, base, "Base")?;
@@ -111,8 +108,8 @@ pub async fn compare_artifacts(
111108
terminal.draw(|frame| {
112109
screen.draw(frame);
113110
})?;
114-
match event::read()? {
115-
Event::Key(key_event) => match key_event.code {
111+
if let Event::Key(key_event) = event::read()? {
112+
match key_event.code {
116113
KeyCode::Char('q') | KeyCode::Esc => break,
117114
key => {
118115
if let Some(action) = screen.handle_key(key).await? {
@@ -123,8 +120,7 @@ pub async fn compare_artifacts(
123120
}
124121
}
125122
}
126-
},
127-
_ => {}
123+
}
128124
}
129125
}
130126
ratatui::restore();
@@ -301,7 +297,7 @@ impl CompareScreen {
301297
base: Commit,
302298
modified: Commit,
303299
) -> anyhow::Result<Self> {
304-
let pstats = load_data(
300+
let data = load_data(
305301
metric,
306302
&db_state.index,
307303
db_state.db.as_mut(),
@@ -314,10 +310,22 @@ impl CompareScreen {
314310
modified,
315311
db_state,
316312
metric,
317-
data: pstats,
313+
data,
318314
table_state: TableState::default(),
319315
})
320316
}
317+
318+
async fn reload_data(&mut self) -> anyhow::Result<()> {
319+
self.data = load_data(
320+
self.metric,
321+
&self.db_state.index,
322+
self.db_state.db.as_mut(),
323+
&self.base,
324+
&self.modified,
325+
)
326+
.await?;
327+
Ok(())
328+
}
321329
}
322330

323331
impl Screen for CompareScreen {
@@ -330,15 +338,20 @@ impl Screen for CompareScreen {
330338
[
331339
// +2 because of borders
332340
Constraint::Min((summary_table.lines().count() + 2) as u16),
341+
Constraint::Length(2),
333342
Constraint::Percentage(100),
334343
]
335344
.as_ref(),
336345
)
337346
.split(frame.area());
347+
338348
frame.render_widget(
339349
Paragraph::new(Text::raw(summary_table)).block(Block::bordered().title("Summary")),
340350
layout[0],
341351
);
352+
353+
render_metric(frame, self.metric, layout[1]);
354+
342355
let header = Row::new(vec![
343356
Line::from("Benchmark"),
344357
Line::from("Profile"),
@@ -398,7 +411,7 @@ impl Screen for CompareScreen {
398411
.row_highlight_style(Style::new().bold());
399412

400413
let table_layout =
401-
Layout::new(Direction::Horizontal, [Constraint::Max(120)]).split(layout[1]);
414+
Layout::new(Direction::Horizontal, [Constraint::Max(120)]).split(layout[2]);
402415
frame.render_stateful_widget(table, table_layout[0], &mut self.table_state);
403416
}
404417

@@ -410,6 +423,14 @@ impl Screen for CompareScreen {
410423
match key {
411424
KeyCode::Down => self.table_state.select_next(),
412425
KeyCode::Up => self.table_state.select_previous(),
426+
KeyCode::Char('a') => {
427+
self.metric = select_metric(self.metric, -1);
428+
self.reload_data().await?;
429+
}
430+
KeyCode::Char('s') => {
431+
self.metric = select_metric(self.metric, 1);
432+
self.reload_data().await?;
433+
}
413434
_ => {}
414435
}
415436

@@ -418,6 +439,23 @@ impl Screen for CompareScreen {
418439
}
419440
}
420441

442+
fn select_metric(current: Metric, direction: isize) -> Metric {
443+
let index = ALL_METRICS.iter().position(|m| *m == current).unwrap_or(0) as isize;
444+
let index = ((index + direction) + ALL_METRICS.len() as isize) % ALL_METRICS.len() as isize;
445+
ALL_METRICS[index as usize]
446+
}
447+
448+
fn render_metric(frame: &mut Frame, metric: Metric, area: Rect) {
449+
frame.render_widget(
450+
Line::from(vec![
451+
"Metric: ".into(),
452+
metric.as_str().bold(),
453+
" (switch: A/S)".into(),
454+
]),
455+
area,
456+
)
457+
}
458+
421459
async fn load_data(
422460
metric: Metric,
423461
index: &Index,
@@ -429,7 +467,7 @@ async fn load_data(
429467
let resp = query
430468
.execute(
431469
conn,
432-
&index,
470+
index,
433471
Arc::new(vec![
434472
ArtifactId::Commit(base.clone()),
435473
ArtifactId::Commit(modified.clone()),
@@ -497,26 +535,19 @@ struct Regression {
497535
count: usize,
498536
#[tabled(display("display_range"))]
499537
range: (Option<f64>, Option<f64>),
500-
#[tabled(display("display_mean"))]
538+
#[tabled(display("format_value"))]
501539
mean: Option<f64>,
502540
}
503541

504-
fn format_value(value: Option<f64>) -> String {
542+
fn format_value(value: &Option<f64>) -> String {
505543
match value {
506-
Some(value) => format!("{:+.2}%", value),
544+
Some(value) => format!("{value:+.2}%"),
507545
None => "-".to_string(),
508546
}
509547
}
510548

511549
fn display_range(&(min, max): &(Option<f64>, Option<f64>)) -> String {
512-
format!("[{}, {}]", &format_value(min), &format_value(max))
513-
}
514-
515-
fn display_mean(value: &Option<f64>) -> String {
516-
match value {
517-
Some(value) => format!("{:+.2}%", value),
518-
None => "-".to_string(),
519-
}
550+
format!("[{}, {}]", &format_value(&min), &format_value(&max))
520551
}
521552

522553
impl From<&Vec<f64>> for Regression {

0 commit comments

Comments
 (0)