Skip to content

Commit a7e8957

Browse files
committed
auto merge of #9823 : pnkfelix/rust/issue7655-bench-papercuts, r=alexcrichton
r? anyone. You can see a bit more discussion on #7655. This does not close any ticket; I am just scratching an itch. Note in particular that I picked the value `{:>9} ns/iter` pretty much out of a hat. :)
2 parents c8e77d5 + 602b3cd commit a7e8957

File tree

1 file changed

+60
-12
lines changed

1 file changed

+60
-12
lines changed

src/libextra/test.rs

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ impl ToStr for TestName {
5757
}
5858
}
5959

60+
#[deriving(Clone)]
61+
enum NamePadding { PadNone, PadOnLeft, PadOnRight }
62+
63+
impl TestDesc {
64+
fn padded_name(&self, column_count: uint, align: NamePadding) -> ~str {
65+
use std::num::Saturating;
66+
let name = self.name.to_str();
67+
let fill = column_count.saturating_sub(name.len());
68+
let pad = " ".repeat(fill);
69+
match align {
70+
PadNone => name,
71+
PadOnLeft => pad.append(name),
72+
PadOnRight => name.append(pad),
73+
}
74+
}
75+
}
76+
6077
// A function that runs a test. If the function returns successfully,
6178
// the test succeeds; if the function fails then the test fails. We
6279
// may need to come up with a more clever definition of test in order
@@ -70,6 +87,19 @@ pub enum TestFn {
7087
DynBenchFn(~fn(&mut BenchHarness))
7188
}
7289

90+
impl TestFn {
91+
fn padding(&self) -> NamePadding {
92+
match self {
93+
&StaticTestFn(*) => PadNone,
94+
&StaticBenchFn(*) => PadOnRight,
95+
&StaticMetricFn(*) => PadOnRight,
96+
&DynTestFn(*) => PadNone,
97+
&DynMetricFn(*) => PadOnRight,
98+
&DynBenchFn(*) => PadOnRight,
99+
}
100+
}
101+
}
102+
73103
// Structure passed to BenchFns
74104
pub struct BenchHarness {
75105
iterations: u64,
@@ -316,7 +346,8 @@ struct ConsoleTestState {
316346
ignored: uint,
317347
measured: uint,
318348
metrics: MetricMap,
319-
failures: ~[TestDesc]
349+
failures: ~[TestDesc],
350+
max_name_len: uint, // number of columns to fill when aligning names
320351
}
321352

322353
impl ConsoleTestState {
@@ -348,7 +379,8 @@ impl ConsoleTestState {
348379
ignored: 0u,
349380
measured: 0u,
350381
metrics: MetricMap::new(),
351-
failures: ~[]
382+
failures: ~[],
383+
max_name_len: 0u,
352384
}
353385
}
354386

@@ -411,8 +443,9 @@ impl ConsoleTestState {
411443
self.out.write_line(format!("\nrunning {} {}", len, noun));
412444
}
413445

414-
pub fn write_test_start(&self, test: &TestDesc) {
415-
self.out.write_str(format!("test {} ... ", test.name.to_str()));
446+
pub fn write_test_start(&self, test: &TestDesc, align: NamePadding) {
447+
let name = test.padded_name(self.max_name_len, align);
448+
self.out.write_str(format!("test {} ... ", name));
416449
}
417450

418451
pub fn write_result(&self, result: &TestResult) {
@@ -559,12 +592,12 @@ pub fn fmt_metrics(mm: &MetricMap) -> ~str {
559592

560593
pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
561594
if bs.mb_s != 0 {
562-
format!("{} ns/iter (+/- {}) = {} MB/s",
595+
format!("{:>9} ns/iter (+/- {}) = {} MB/s",
563596
bs.ns_iter_summ.median as uint,
564597
(bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
565598
bs.mb_s)
566599
} else {
567-
format!("{} ns/iter (+/- {})",
600+
format!("{:>9} ns/iter (+/- {})",
568601
bs.ns_iter_summ.median as uint,
569602
(bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
570603
}
@@ -577,7 +610,7 @@ pub fn run_tests_console(opts: &TestOpts,
577610
debug2!("callback(event={:?})", event);
578611
match (*event).clone() {
579612
TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
580-
TeWait(ref test) => st.write_test_start(test),
613+
TeWait(ref test, padding) => st.write_test_start(test, padding),
581614
TeResult(test, result) => {
582615
st.write_log(&test, &result);
583616
st.write_result(&result);
@@ -607,6 +640,20 @@ pub fn run_tests_console(opts: &TestOpts,
607640
}
608641
}
609642
let st = @mut ConsoleTestState::new(opts);
643+
fn len_if_padded(t: &TestDescAndFn) -> uint {
644+
match t.testfn.padding() {
645+
PadNone => 0u,
646+
PadOnLeft | PadOnRight => t.desc.name.to_str().len(),
647+
}
648+
}
649+
match tests.iter().max_by(|t|len_if_padded(*t)) {
650+
Some(t) => {
651+
let n = t.desc.name.to_str();
652+
debug2!("Setting max_name_len from: {}", n);
653+
st.max_name_len = n.len();
654+
},
655+
None => {}
656+
}
610657
run_tests(opts, tests, |x| callback(&x, st));
611658
match opts.save_metrics {
612659
None => (),
@@ -646,7 +693,8 @@ fn should_sort_failures_before_printing_them() {
646693
ignored: 0u,
647694
measured: 0u,
648695
metrics: MetricMap::new(),
649-
failures: ~[test_b, test_a]
696+
failures: ~[test_b, test_a],
697+
max_name_len: 0u,
650698
};
651699

652700
st.write_failures();
@@ -662,7 +710,7 @@ fn use_color() -> bool { return get_concurrency() == 1; }
662710
#[deriving(Clone)]
663711
enum TestEvent {
664712
TeFiltered(~[TestDesc]),
665-
TeWait(TestDesc),
713+
TeWait(TestDesc, NamePadding),
666714
TeResult(TestDesc, TestResult),
667715
}
668716

@@ -704,15 +752,15 @@ fn run_tests(opts: &TestOpts,
704752
// We are doing one test at a time so we can print the name
705753
// of the test before we run it. Useful for debugging tests
706754
// that hang forever.
707-
callback(TeWait(test.desc.clone()));
755+
callback(TeWait(test.desc.clone(), test.testfn.padding()));
708756
}
709757
run_test(!opts.run_tests, test, ch.clone());
710758
pending += 1;
711759
}
712760

713761
let (desc, result) = p.recv();
714762
if concurrency != 1 {
715-
callback(TeWait(desc.clone()));
763+
callback(TeWait(desc.clone(), PadNone));
716764
}
717765
callback(TeResult(desc, result));
718766
pending -= 1;
@@ -721,7 +769,7 @@ fn run_tests(opts: &TestOpts,
721769
// All benchmarks run at the end, in serial.
722770
// (this includes metric fns)
723771
for b in filtered_benchs_and_metrics.move_iter() {
724-
callback(TeWait(b.desc.clone()));
772+
callback(TeWait(b.desc.clone(), b.testfn.padding()));
725773
run_test(!opts.run_benchmarks, b, ch.clone());
726774
let (test, result) = p.recv();
727775
callback(TeResult(test, result));

0 commit comments

Comments
 (0)