-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathrender.rs
More file actions
302 lines (273 loc) · 10.4 KB
/
Copy pathrender.rs
File metadata and controls
302 lines (273 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Custom console-table rendering for random-access benchmark results.
//!
//! Random-access has two extra dimensions beyond what the shared
//! [`vortex_bench::display::render_table`] handles: the access pattern (which
//! we keep on the row) and the open mode (cached vs reopen), which becomes a
//! second-level column header inlined into the format label (e.g.
//! `parquet-cached`, `parquet-reopen`). Rather than push those concerns into
//! the shared renderer, we keep this layout local.
//!
//! Rows are keyed by `(dataset, Option<AccessPattern>)` in the order they are
//! first observed in `runs`; columns are the cartesian product of `formats`
//! and `reopen_variants`.
use std::io::Write;
use anyhow::Result;
use tabled::builder::Builder;
use tabled::settings::Color;
use tabled::settings::Style;
use tabled::settings::themes::Colorization;
use vortex_bench::Format;
use vortex_bench::measurements::TimingMeasurement;
use vortex_bench::utils::aliases::hash_map::HashMap;
use crate::AccessPattern;
/// One row of random-access timing for a `(dataset, pattern)` benchmark and a
/// specific `(format, reopen)` combination. Carries the same
/// [`TimingMeasurement`] used for JSON output so the two emitters stay in sync.
pub struct RandomAccessRun {
pub timing: TimingMeasurement,
pub dataset: String,
pub pattern: Option<AccessPattern>,
pub reopen: bool,
/// Row label for this run (e.g. `random-access/taxi/uniform`). Format is
/// implied by the column header, so it is not part of the label.
pub display_name: String,
}
/// Render a random-access benchmark result table.
///
/// Columns are `formats × reopen_variants` with `{format-ext}-{open-mode}`
/// headers (e.g. `vortex-cached`, `parquet-reopen`). Rows are unique
/// `(dataset, pattern)` pairs in the order they were observed in `runs`.
///
/// The first column (the baseline) is the leftmost format / cached variant;
/// each non-baseline cell is colored relative to the baseline value in its
/// row.
pub fn render_random_access_table<W: Write>(
writer: &mut W,
runs: &[RandomAccessRun],
formats: &[Format],
reopen_variants: &[bool],
) -> Result<()> {
// Columns: cartesian product of (format, reopen) in the user-supplied
// ordering. Storing the cell key alongside its display label keeps the
// lookup loop straightforward.
let columns: Vec<(Format, bool, String)> = formats
.iter()
.flat_map(|format| {
reopen_variants
.iter()
.map(move |&reopen| (*format, reopen, column_label(*format, reopen)))
})
.collect();
// Rows: unique (dataset, pattern) keys in insertion order. Insertion
// order matches the outer iteration in `run_random_access`, so taxi/legacy
// rows appear before pattern rows.
let mut rows: Vec<(String, Option<AccessPattern>, String)> = Vec::new();
let mut row_index: HashMap<(String, Option<AccessPattern>), usize> = HashMap::new();
let mut cells: HashMap<(usize, Format, bool), u128> = HashMap::new();
for run in runs {
let key = (run.dataset.clone(), run.pattern);
let idx = match row_index.get(&key) {
Some(&idx) => idx,
None => {
let idx = rows.len();
row_index.insert(key.clone(), idx);
rows.push((run.dataset.clone(), run.pattern, run.display_name.clone()));
idx
}
};
let format = run.timing.target.format;
cells.insert(
(idx, format, run.reopen),
run.timing.median_time().as_micros(),
);
}
let mut table_builder = Builder::default();
// Single header row: `Benchmark | format-mode | format-mode | ...`.
let header: Vec<String> = std::iter::once("Benchmark".to_owned())
.chain(columns.iter().map(|(_, _, label)| label.clone()))
.collect();
table_builder.push_record(header);
// Baseline for each row is the leftmost column. We capture it before
// emitting the row so non-baseline cells can be colored relative to it.
let baseline_column = columns
.first()
.map(|(format, reopen, _)| (*format, *reopen));
let mut colors = Vec::new();
for (row_idx, (_, _, label)) in rows.iter().enumerate() {
let baseline_value = baseline_column
.and_then(|(format, reopen)| cells.get(&(row_idx, format, reopen)).copied());
let mut record = vec![label.clone()];
for (col_idx, (format, reopen, _)) in columns.iter().enumerate() {
let value = cells.get(&(row_idx, *format, *reopen)).copied();
record.push(match (value, baseline_value) {
(Some(v), Some(base)) if base > 0 => {
let ratio = v as f64 / base as f64;
if col_idx > 0 {
colors.push(Colorization::exact(
vec![color(base, v)],
(row_idx + 1, col_idx + 1),
));
}
format!("{v} μs ({ratio:.2})")
}
(Some(v), _) => format!("{v} μs"),
(None, _) => "-".to_string(),
});
}
table_builder.push_record(record);
}
let mut table = table_builder.build();
table.with(Style::modern());
for c in colors {
table.with(c);
}
writeln!(writer, "{table}")?;
Ok(())
}
/// `{format-ext}-{mode}` (e.g. `vortex-cached`, `parquet-reopen`). Using
/// `Format::ext()` keeps headers narrow (`vortex` rather than
/// `vortex-file-compressed`).
fn column_label(format: Format, reopen: bool) -> String {
let mode = if reopen { "reopen" } else { "cached" };
format!("{}-{}", format.ext(), mode)
}
/// Mirror of the coloring used by `vortex_bench::display::render_table`:
/// green when within 10% of baseline, yellow when within 50%, red beyond.
fn color(baseline: u128, value: u128) -> Color {
if value > baseline + baseline / 2 {
Color::BG_RED | Color::FG_BLACK
} else if value > baseline + baseline / 10 {
Color::BG_YELLOW | Color::FG_BLACK
} else {
Color::BG_BRIGHT_GREEN | Color::FG_BLACK
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use vortex_bench::Engine;
use vortex_bench::Target;
use super::*;
fn strip_ansi(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '\u{1b}' {
for c in chars.by_ref() {
if c == 'm' {
break;
}
}
} else {
out.push(c);
}
}
out
}
fn run(
dataset: &str,
pattern: Option<AccessPattern>,
format: Format,
reopen: bool,
micros: u64,
) -> RandomAccessRun {
RandomAccessRun {
timing: TimingMeasurement {
name: format!("random-access/{dataset}/{}-tokio-local-disk", format.ext()),
target: Target::new(Engine::Vortex, format),
storage: "nvme".to_string(),
runs: vec![Duration::from_micros(micros)],
},
dataset: dataset.to_string(),
pattern,
reopen,
display_name: match pattern {
Some(p) => format!("random-access/{dataset}/{}", p.name()),
None => format!("random-access/{dataset}"),
},
}
}
#[test]
fn column_label_uses_format_ext_and_mode() {
assert_eq!(column_label(Format::Parquet, false), "parquet-cached");
assert_eq!(column_label(Format::Parquet, true), "parquet-reopen");
assert_eq!(column_label(Format::OnDiskVortex, false), "vortex-cached");
}
#[test]
fn render_emits_single_header_row_with_format_mode_columns() -> Result<()> {
let runs = vec![
run("taxi", None, Format::Parquet, false, 100),
run("taxi", None, Format::Parquet, true, 200),
run("taxi", None, Format::OnDiskVortex, false, 50),
run("taxi", None, Format::OnDiskVortex, true, 110),
run(
"taxi",
Some(AccessPattern::Uniform),
Format::Parquet,
false,
300,
),
run(
"taxi",
Some(AccessPattern::Uniform),
Format::Parquet,
true,
600,
),
run(
"taxi",
Some(AccessPattern::Uniform),
Format::OnDiskVortex,
false,
150,
),
run(
"taxi",
Some(AccessPattern::Uniform),
Format::OnDiskVortex,
true,
330,
),
];
let mut buf = Vec::new();
render_random_access_table(
&mut buf,
&runs,
&[Format::Parquet, Format::OnDiskVortex],
&[false, true],
)?;
let rendered = strip_ansi(&String::from_utf8(buf)?);
assert!(
rendered.contains("parquet-cached") && rendered.contains("parquet-reopen"),
"expected format-mode column headers, got:\n{rendered}"
);
assert!(
rendered.contains("vortex-cached") && rendered.contains("vortex-reopen"),
"expected ext-based column headers, got:\n{rendered}"
);
assert!(
rendered.contains("random-access/taxi")
&& rendered.contains("random-access/taxi/uniform"),
"expected display-name row labels, got:\n{rendered}"
);
Ok(())
}
#[test]
fn render_renders_dash_for_missing_cells() -> Result<()> {
let runs = vec![
run("taxi", None, Format::Parquet, false, 100),
// No reopen variant supplied for taxi, but the column is still
// listed in `reopen_variants` — that cell should render as `-`.
];
let mut buf = Vec::new();
render_random_access_table(&mut buf, &runs, &[Format::Parquet], &[false, true])?;
let rendered = strip_ansi(&String::from_utf8(buf)?);
assert!(
rendered.contains('-'),
"expected `-` placeholder for missing cell, got:\n{rendered}"
);
Ok(())
}
}