Skip to content

Commit

Permalink
fix_plot_img
Browse files Browse the repository at this point in the history
  • Loading branch information
epompeii committed Dec 26, 2024
1 parent c715eac commit beca0d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 56 deletions.
55 changes: 2 additions & 53 deletions lib/bencher_plot/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl LinePlot {
.y_desc(&perf_data.y_desc)
.y_labels(Y_LABELS)
.y_label_style((FontFamily::Monospace, 12))
.y_label_formatter(&|&y| PerfData::y_label_fmt(y))
.y_label_formatter(&|&y| Units::trim_number(y))
.max_light_lines(4)
.draw()?;

Expand Down Expand Up @@ -345,59 +345,8 @@ impl PerfData {
u32::try_from(y_len).map_err(Into::into)
}

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn y_label_fmt(y: f64) -> String {
if y < 1.0 {
Self::decimal_format(y)
} else {
Self::comma_format(y as u64)
}
}

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn float_len(y: f64) -> usize {
if y < 1.0 {
Self::decimal_format(y).len()
} else {
Self::comma_format(y as u64).len()
}
}

fn decimal_format(y: f64) -> String {
const ZERO: char = '0';

let y_str = y.to_string();
let mut y_chars = String::with_capacity(y_str.len());
let mut zero_count = 0;
for (index, c) in y_str.chars().enumerate() {
if index < 2 {
y_chars.push(c);
} else if zero_count == 4 {
if index == 6 {
y_chars.push(ZERO);
}
break;
} else if c == ZERO {
zero_count += 1;
} else {
for _ in 0..zero_count {
y_chars.push(ZERO);
}
zero_count = 0;
y_chars.push(c);
}
}
y_chars
}

fn comma_format(y: u64) -> String {
y.to_string()
.as_bytes()
.rchunks(3)
.rev()
.filter_map(|thousand| std::str::from_utf8(thousand).ok())
.collect::<Vec<_>>()
.join(",")
Units::trim_number(y).len()
}

fn plot_box(&self) -> Result<PlotBox, PlotError> {
Expand Down
20 changes: 17 additions & 3 deletions lib/bencher_valid/src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ impl Units {
}

pub fn format_number(number: f64) -> String {
format_number(number)
format_number(number, false)
}

pub fn trim_number(number: f64) -> String {
format_number(number, true)
}
}

Expand Down Expand Up @@ -236,7 +240,7 @@ enum Position {
Decimal,
}

fn format_number(number: f64) -> String {
fn format_number(number: f64, trim_decimal: bool) -> String {
let mut number_str = String::new();
let mut position = Position::Decimal;
for c in format!("{:.2}", number.abs()).chars().rev() {
Expand All @@ -261,5 +265,15 @@ fn format_number(number: f64) -> String {
if number < 0.0 {
number_str.push('-');
}
number_str.chars().rev().collect()
if trim_decimal && number_str.starts_with("00.") {
number_str
.chars()
.collect::<Vec<_>>()
.into_iter()
.skip(3)
.rev()
.collect()
} else {
number_str.chars().rev().collect()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Pending `v0.4.33`
- Fix plot image y-axis labels

## `v0.4.32`
- Fix benchmark result units in PR comments (Thank you [@dklassic](https://github.com/dklassic))

Expand Down

0 comments on commit beca0d0

Please sign in to comment.