Skip to content

Commit 935c632

Browse files
committed
Add Throughput::BytesDecimal. Fixes #581.
1 parent f82ce59 commit 935c632

6 files changed

Lines changed: 52 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3232
- A `--discard-baseline` flag for discarding rather than saving benchmark results.
3333
- Formal support for benchmarking code compiled to web-assembly.
3434
- A `--quiet` flag for printing just a single line per benchmark.
35+
- A `Throughput::BytesDecimal` option for measuring throughput in bytes but printing them using
36+
decimal units like kilobytes instead of binary units like kibibytes.
3537

3638
### Fixed
37-
- When using `bench_with_input`, the input parameter will now be passed through `black_box` before
39+
- When using `bench_with_input`, the input parameter will now be passed through `black_box` before
3840
passing it to the benchmark.
3941

4042
## [0.3.6] - 2022-07-06
@@ -502,12 +504,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
502504

503505
- Initial release on Crates.io.
504506

505-
<<<<<<< HEAD
506-
[unreleased]: https://github.com/bheisler/criterion.rs/compare/0.3.4...HEAD
507-
=======
508-
509507
[Unreleased]: https://github.com/bheisler/criterion.rs/compare/0.3.6...HEAD
510-
>>>>>>> master
511508
[0.1.1]: https://github.com/bheisler/criterion.rs/compare/0.1.0...0.1.1
512509
[0.1.2]: https://github.com/bheisler/criterion.rs/compare/0.1.1...0.1.2
513510
[0.2.0]: https://github.com/bheisler/criterion.rs/compare/0.1.2...0.2.0
@@ -528,4 +525,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
528525
[0.3.3]: https://github.com/bheisler/criterion.rs/compare/0.3.2...0.3.3
529526
[0.3.4]: https://github.com/bheisler/criterion.rs/compare/0.3.3...0.3.4
530527
[0.3.5]: https://github.com/bheisler/criterion.rs/compare/0.3.4...0.3.5
531-
[0.3.5]: https://github.com/bheisler/criterion.rs/compare/0.3.5...0.3.6
528+
[0.3.5]: https://github.com/bheisler/criterion.rs/compare/0.3.5...0.3.6

benches/benchmarks/custom_measurement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl ValueFormatter for HalfSecFormatter {
1414

1515
fn format_throughput(&self, throughput: &Throughput, value: f64) -> String {
1616
match *throughput {
17-
Throughput::Bytes(bytes) => {
17+
Throughput::Bytes(bytes) | Throughput::BytesDecimal(bytes) => {
1818
format!("{} b/s/2", (bytes as f64) / (value * 2f64 * 10f64.powi(-9)))
1919
}
2020
Throughput::Elements(elems) => format!(
@@ -39,7 +39,7 @@ impl ValueFormatter for HalfSecFormatter {
3939
values: &mut [f64],
4040
) -> &'static str {
4141
match *throughput {
42-
Throughput::Bytes(bytes) => {
42+
Throughput::Bytes(bytes) | Throughput::BytesDecimal(bytes) => {
4343
for val in values {
4444
*val = (bytes as f64) / (*val * 2f64 * 10f64.powi(-9))
4545
}

benches/benchmarks/with_inputs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ fn from_elem(c: &mut Criterion) {
1313
});
1414
}
1515
group.finish();
16+
17+
let mut group = c.benchmark_group("from_elem_decimal");
18+
for size in [KB, 2 * KB].iter() {
19+
group.throughput(Throughput::BytesDecimal(*size as u64));
20+
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
21+
b.iter(|| iter::repeat(0u8).take(size).collect::<Vec<_>>());
22+
});
23+
}
24+
group.finish();
1625
}
1726

1827
criterion_group!(benches, from_elem);

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,11 @@ pub enum Throughput {
12071207
/// an input string or `&[u8]`.
12081208
Bytes(u64),
12091209

1210+
/// Equivalent to Bytes, but the value will be reported in terms of
1211+
/// kilobytes (1000 bytes) per second instead of kibibytes (1024 bytes) per
1212+
/// second, megabytes instead of mibibytes, and gigabytes instead of gibibytes.
1213+
BytesDecimal(u64),
1214+
12101215
/// Measure throughput in terms of elements/second. The value should be the number of elements
12111216
/// processed by one iteration of the benchmarked code. Typically, this would be the size of a
12121217
/// collection, but could also be the number of lines of input text or the number of values to

src/measurement.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,31 @@ impl DurationFormatter {
124124
unit
125125
}
126126

127+
fn bytes_per_second_decimal(
128+
&self,
129+
bytes: f64,
130+
typical: f64,
131+
values: &mut [f64],
132+
) -> &'static str {
133+
let bytes_per_second = bytes * (1e9 / typical);
134+
let (denominator, unit) = if bytes_per_second < 1000.0 {
135+
(1.0, " B/s")
136+
} else if bytes_per_second < 1000.0 * 1000.0 {
137+
(1000.0, "KB/s")
138+
} else if bytes_per_second < 1000.0 * 1000.0 * 1000.0 {
139+
(1000.0 * 1000.0, "MB/s")
140+
} else {
141+
(1000.0 * 1000.0 * 1000.0, "GB/s")
142+
};
143+
144+
for val in values {
145+
let bytes_per_second = bytes * (1e9 / *val);
146+
*val = bytes_per_second / denominator;
147+
}
148+
149+
unit
150+
}
151+
127152
fn elements_per_second(&self, elems: f64, typical: f64, values: &mut [f64]) -> &'static str {
128153
let elems_per_second = elems * (1e9 / typical);
129154
let (denominator, unit) = if elems_per_second < 1000.0 {
@@ -153,6 +178,9 @@ impl ValueFormatter for DurationFormatter {
153178
) -> &'static str {
154179
match *throughput {
155180
Throughput::Bytes(bytes) => self.bytes_per_second(bytes as f64, typical, values),
181+
Throughput::BytesDecimal(bytes) => {
182+
self.bytes_per_second_decimal(bytes as f64, typical, values)
183+
}
156184
Throughput::Elements(elems) => self.elements_per_second(elems as f64, typical, values),
157185
}
158186
}

src/report.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ impl BenchmarkId {
173173

174174
pub fn as_number(&self) -> Option<f64> {
175175
match self.throughput {
176-
Some(Throughput::Bytes(n)) | Some(Throughput::Elements(n)) => Some(n as f64),
176+
Some(Throughput::Bytes(n))
177+
| Some(Throughput::Elements(n))
178+
| Some(Throughput::BytesDecimal(n)) => Some(n as f64),
177179
None => self
178180
.value_str
179181
.as_ref()
@@ -184,6 +186,7 @@ impl BenchmarkId {
184186
pub fn value_type(&self) -> Option<ValueType> {
185187
match self.throughput {
186188
Some(Throughput::Bytes(_)) => Some(ValueType::Bytes),
189+
Some(Throughput::BytesDecimal(_)) => Some(ValueType::Bytes),
187190
Some(Throughput::Elements(_)) => Some(ValueType::Elements),
188191
None => self
189192
.value_str

0 commit comments

Comments
 (0)