Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5cde4ae

Browse files
committedJun 11, 2020
support Throughput::Elements
Example output: ``` base 1.00 683.6±86.03µs 5.7 MElem/sec ``` - add CThroughput::elements to de/serialize the values - replace Benchmark.bytes_per_second with a Throughput enum and adjust - the output to print B/sec or Elem/sec accordingly Fixes: BurntSushi#3
1 parent 83040b4 commit 5cde4ae

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed
 

‎src/data.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct CBenchmark {
4545
#[serde(rename_all = "PascalCase")]
4646
pub struct CThroughput {
4747
pub bytes: Option<u64>,
48+
pub elements: Option<u64>,
4849
}
4950

5051
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -165,15 +166,29 @@ impl Benchmark {
165166
&self.info.full_id
166167
}
167168

168-
pub fn bytes_per_second(&self) -> Option<f64> {
169+
pub fn throughput(&self) -> Option<Throughput> {
169170
const NANOS_PER_SECOND: f64 = 1_000_000_000.0;
170171

171-
self.info.throughput.as_ref().and_then(|t| t.bytes).map(|bytes| {
172-
bytes as f64 * (NANOS_PER_SECOND / self.nanoseconds())
172+
let scale = NANOS_PER_SECOND / self.nanoseconds();
173+
174+
self.info.throughput.as_ref().and_then(|t| {
175+
if let Some(num) = t.bytes {
176+
Some(Throughput::Bytes(num as f64 * scale))
177+
} else if let Some(num) = t.elements {
178+
Some(Throughput::Elements(num as f64 * scale))
179+
} else {
180+
None
181+
}
173182
})
174183
}
175184
}
176185

186+
#[derive(Clone, Copy, Debug)]
187+
pub enum Throughput {
188+
Bytes(f64),
189+
Elements(f64),
190+
}
191+
177192
impl BaseBenchmarks {
178193
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<BaseBenchmarks> {
179194
deserialize_json_path(path.as_ref())

‎src/output.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Benchmark {
1919
name: String,
2020
nanoseconds: f64,
2121
stddev: Option<f64>,
22-
bytes_per_second: Option<f64>,
22+
throughput: Option<data::Throughput>,
2323
/// Whether this is the best benchmark in a group. This is only populated
2424
/// when a `Comparison` is built.
2525
best: bool,
@@ -74,7 +74,7 @@ impl Benchmark {
7474
name: b.fullname().to_string(),
7575
nanoseconds: b.nanoseconds(),
7676
stddev: Some(b.stddev()),
77-
bytes_per_second: b.bytes_per_second(),
77+
throughput: b.throughput(),
7878
best: false,
7979
rank: 0.0,
8080
}
@@ -134,7 +134,7 @@ pub fn columns<W: WriteColor>(
134134
"\t {:<5.2} {:>14} {:>14}",
135135
b.rank,
136136
time(b.nanoseconds, b.stddev),
137-
throughput(b.bytes_per_second),
137+
throughput(b.throughput),
138138
)?;
139139
if b.best {
140140
wtr.reset()?;
@@ -172,7 +172,7 @@ fn rows_one<W: WriteColor>(mut wtr: W, group: &Comparison) -> Result<()> {
172172
b.name,
173173
b.rank,
174174
time(b.nanoseconds, b.stddev),
175-
throughput(b.bytes_per_second),
175+
throughput(b.throughput),
176176
)?;
177177
}
178178
Ok(())
@@ -209,22 +209,27 @@ fn time(nanos: f64, stddev: Option<f64>) -> String {
209209
}
210210
}
211211

212-
fn throughput(bytes_per_second: Option<f64>) -> String {
213-
const MIN_KB: f64 = (2 * (1 << 10) as u64) as f64;
214-
const MIN_MB: f64 = (2 * (1 << 20) as u64) as f64;
215-
const MIN_GB: f64 = (2 * (1 << 30) as u64) as f64;
212+
fn throughput(throughput: Option<data::Throughput>) -> String {
213+
use data::Throughput::*;
214+
match throughput {
215+
Some(Bytes(num)) => throughput_per(num, "B"),
216+
Some(Elements(num)) => throughput_per(num, "Elem"),
217+
_ => "? ?/sec".to_string(),
218+
}
219+
}
216220

217-
let per = match bytes_per_second {
218-
None => return "? B/sec".to_string(),
219-
Some(per) => per,
220-
};
221-
if per < MIN_KB {
222-
format!("{} B/sec", per as u64)
223-
} else if per < MIN_MB {
224-
format!("{:.1} KB/sec", (per / (1 << 10) as f64))
225-
} else if per < MIN_GB {
226-
format!("{:.1} MB/sec", (per / (1 << 20) as f64))
221+
fn throughput_per(per: f64, unit: &str) -> String {
222+
const MIN_K: f64 = (2 * (1 << 10) as u64) as f64;
223+
const MIN_M: f64 = (2 * (1 << 20) as u64) as f64;
224+
const MIN_G: f64 = (2 * (1 << 30) as u64) as f64;
225+
226+
if per < MIN_K {
227+
format!("{} {}/sec", per as u64, unit)
228+
} else if per < MIN_M {
229+
format!("{:.1} K{}/sec", (per / (1 << 10) as f64), unit)
230+
} else if per < MIN_G {
231+
format!("{:.1} M{}/sec", (per / (1 << 20) as f64), unit)
227232
} else {
228-
format!("{:.1} GB/sec", (per / (1 << 30) as f64))
233+
format!("{:.1} G{}/sec", (per / (1 << 30) as f64), unit)
229234
}
230235
}

0 commit comments

Comments
 (0)
Please sign in to comment.