1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Support code for rustc's built in unit-test and micro-benchmarking
14 //! Almost all user code will only be interested in `BenchHarness` and
15 //! `black_box`. All other interactions (such as writing tests and
16 //! benchmarks themselves) should be done via the `#[test]` and
17 //! `#[bench]` attributes.
19 //! See the [Testing Guide](../guide-testing.html) for more details.
21 // Currently, not much of this is meant for users. It is intended to
22 // support the simplest interface possible for representing and
23 // running tests while providing a base that other test frameworks may
26 #![crate_id = "test#0.10-pre"]
27 #![comment = "Rust internal test library only used by rustc"]
28 #![license = "MIT/ASL2"]
29 #![crate_type = "rlib"]
30 #![crate_type = "dylib"]
31 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
32 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
33 html_root_url = "http://static.rust-lang.org/doc/master")]
35 #![feature(asm, macro_rules)]
36 #![deny(deprecated_owned_vector)]
38 extern crate collections;
40 extern crate serialize;
44 use collections::TreeMap;
46 use time::precise_time_ns;
47 use getopts::{OptGroup, optflag, optopt};
48 use serialize::{json, Decodable};
49 use serialize::json::{Json, ToJson};
51 use term::color::{Color, RED, YELLOW, GREEN, CYAN};
56 use std::from_str::FromStr;
57 use std::io::stdio::StdWriter;
58 use std::io::{File, ChanReader, ChanWriter};
64 // to be used by rustc to compile tests in libtest
66 pub use {BenchHarness, TestName, TestResult, TestDesc,
67 TestDescAndFn, TestOpts, TrFailed, TrIgnored, TrOk,
68 Metric, MetricMap, MetricAdded, MetricRemoved,
69 MetricChange, Improvement, Regression, LikelyNoise,
70 StaticTestFn, StaticTestName, DynTestName, DynTestFn,
71 run_test, test_main, test_main_static, filter_tests,
72 parse_opts, StaticBenchFn};
77 // The name of a test. By convention this follows the rules for rust
78 // paths; i.e. it should be a series of identifiers separated by double
79 // colons. This way if some test runner wants to arrange the tests
80 // hierarchically it may.
84 StaticTestName(&'static str),
87 impl fmt::Show for TestName {
88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90 StaticTestName(s) => f.buf.write_str(s),
91 DynTestName(ref s) => f.buf.write_str(s.as_slice()),
97 enum NamePadding { PadNone, PadOnLeft, PadOnRight }
100 fn padded_name(&self, column_count: uint, align: NamePadding) -> ~str {
101 use std::num::Saturating;
102 let name = self.name.to_str();
103 let fill = column_count.saturating_sub(name.len());
104 let pad = " ".repeat(fill);
107 PadOnLeft => pad.append(name),
108 PadOnRight => name.append(pad),
113 /// Represents a benchmark function.
114 pub trait TDynBenchFn {
115 fn run(&self, harness: &mut BenchHarness);
118 // A function that runs a test. If the function returns successfully,
119 // the test succeeds; if the function fails then the test fails. We
120 // may need to come up with a more clever definition of test in order
121 // to support isolation of tests into tasks.
124 StaticBenchFn(fn(&mut BenchHarness)),
125 StaticMetricFn(proc(&mut MetricMap)),
126 DynTestFn(proc:Send()),
127 DynMetricFn(proc(&mut MetricMap)),
128 DynBenchFn(~TDynBenchFn)
132 fn padding(&self) -> NamePadding {
134 &StaticTestFn(..) => PadNone,
135 &StaticBenchFn(..) => PadOnRight,
136 &StaticMetricFn(..) => PadOnRight,
137 &DynTestFn(..) => PadNone,
138 &DynMetricFn(..) => PadOnRight,
139 &DynBenchFn(..) => PadOnRight,
144 /// Manager of the benchmarking runs.
146 /// This is feed into functions marked with `#[bench]` to allow for
147 /// set-up & tear-down before running a piece of code repeatedly via a
149 pub struct BenchHarness {
150 priv iterations: u64,
156 // The definition of a single test. A test runner will run a list of
159 pub struct TestDesc {
165 pub struct TestDescAndFn {
170 #[deriving(Clone, Encodable, Decodable, Eq, Show)]
177 pub fn new(value: f64, noise: f64) -> Metric {
178 Metric {value: value, noise: noise}
183 pub struct MetricMap(TreeMap<~str,Metric>);
185 impl Clone for MetricMap {
186 fn clone(&self) -> MetricMap {
187 let MetricMap(ref map) = *self;
188 MetricMap(map.clone())
192 /// Analysis of a single change in metric
193 #[deriving(Eq, Show)]
194 pub enum MetricChange {
202 pub type MetricDiff = TreeMap<~str,MetricChange>;
204 // The default console test runner. It accepts the command line
205 // arguments and a vector of test_descs.
206 pub fn test_main(args: &[~str], tests: Vec<TestDescAndFn> ) {
208 match parse_opts(args) {
210 Some(Err(msg)) => fail!("{}", msg),
213 match run_tests_console(&opts, tests) {
215 Ok(false) => fail!("Some tests failed"),
216 Err(e) => fail!("io error when running tests: {}", e),
220 // A variant optimized for invocation with a static test vector.
221 // This will fail (intentionally) when fed any dynamic tests, because
222 // it is copying the static values out into a dynamic vector and cannot
223 // copy dynamic values. It is doing this because from this point on
224 // a ~[TestDescAndFn] is used in order to effect ownership-transfer
225 // semantics into parallel test runners, which in turn requires a ~[]
226 // rather than a &[].
227 pub fn test_main_static(args: &[~str], tests: &[TestDescAndFn]) {
228 let owned_tests = tests.iter().map(|t| {
231 TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
234 TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
237 fail!("non-static tests passed to test::test_main_static");
241 test_main(args, owned_tests)
244 pub struct TestOpts {
245 filter: Option<~str>,
248 run_benchmarks: bool,
249 ratchet_metrics: Option<Path>,
250 ratchet_noise_percent: Option<f64>,
251 save_metrics: Option<Path>,
252 test_shard: Option<(uint,uint)>,
253 logfile: Option<Path>
256 /// Result of parsing the options.
257 pub type OptRes = Result<TestOpts, ~str>;
259 fn optgroups() -> Vec<getopts::OptGroup> {
260 vec!(getopts::optflag("", "ignored", "Run ignored tests"),
261 getopts::optflag("", "test", "Run tests and not benchmarks"),
262 getopts::optflag("", "bench", "Run benchmarks instead of tests"),
263 getopts::optflag("h", "help", "Display this message (longer with --help)"),
264 getopts::optopt("", "save-metrics", "Location to save bench metrics",
266 getopts::optopt("", "ratchet-metrics",
267 "Location to load and save metrics from. The metrics \
268 loaded are cause benchmarks to fail if they run too \
270 getopts::optopt("", "ratchet-noise-percent",
271 "Tests within N% of the recorded metrics will be \
272 considered as passing", "PERCENTAGE"),
273 getopts::optopt("", "logfile", "Write logs to the specified file instead \
275 getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
279 fn usage(binary: &str, helpstr: &str) {
280 let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
281 println!("{}", getopts::usage(message, optgroups().as_slice()));
283 if helpstr == "help" {
285 The FILTER is matched against the name of all tests to run, and if any tests
286 have a substring match, only those tests are run.
288 By default, all tests are run in parallel. This can be altered with the
289 RUST_TEST_TASKS environment variable when running tests (set it to 1).
293 #[test] - Indicates a function is a test to be run. This function
295 #[bench] - Indicates a function is a benchmark to be run. This
296 function takes one argument (test::BenchHarness).
297 #[should_fail] - This function (also labeled with #[test]) will only pass if
298 the code causes a failure (an assertion failure or fail!)
299 #[ignore] - When applied to a function which is already attributed as a
300 test, then the test runner will ignore these tests during
301 normal test runs. Running with --ignored will run these
302 tests. This may also be written as #[ignore(cfg(...))] to
303 ignore the test on certain configurations.");
307 // Parses command line arguments into test options
308 pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
309 let args_ = args.tail();
311 match getopts::getopts(args_, optgroups().as_slice()) {
313 Err(f) => return Some(Err(f.to_err_msg()))
316 if matches.opt_present("h") { usage(args[0], "h"); return None; }
317 if matches.opt_present("help") { usage(args[0], "help"); return None; }
320 if matches.free.len() > 0 {
321 Some((*matches.free.get(0)).clone())
326 let run_ignored = matches.opt_present("ignored");
328 let logfile = matches.opt_str("logfile");
329 let logfile = logfile.map(|s| Path::new(s));
331 let run_benchmarks = matches.opt_present("bench");
332 let run_tests = ! run_benchmarks ||
333 matches.opt_present("test");
335 let ratchet_metrics = matches.opt_str("ratchet-metrics");
336 let ratchet_metrics = ratchet_metrics.map(|s| Path::new(s));
338 let ratchet_noise_percent = matches.opt_str("ratchet-noise-percent");
339 let ratchet_noise_percent = ratchet_noise_percent.map(|s| from_str::<f64>(s).unwrap());
341 let save_metrics = matches.opt_str("save-metrics");
342 let save_metrics = save_metrics.map(|s| Path::new(s));
344 let test_shard = matches.opt_str("test-shard");
345 let test_shard = opt_shard(test_shard);
347 let test_opts = TestOpts {
349 run_ignored: run_ignored,
350 run_tests: run_tests,
351 run_benchmarks: run_benchmarks,
352 ratchet_metrics: ratchet_metrics,
353 ratchet_noise_percent: ratchet_noise_percent,
354 save_metrics: save_metrics,
355 test_shard: test_shard,
362 pub fn opt_shard(maybestr: Option<~str>) -> Option<(uint,uint)> {
366 let mut it = s.split('.');
367 match (it.next().and_then(from_str), it.next().and_then(from_str), it.next()) {
368 (Some(a), Some(b), None) => Some((a, b)),
376 #[deriving(Clone, Eq)]
377 pub struct BenchSamples {
378 priv ns_iter_summ: stats::Summary,
382 #[deriving(Clone, Eq)]
383 pub enum TestResult {
387 TrMetrics(MetricMap),
388 TrBench(BenchSamples),
391 enum OutputLocation<T> {
392 Pretty(term::Terminal<T>),
396 struct ConsoleTestState<T> {
397 log_out: Option<File>,
398 out: OutputLocation<T>,
406 failures: Vec<(TestDesc, Vec<u8> )> ,
407 max_name_len: uint, // number of columns to fill when aligning names
410 impl<T: Writer> ConsoleTestState<T> {
411 pub fn new(opts: &TestOpts,
412 _: Option<T>) -> io::IoResult<ConsoleTestState<StdWriter>> {
413 let log_out = match opts.logfile {
414 Some(ref path) => Some(try!(File::create(path))),
417 let out = match term::Terminal::new(io::stdio::stdout_raw()) {
418 Err(_) => Raw(io::stdio::stdout_raw()),
421 Ok(ConsoleTestState {
424 use_color: use_color(),
430 metrics: MetricMap::new(),
431 failures: Vec::new(),
436 pub fn write_ok(&mut self) -> io::IoResult<()> {
437 self.write_pretty("ok", term::color::GREEN)
440 pub fn write_failed(&mut self) -> io::IoResult<()> {
441 self.write_pretty("FAILED", term::color::RED)
444 pub fn write_ignored(&mut self) -> io::IoResult<()> {
445 self.write_pretty("ignored", term::color::YELLOW)
448 pub fn write_metric(&mut self) -> io::IoResult<()> {
449 self.write_pretty("metric", term::color::CYAN)
452 pub fn write_bench(&mut self) -> io::IoResult<()> {
453 self.write_pretty("bench", term::color::CYAN)
456 pub fn write_added(&mut self) -> io::IoResult<()> {
457 self.write_pretty("added", term::color::GREEN)
460 pub fn write_improved(&mut self) -> io::IoResult<()> {
461 self.write_pretty("improved", term::color::GREEN)
464 pub fn write_removed(&mut self) -> io::IoResult<()> {
465 self.write_pretty("removed", term::color::YELLOW)
468 pub fn write_regressed(&mut self) -> io::IoResult<()> {
469 self.write_pretty("regressed", term::color::RED)
472 pub fn write_pretty(&mut self,
474 color: term::color::Color) -> io::IoResult<()> {
476 Pretty(ref mut term) => {
478 try!(term.fg(color));
480 try!(term.write(word.as_bytes()));
486 Raw(ref mut stdout) => stdout.write(word.as_bytes())
490 pub fn write_plain(&mut self, s: &str) -> io::IoResult<()> {
492 Pretty(ref mut term) => term.write(s.as_bytes()),
493 Raw(ref mut stdout) => stdout.write(s.as_bytes())
497 pub fn write_run_start(&mut self, len: uint) -> io::IoResult<()> {
499 let noun = if len != 1 { &"tests" } else { &"test" };
500 self.write_plain(format!("\nrunning {} {}\n", len, noun))
503 pub fn write_test_start(&mut self, test: &TestDesc,
504 align: NamePadding) -> io::IoResult<()> {
505 let name = test.padded_name(self.max_name_len, align);
506 self.write_plain(format!("test {} ... ", name))
509 pub fn write_result(&mut self, result: &TestResult) -> io::IoResult<()> {
511 TrOk => self.write_ok(),
512 TrFailed => self.write_failed(),
513 TrIgnored => self.write_ignored(),
514 TrMetrics(ref mm) => {
515 try!(self.write_metric());
516 self.write_plain(format!(": {}", fmt_metrics(mm)))
519 try!(self.write_bench());
520 self.write_plain(format!(": {}", fmt_bench_samples(bs)))
523 self.write_plain("\n")
526 pub fn write_log(&mut self, test: &TestDesc,
527 result: &TestResult) -> io::IoResult<()> {
531 let s = format!("{} {}\n", match *result {
533 TrFailed => ~"failed",
534 TrIgnored => ~"ignored",
535 TrMetrics(ref mm) => fmt_metrics(mm),
536 TrBench(ref bs) => fmt_bench_samples(bs)
537 }, test.name.to_str());
538 o.write(s.as_bytes())
543 pub fn write_failures(&mut self) -> io::IoResult<()> {
544 try!(self.write_plain("\nfailures:\n"));
545 let mut failures = Vec::new();
546 let mut fail_out = ~"";
547 for &(ref f, ref stdout) in self.failures.iter() {
548 failures.push(f.name.to_str());
549 if stdout.len() > 0 {
550 fail_out.push_str(format!("---- {} stdout ----\n\t",
552 let output = str::from_utf8_lossy(stdout.as_slice());
553 fail_out.push_str(output.as_slice().replace("\n", "\n\t"));
554 fail_out.push_str("\n");
557 if fail_out.len() > 0 {
558 try!(self.write_plain("\n"));
559 try!(self.write_plain(fail_out));
562 try!(self.write_plain("\nfailures:\n"));
563 failures.as_mut_slice().sort();
564 for name in failures.iter() {
565 try!(self.write_plain(format!(" {}\n", name.to_str())));
570 pub fn write_metric_diff(&mut self, diff: &MetricDiff) -> io::IoResult<()> {
572 let mut improved = 0;
573 let mut regressed = 0;
577 for (k, v) in diff.iter() {
579 LikelyNoise => noise += 1,
582 try!(self.write_added());
583 try!(self.write_plain(format!(": {}\n", *k)));
587 try!(self.write_removed());
588 try!(self.write_plain(format!(": {}\n", *k)));
590 Improvement(pct) => {
592 try!(self.write_plain(format!(": {}", *k)));
593 try!(self.write_improved());
594 try!(self.write_plain(format!(" by {:.2f}%\n", pct as f64)));
598 try!(self.write_plain(format!(": {}", *k)));
599 try!(self.write_regressed());
600 try!(self.write_plain(format!(" by {:.2f}%\n", pct as f64)));
604 try!(self.write_plain(format!("result of ratchet: {} metrics added, \
605 {} removed, {} improved, {} regressed, \
607 added, removed, improved, regressed,
610 try!(self.write_plain("updated ratchet file\n"));
612 try!(self.write_plain("left ratchet file untouched\n"));
617 pub fn write_run_finish(&mut self,
618 ratchet_metrics: &Option<Path>,
619 ratchet_pct: Option<f64>) -> io::IoResult<bool> {
620 assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
622 let ratchet_success = match *ratchet_metrics {
625 try!(self.write_plain(format!("\nusing metrics ratchet: {}\n",
630 try!(self.write_plain(format!("with noise-tolerance \
634 let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
635 try!(self.write_metric_diff(&diff));
640 let test_success = self.failed == 0u;
642 try!(self.write_failures());
645 let success = ratchet_success && test_success;
647 try!(self.write_plain("\ntest result: "));
649 // There's no parallelism at this point so it's safe to use color
650 try!(self.write_ok());
652 try!(self.write_failed());
654 let s = format!(". {} passed; {} failed; {} ignored; {} measured\n\n",
655 self.passed, self.failed, self.ignored, self.measured);
656 try!(self.write_plain(s));
661 pub fn fmt_metrics(mm: &MetricMap) -> ~str {
662 let MetricMap(ref mm) = *mm;
663 let v : Vec<~str> = mm.iter()
664 .map(|(k,v)| format!("{}: {} (+/- {})",
672 pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
674 format!("{:>9} ns/iter (+/- {}) = {} MB/s",
675 bs.ns_iter_summ.median as uint,
676 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
679 format!("{:>9} ns/iter (+/- {})",
680 bs.ns_iter_summ.median as uint,
681 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
685 // A simple console test runner
686 pub fn run_tests_console(opts: &TestOpts,
687 tests: Vec<TestDescAndFn> ) -> io::IoResult<bool> {
688 fn callback<T: Writer>(event: &TestEvent,
689 st: &mut ConsoleTestState<T>) -> io::IoResult<()> {
690 match (*event).clone() {
691 TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
692 TeWait(ref test, padding) => st.write_test_start(test, padding),
693 TeResult(test, result, stdout) => {
694 try!(st.write_log(&test, &result));
695 try!(st.write_result(&result));
697 TrOk => st.passed += 1,
698 TrIgnored => st.ignored += 1,
700 let tname = test.name.to_str();
701 let MetricMap(mm) = mm;
702 for (k,v) in mm.iter() {
703 st.metrics.insert_metric(tname + "." + *k,
709 st.metrics.insert_metric(test.name.to_str(),
710 bs.ns_iter_summ.median,
711 bs.ns_iter_summ.max - bs.ns_iter_summ.min);
716 st.failures.push((test, stdout));
723 let mut st = try!(ConsoleTestState::new(opts, None::<StdWriter>));
724 fn len_if_padded(t: &TestDescAndFn) -> uint {
725 match t.testfn.padding() {
727 PadOnLeft | PadOnRight => t.desc.name.to_str().len(),
730 match tests.iter().max_by(|t|len_if_padded(*t)) {
732 let n = t.desc.name.to_str();
733 st.max_name_len = n.len();
737 try!(run_tests(opts, tests, |x| callback(&x, &mut st)));
738 match opts.save_metrics {
741 try!(st.metrics.save(pth));
742 try!(st.write_plain(format!("\nmetrics saved to: {}",
746 return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
750 fn should_sort_failures_before_printing_them() {
751 use std::io::MemWriter;
754 let test_a = TestDesc {
755 name: StaticTestName("a"),
760 let test_b = TestDesc {
761 name: StaticTestName("b"),
766 let mut st = ConsoleTestState {
768 out: Raw(MemWriter::new()),
776 metrics: MetricMap::new(),
777 failures: vec!((test_b, Vec::new()), (test_a, Vec::new()))
780 st.write_failures().unwrap();
781 let s = match st.out {
782 Raw(ref m) => str::from_utf8_lossy(m.get_ref()),
783 Pretty(_) => unreachable!()
786 let apos = s.as_slice().find_str("a").unwrap();
787 let bpos = s.as_slice().find_str("b").unwrap();
788 assert!(apos < bpos);
791 fn use_color() -> bool { return get_concurrency() == 1; }
795 TeFiltered(Vec<TestDesc> ),
796 TeWait(TestDesc, NamePadding),
797 TeResult(TestDesc, TestResult, Vec<u8> ),
800 pub type MonitorMsg = (TestDesc, TestResult, Vec<u8> );
802 fn run_tests(opts: &TestOpts,
803 tests: Vec<TestDescAndFn> ,
804 callback: |e: TestEvent| -> io::IoResult<()>) -> io::IoResult<()> {
805 let filtered_tests = filter_tests(opts, tests);
806 let filtered_descs = filtered_tests.iter()
807 .map(|t| t.desc.clone())
810 try!(callback(TeFiltered(filtered_descs)));
812 let (filtered_tests, filtered_benchs_and_metrics) =
813 filtered_tests.partition(|e| {
815 StaticTestFn(_) | DynTestFn(_) => true,
820 // It's tempting to just spawn all the tests at once, but since we have
821 // many tests that run in other processes we would be making a big mess.
822 let concurrency = get_concurrency();
824 let mut remaining = filtered_tests;
828 let (tx, rx) = channel::<MonitorMsg>();
830 while pending > 0 || !remaining.is_empty() {
831 while pending < concurrency && !remaining.is_empty() {
832 let test = remaining.pop().unwrap();
833 if concurrency == 1 {
834 // We are doing one test at a time so we can print the name
835 // of the test before we run it. Useful for debugging tests
836 // that hang forever.
837 try!(callback(TeWait(test.desc.clone(), test.testfn.padding())));
839 run_test(!opts.run_tests, test, tx.clone());
843 let (desc, result, stdout) = rx.recv();
844 if concurrency != 1 {
845 try!(callback(TeWait(desc.clone(), PadNone)));
847 try!(callback(TeResult(desc, result, stdout)));
851 // All benchmarks run at the end, in serial.
852 // (this includes metric fns)
853 for b in filtered_benchs_and_metrics.move_iter() {
854 try!(callback(TeWait(b.desc.clone(), b.testfn.padding())));
855 run_test(!opts.run_benchmarks, b, tx.clone());
856 let (test, result, stdout) = rx.recv();
857 try!(callback(TeResult(test, result, stdout)));
862 fn get_concurrency() -> uint {
864 match os::getenv("RUST_TEST_TASKS") {
866 let opt_n: Option<uint> = FromStr::from_str(s);
868 Some(n) if n > 0 => n,
869 _ => fail!("RUST_TEST_TASKS is `{}`, should be a positive integer.", s)
873 rt::default_sched_threads()
880 tests: Vec<TestDescAndFn> ) -> Vec<TestDescAndFn> {
881 let mut filtered = tests;
883 // Remove tests that don't match the test filter
884 filtered = if opts.filter.is_none() {
887 let filter_str = match opts.filter {
888 Some(ref f) => (*f).clone(),
892 fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
893 Option<TestDescAndFn> {
894 if test.desc.name.to_str().contains(filter_str) {
901 filtered.move_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
904 // Maybe pull out the ignored test and unignore them
905 filtered = if !opts.run_ignored {
908 fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
909 if test.desc.ignore {
910 let TestDescAndFn {desc, testfn} = test;
912 desc: TestDesc {ignore: false, ..desc},
919 filtered.move_iter().filter_map(|x| filter(x)).collect()
922 // Sort the tests alphabetically
923 filtered.sort_by(|t1, t2| t1.desc.name.to_str().cmp(&t2.desc.name.to_str()));
925 // Shard the remaining tests, if sharding requested.
926 match opts.test_shard {
929 filtered.move_iter().enumerate()
930 .filter(|&(i,_)| i % b == a)
937 pub fn run_test(force_ignore: bool,
939 monitor_ch: Sender<MonitorMsg>) {
941 let TestDescAndFn {desc, testfn} = test;
943 if force_ignore || desc.ignore {
944 monitor_ch.send((desc, TrIgnored, Vec::new()));
948 #[allow(deprecated_owned_vector)]
949 fn run_test_inner(desc: TestDesc,
950 monitor_ch: Sender<MonitorMsg>,
951 testfn: proc:Send()) {
953 let (tx, rx) = channel();
954 let mut reader = ChanReader::new(rx);
955 let stdout = ChanWriter::new(tx.clone());
956 let stderr = ChanWriter::new(tx);
957 let mut task = task::task().named(match desc.name {
958 DynTestName(ref name) => name.clone().into_maybe_owned(),
959 StaticTestName(name) => name.into_maybe_owned(),
961 task.opts.stdout = Some(~stdout as ~Writer:Send);
962 task.opts.stderr = Some(~stderr as ~Writer:Send);
963 let result_future = task.future_result();
966 let stdout = reader.read_to_end().unwrap().move_iter().collect();
967 let task_result = result_future.recv();
968 let test_result = calc_result(&desc, task_result.is_ok());
969 monitor_ch.send((desc.clone(), test_result, stdout));
974 DynBenchFn(bencher) => {
975 let bs = ::bench::benchmark(|harness| bencher.run(harness));
976 monitor_ch.send((desc, TrBench(bs), Vec::new()));
979 StaticBenchFn(benchfn) => {
980 let bs = ::bench::benchmark(|harness| benchfn(harness));
981 monitor_ch.send((desc, TrBench(bs), Vec::new()));
985 let mut mm = MetricMap::new();
987 monitor_ch.send((desc, TrMetrics(mm), Vec::new()));
990 StaticMetricFn(f) => {
991 let mut mm = MetricMap::new();
993 monitor_ch.send((desc, TrMetrics(mm), Vec::new()));
996 DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
997 StaticTestFn(f) => run_test_inner(desc, monitor_ch, proc() f())
1001 fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
1003 if desc.should_fail { TrFailed }
1006 if desc.should_fail { TrOk }
1012 impl ToJson for Metric {
1013 fn to_json(&self) -> json::Json {
1014 let mut map = ~TreeMap::new();
1015 map.insert(~"value", json::Number(self.value));
1016 map.insert(~"noise", json::Number(self.noise));
1021 // FIXME: remove decode_ after snapshot
1023 fn decode_(json: Json) -> MetricMap {
1024 let mut decoder = json::Decoder::new(json);
1025 MetricMap(Decodable::decode(&mut decoder))
1029 fn decode_(json: Json) -> MetricMap {
1030 let mut decoder = json::Decoder::new(json);
1031 MetricMap(match Decodable::decode(&mut decoder) {
1033 Err(e) => fail!("failure decoding JSON: {}", e)
1040 pub fn new() -> MetricMap {
1041 MetricMap(TreeMap::new())
1044 /// Load MetricDiff from a file.
1048 /// This function will fail if the path does not exist or the path does not
1049 /// contain a valid metric map.
1050 pub fn load(p: &Path) -> MetricMap {
1051 assert!(p.exists());
1052 let mut f = File::open(p).unwrap();
1053 let value = json::from_reader(&mut f as &mut io::Reader).unwrap();
1057 /// Write MetricDiff to a file.
1058 pub fn save(&self, p: &Path) -> io::IoResult<()> {
1059 let mut file = try!(File::create(p));
1060 let MetricMap(ref map) = *self;
1061 map.to_json().to_pretty_writer(&mut file)
1064 /// Compare against another MetricMap. Optionally compare all
1065 /// measurements in the maps using the provided `noise_pct` as a
1066 /// percentage of each value to consider noise. If `None`, each
1067 /// measurement's noise threshold is independently chosen as the
1068 /// maximum of that measurement's recorded noise quantity in either
1070 pub fn compare_to_old(&self, old: &MetricMap,
1071 noise_pct: Option<f64>) -> MetricDiff {
1072 let mut diff : MetricDiff = TreeMap::new();
1073 let MetricMap(ref selfmap) = *self;
1074 let MetricMap(ref old) = *old;
1075 for (k, vold) in old.iter() {
1076 let r = match selfmap.find(k) {
1077 None => MetricRemoved,
1079 let delta = v.value - vold.value;
1080 let noise = match noise_pct {
1081 None => vold.noise.abs().max(v.noise.abs()),
1082 Some(pct) => vold.value * pct / 100.0
1084 if delta.abs() <= noise {
1087 let pct = delta.abs() / vold.value.max(f64::EPSILON) * 100.0;
1088 if vold.noise < 0.0 {
1089 // When 'noise' is negative, it means we want
1090 // to see deltas that go up over time, and can
1091 // only tolerate slight negative movement.
1098 // When 'noise' is positive, it means we want
1099 // to see deltas that go down over time, and
1100 // can only tolerate slight positive movements.
1110 diff.insert((*k).clone(), r);
1112 let MetricMap(ref map) = *self;
1113 for (k, _) in map.iter() {
1114 if !diff.contains_key(k) {
1115 diff.insert((*k).clone(), MetricAdded);
1121 /// Insert a named `value` (+/- `noise`) metric into the map. The value
1122 /// must be non-negative. The `noise` indicates the uncertainty of the
1123 /// metric, which doubles as the "noise range" of acceptable
1124 /// pairwise-regressions on this named value, when comparing from one
1125 /// metric to the next using `compare_to_old`.
1127 /// If `noise` is positive, then it means this metric is of a value
1128 /// you want to see grow smaller, so a change larger than `noise` in the
1129 /// positive direction represents a regression.
1131 /// If `noise` is negative, then it means this metric is of a value
1132 /// you want to see grow larger, so a change larger than `noise` in the
1133 /// negative direction represents a regression.
1134 pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
1139 let MetricMap(ref mut map) = *self;
1140 map.insert(name.to_owned(), m);
1143 /// Attempt to "ratchet" an external metric file. This involves loading
1144 /// metrics from a metric file (if it exists), comparing against
1145 /// the metrics in `self` using `compare_to_old`, and rewriting the
1146 /// file to contain the metrics in `self` if none of the
1147 /// `MetricChange`s are `Regression`. Returns the diff as well
1148 /// as a boolean indicating whether the ratchet succeeded.
1149 pub fn ratchet(&self, p: &Path, pct: Option<f64>) -> (MetricDiff, bool) {
1150 let old = if p.exists() {
1156 let diff : MetricDiff = self.compare_to_old(&old, pct);
1157 let ok = diff.iter().all(|(_, v)| {
1159 Regression(_) => false,
1165 self.save(p).unwrap();
1174 /// A function that is opaque to the optimizer, to allow benchmarks to
1175 /// pretend to use outputs to assist in avoiding dead-code
1178 /// This function is a no-op, and does not even read from `dummy`.
1179 pub fn black_box<T>(dummy: T) {
1180 // we need to "use" the argument in some way LLVM can't
1182 unsafe {asm!("" : : "r"(&dummy))}
1187 /// Callback for benchmark functions to run in their body.
1188 pub fn iter<T>(&mut self, inner: || -> T) {
1189 self.ns_start = precise_time_ns();
1190 let k = self.iterations;
1191 for _ in range(0u64, k) {
1194 self.ns_end = precise_time_ns();
1197 pub fn ns_elapsed(&mut self) -> u64 {
1198 if self.ns_start == 0 || self.ns_end == 0 {
1201 self.ns_end - self.ns_start
1205 pub fn ns_per_iter(&mut self) -> u64 {
1206 if self.iterations == 0 {
1209 self.ns_elapsed() / cmp::max(self.iterations, 1)
1213 pub fn bench_n(&mut self, n: u64, f: |&mut BenchHarness|) {
1214 self.iterations = n;
1218 // This is a more statistics-driven benchmark algorithm
1219 pub fn auto_bench(&mut self, f: |&mut BenchHarness|) -> stats::Summary {
1221 // Initial bench run to get ballpark figure.
1223 self.bench_n(n, |x| f(x));
1225 // Try to estimate iter count for 1ms falling back to 1m
1226 // iterations if first run took < 1ns.
1227 if self.ns_per_iter() == 0 {
1230 n = 1_000_000 / cmp::max(self.ns_per_iter(), 1);
1232 // if the first run took more than 1ms we don't want to just
1233 // be left doing 0 iterations on every loop. The unfortunate
1234 // side effect of not being able to do as many runs is
1235 // automatically handled by the statistical analysis below
1236 // (i.e. larger error bars).
1237 if n == 0 { n = 1; }
1239 let mut total_run = 0;
1240 let samples : &mut [f64] = [0.0_f64, ..50];
1242 let loop_start = precise_time_ns();
1244 for p in samples.mut_iter() {
1245 self.bench_n(n, |x| f(x));
1246 *p = self.ns_per_iter() as f64;
1249 stats::winsorize(samples, 5.0);
1250 let summ = stats::Summary::new(samples);
1252 for p in samples.mut_iter() {
1253 self.bench_n(5 * n, |x| f(x));
1254 *p = self.ns_per_iter() as f64;
1257 stats::winsorize(samples, 5.0);
1258 let summ5 = stats::Summary::new(samples);
1260 let now = precise_time_ns();
1261 let loop_run = now - loop_start;
1263 // If we've run for 100ms and seem to have converged to a
1265 if loop_run > 100_000_000 &&
1266 summ.median_abs_dev_pct < 1.0 &&
1267 summ.median - summ5.median < summ5.median_abs_dev {
1271 total_run += loop_run;
1272 // Longest we ever run for is 3s.
1273 if total_run > 3_000_000_000 {
1284 use super::{BenchHarness, BenchSamples};
1286 pub fn benchmark(f: |&mut BenchHarness|) -> BenchSamples {
1287 let mut bs = BenchHarness {
1294 let ns_iter_summ = bs.auto_bench(f);
1296 let ns_iter = cmp::max(ns_iter_summ.median as u64, 1);
1297 let iter_s = 1_000_000_000 / ns_iter;
1298 let mb_s = (bs.bytes * iter_s) / 1_000_000;
1301 ns_iter_summ: ns_iter_summ,
1309 use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
1310 TestDesc, TestDescAndFn, TestOpts, run_test,
1311 Metric, MetricMap, MetricAdded, MetricRemoved,
1312 Improvement, Regression, LikelyNoise,
1313 StaticTestName, DynTestName, DynTestFn};
1314 use std::io::TempDir;
1317 pub fn do_not_run_ignored_tests() {
1319 let desc = TestDescAndFn {
1321 name: StaticTestName("whatever"),
1325 testfn: DynTestFn(proc() f()),
1327 let (tx, rx) = channel();
1328 run_test(false, desc, tx);
1329 let (_, res, _) = rx.recv();
1330 assert!(res != TrOk);
1334 pub fn ignored_tests_result_in_ignored() {
1336 let desc = TestDescAndFn {
1338 name: StaticTestName("whatever"),
1342 testfn: DynTestFn(proc() f()),
1344 let (tx, rx) = channel();
1345 run_test(false, desc, tx);
1346 let (_, res, _) = rx.recv();
1347 assert!(res == TrIgnored);
1351 fn test_should_fail() {
1353 let desc = TestDescAndFn {
1355 name: StaticTestName("whatever"),
1359 testfn: DynTestFn(proc() f()),
1361 let (tx, rx) = channel();
1362 run_test(false, desc, tx);
1363 let (_, res, _) = rx.recv();
1364 assert!(res == TrOk);
1368 fn test_should_fail_but_succeeds() {
1370 let desc = TestDescAndFn {
1372 name: StaticTestName("whatever"),
1376 testfn: DynTestFn(proc() f()),
1378 let (tx, rx) = channel();
1379 run_test(false, desc, tx);
1380 let (_, res, _) = rx.recv();
1381 assert!(res == TrFailed);
1385 fn first_free_arg_should_be_a_filter() {
1386 let args = vec!(~"progname", ~"filter");
1387 let opts = match parse_opts(args.as_slice()) {
1389 _ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
1391 assert!("filter" == opts.filter.clone().unwrap());
1395 fn parse_ignored_flag() {
1396 let args = vec!(~"progname", ~"filter", ~"--ignored");
1397 let opts = match parse_opts(args.as_slice()) {
1399 _ => fail!("Malformed arg in parse_ignored_flag")
1401 assert!((opts.run_ignored));
1405 pub fn filter_for_ignored_option() {
1406 // When we run ignored tests the test filter should filter out all the
1407 // unignored tests and flip the ignore flag on the rest to false
1409 let opts = TestOpts {
1414 run_benchmarks: false,
1415 ratchet_noise_percent: None,
1416 ratchet_metrics: None,
1424 name: StaticTestName("1"),
1428 testfn: DynTestFn(proc() {}),
1432 name: StaticTestName("2"),
1436 testfn: DynTestFn(proc() {}),
1438 let filtered = filter_tests(&opts, tests);
1440 assert_eq!(filtered.len(), 1);
1441 assert_eq!(filtered.get(0).desc.name.to_str(), ~"1");
1442 assert!(filtered.get(0).desc.ignore == false);
1446 pub fn sort_tests() {
1447 let opts = TestOpts {
1452 run_benchmarks: false,
1453 ratchet_noise_percent: None,
1454 ratchet_metrics: None,
1460 vec!(~"sha1::test", ~"int::test_to_str", ~"int::test_pow",
1461 ~"test::do_not_run_ignored_tests",
1462 ~"test::ignored_tests_result_in_ignored",
1463 ~"test::first_free_arg_should_be_a_filter",
1464 ~"test::parse_ignored_flag", ~"test::filter_for_ignored_option",
1465 ~"test::sort_tests");
1469 let mut tests = Vec::new();
1470 for name in names.iter() {
1471 let test = TestDescAndFn {
1473 name: DynTestName((*name).clone()),
1477 testfn: DynTestFn(testfn),
1483 let filtered = filter_tests(&opts, tests);
1486 vec!(~"int::test_pow", ~"int::test_to_str", ~"sha1::test",
1487 ~"test::do_not_run_ignored_tests",
1488 ~"test::filter_for_ignored_option",
1489 ~"test::first_free_arg_should_be_a_filter",
1490 ~"test::ignored_tests_result_in_ignored",
1491 ~"test::parse_ignored_flag",
1492 ~"test::sort_tests");
1494 for (a, b) in expected.iter().zip(filtered.iter()) {
1495 assert!(*a == b.desc.name.to_str());
1500 pub fn test_metricmap_compare() {
1501 let mut m1 = MetricMap::new();
1502 let mut m2 = MetricMap::new();
1503 m1.insert_metric("in-both-noise", 1000.0, 200.0);
1504 m2.insert_metric("in-both-noise", 1100.0, 200.0);
1506 m1.insert_metric("in-first-noise", 1000.0, 2.0);
1507 m2.insert_metric("in-second-noise", 1000.0, 2.0);
1509 m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
1510 m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
1512 m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
1513 m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
1515 m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
1516 m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
1518 m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
1519 m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
1521 let diff1 = m2.compare_to_old(&m1, None);
1523 assert_eq!(*(diff1.find(&~"in-both-noise").unwrap()), LikelyNoise);
1524 assert_eq!(*(diff1.find(&~"in-first-noise").unwrap()), MetricRemoved);
1525 assert_eq!(*(diff1.find(&~"in-second-noise").unwrap()), MetricAdded);
1526 assert_eq!(*(diff1.find(&~"in-both-want-downwards-but-regressed").unwrap()),
1528 assert_eq!(*(diff1.find(&~"in-both-want-downwards-and-improved").unwrap()),
1530 assert_eq!(*(diff1.find(&~"in-both-want-upwards-but-regressed").unwrap()),
1532 assert_eq!(*(diff1.find(&~"in-both-want-upwards-and-improved").unwrap()),
1533 Improvement(100.0));
1534 assert_eq!(diff1.len(), 7);
1536 let diff2 = m2.compare_to_old(&m1, Some(200.0));
1538 assert_eq!(*(diff2.find(&~"in-both-noise").unwrap()), LikelyNoise);
1539 assert_eq!(*(diff2.find(&~"in-first-noise").unwrap()), MetricRemoved);
1540 assert_eq!(*(diff2.find(&~"in-second-noise").unwrap()), MetricAdded);
1541 assert_eq!(*(diff2.find(&~"in-both-want-downwards-but-regressed").unwrap()), LikelyNoise);
1542 assert_eq!(*(diff2.find(&~"in-both-want-downwards-and-improved").unwrap()), LikelyNoise);
1543 assert_eq!(*(diff2.find(&~"in-both-want-upwards-but-regressed").unwrap()), LikelyNoise);
1544 assert_eq!(*(diff2.find(&~"in-both-want-upwards-and-improved").unwrap()), LikelyNoise);
1545 assert_eq!(diff2.len(), 7);
1549 pub fn ratchet_test() {
1551 let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet");
1552 let pth = dpth.path().join("ratchet.json");
1554 let mut m1 = MetricMap::new();
1555 m1.insert_metric("runtime", 1000.0, 2.0);
1556 m1.insert_metric("throughput", 50.0, 2.0);
1558 let mut m2 = MetricMap::new();
1559 m2.insert_metric("runtime", 1100.0, 2.0);
1560 m2.insert_metric("throughput", 50.0, 2.0);
1562 m1.save(&pth).unwrap();
1564 // Ask for a ratchet that should fail to advance.
1565 let (diff1, ok1) = m2.ratchet(&pth, None);
1566 assert_eq!(ok1, false);
1567 assert_eq!(diff1.len(), 2);
1568 assert_eq!(*(diff1.find(&~"runtime").unwrap()), Regression(10.0));
1569 assert_eq!(*(diff1.find(&~"throughput").unwrap()), LikelyNoise);
1571 // Check that it was not rewritten.
1572 let m3 = MetricMap::load(&pth);
1573 let MetricMap(m3) = m3;
1574 assert_eq!(m3.len(), 2);
1575 assert_eq!(*(m3.find(&~"runtime").unwrap()), Metric::new(1000.0, 2.0));
1576 assert_eq!(*(m3.find(&~"throughput").unwrap()), Metric::new(50.0, 2.0));
1578 // Ask for a ratchet with an explicit noise-percentage override,
1579 // that should advance.
1580 let (diff2, ok2) = m2.ratchet(&pth, Some(10.0));
1581 assert_eq!(ok2, true);
1582 assert_eq!(diff2.len(), 2);
1583 assert_eq!(*(diff2.find(&~"runtime").unwrap()), LikelyNoise);
1584 assert_eq!(*(diff2.find(&~"throughput").unwrap()), LikelyNoise);
1586 // Check that it was rewritten.
1587 let m4 = MetricMap::load(&pth);
1588 let MetricMap(m4) = m4;
1589 assert_eq!(m4.len(), 2);
1590 assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric::new(1100.0, 2.0));
1591 assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric::new(50.0, 2.0));