1 // Copyright 2012-2013 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.
13 // Support code for rustc's built in test runner generator. Currently,
14 // none of this is meant for users. It is intended to support the
15 // simplest interface possible for representing and running tests
16 // while providing a base that other test frameworks may build off of.
23 use serialize::Decodable;
26 use time::precise_time_ns;
27 use collections::TreeMap;
31 use std::io::{File, PortReader, ChanWriter};
32 use std::io::stdio::StdWriter;
35 use std::to_str::ToStr;
39 // The name of a test. By convention this follows the rules for rust
40 // paths; i.e. it should be a series of identifiers separated by double
41 // colons. This way if some test runner wants to arrange the tests
42 // hierarchically it may.
46 StaticTestName(&'static str),
49 impl ToStr for TestName {
50 fn to_str(&self) -> ~str {
51 match (*self).clone() {
52 StaticTestName(s) => s.to_str(),
53 DynTestName(s) => s.to_str()
59 enum NamePadding { PadNone, PadOnLeft, PadOnRight }
62 fn padded_name(&self, column_count: uint, align: NamePadding) -> ~str {
63 use std::num::Saturating;
64 let name = self.name.to_str();
65 let fill = column_count.saturating_sub(name.len());
66 let pad = " ".repeat(fill);
69 PadOnLeft => pad.append(name),
70 PadOnRight => name.append(pad),
75 /// Represents a benchmark function.
76 pub trait TDynBenchFn {
77 fn run(&self, harness: &mut BenchHarness);
80 // A function that runs a test. If the function returns successfully,
81 // the test succeeds; if the function fails then the test fails. We
82 // may need to come up with a more clever definition of test in order
83 // to support isolation of tests into tasks.
85 StaticTestFn(extern fn()),
86 StaticBenchFn(extern fn(&mut BenchHarness)),
87 StaticMetricFn(proc(&mut MetricMap)),
89 DynMetricFn(proc(&mut MetricMap)),
90 DynBenchFn(~TDynBenchFn)
94 fn padding(&self) -> NamePadding {
96 &StaticTestFn(..) => PadNone,
97 &StaticBenchFn(..) => PadOnRight,
98 &StaticMetricFn(..) => PadOnRight,
99 &DynTestFn(..) => PadNone,
100 &DynMetricFn(..) => PadOnRight,
101 &DynBenchFn(..) => PadOnRight,
106 // Structure passed to BenchFns
107 pub struct BenchHarness {
108 priv iterations: u64,
114 // The definition of a single test. A test runner will run a list of
117 pub struct TestDesc {
123 pub struct TestDescAndFn {
128 #[deriving(Clone, Encodable, Decodable, Eq)]
135 pub struct MetricMap(TreeMap<~str,Metric>);
137 impl Clone for MetricMap {
138 fn clone(&self) -> MetricMap {
139 let MetricMap(ref map) = *self;
140 MetricMap(map.clone())
144 /// Analysis of a single change in metric
146 pub enum MetricChange {
154 pub type MetricDiff = TreeMap<~str,MetricChange>;
156 // The default console test runner. It accepts the command line
157 // arguments and a vector of test_descs.
158 pub fn test_main(args: &[~str], tests: ~[TestDescAndFn]) {
160 match parse_opts(args) {
162 Some(Err(msg)) => fail!("{}", msg),
165 match run_tests_console(&opts, tests) {
167 Ok(false) => fail!("Some tests failed"),
168 Err(e) => fail!("io error when running tests: {}", e),
172 // A variant optimized for invocation with a static test vector.
173 // This will fail (intentionally) when fed any dynamic tests, because
174 // it is copying the static values out into a dynamic vector and cannot
175 // copy dynamic values. It is doing this because from this point on
176 // a ~[TestDescAndFn] is used in order to effect ownership-transfer
177 // semantics into parallel test runners, which in turn requires a ~[]
178 // rather than a &[].
179 pub fn test_main_static(args: &[~str], tests: &[TestDescAndFn]) {
180 let owned_tests = tests.map(|t| {
183 TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
186 TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
189 fail!("non-static tests passed to test::test_main_static");
193 test_main(args, owned_tests)
196 pub struct TestOpts {
197 filter: Option<~str>,
200 run_benchmarks: bool,
201 ratchet_metrics: Option<Path>,
202 ratchet_noise_percent: Option<f64>,
203 save_metrics: Option<Path>,
204 test_shard: Option<(uint,uint)>,
205 logfile: Option<Path>
208 /// Result of parsing the options.
209 pub type OptRes = Result<TestOpts, ~str>;
211 fn optgroups() -> ~[getopts::OptGroup] {
212 ~[getopts::optflag("", "ignored", "Run ignored tests"),
213 getopts::optflag("", "test", "Run tests and not benchmarks"),
214 getopts::optflag("", "bench", "Run benchmarks instead of tests"),
215 getopts::optflag("h", "help", "Display this message (longer with --help)"),
216 getopts::optopt("", "save-metrics", "Location to save bench metrics",
218 getopts::optopt("", "ratchet-metrics",
219 "Location to load and save metrics from. The metrics \
220 loaded are cause benchmarks to fail if they run too \
222 getopts::optopt("", "ratchet-noise-percent",
223 "Tests within N% of the recorded metrics will be \
224 considered as passing", "PERCENTAGE"),
225 getopts::optopt("", "logfile", "Write logs to the specified file instead \
227 getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
231 fn usage(binary: &str, helpstr: &str) {
232 let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
233 println!("{}", getopts::usage(message, optgroups()));
235 if helpstr == "help" {
237 The FILTER is matched against the name of all tests to run, and if any tests
238 have a substring match, only those tests are run.
240 By default, all tests are run in parallel. This can be altered with the
241 RUST_TEST_TASKS environment variable when running tests (set it to 1).
245 #[test] - Indicates a function is a test to be run. This function
247 #[bench] - Indicates a function is a benchmark to be run. This
248 function takes one argument (extra::test::BenchHarness).
249 #[should_fail] - This function (also labeled with #[test]) will only pass if
250 the code causes a failure (an assertion failure or fail!)
251 #[ignore] - When applied to a function which is already attributed as a
252 test, then the test runner will ignore these tests during
253 normal test runs. Running with --ignored will run these
254 tests. This may also be written as #[ignore(cfg(...))] to
255 ignore the test on certain configurations.");
259 // Parses command line arguments into test options
260 pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
261 let args_ = args.tail();
263 match getopts::getopts(args_, optgroups()) {
265 Err(f) => return Some(Err(f.to_err_msg()))
268 if matches.opt_present("h") { usage(args[0], "h"); return None; }
269 if matches.opt_present("help") { usage(args[0], "help"); return None; }
272 if matches.free.len() > 0 {
273 Some((matches).free[0].clone())
278 let run_ignored = matches.opt_present("ignored");
280 let logfile = matches.opt_str("logfile");
281 let logfile = logfile.map(|s| Path::new(s));
283 let run_benchmarks = matches.opt_present("bench");
284 let run_tests = ! run_benchmarks ||
285 matches.opt_present("test");
287 let ratchet_metrics = matches.opt_str("ratchet-metrics");
288 let ratchet_metrics = ratchet_metrics.map(|s| Path::new(s));
290 let ratchet_noise_percent = matches.opt_str("ratchet-noise-percent");
291 let ratchet_noise_percent = ratchet_noise_percent.map(|s| from_str::<f64>(s).unwrap());
293 let save_metrics = matches.opt_str("save-metrics");
294 let save_metrics = save_metrics.map(|s| Path::new(s));
296 let test_shard = matches.opt_str("test-shard");
297 let test_shard = opt_shard(test_shard);
299 let test_opts = TestOpts {
301 run_ignored: run_ignored,
302 run_tests: run_tests,
303 run_benchmarks: run_benchmarks,
304 ratchet_metrics: ratchet_metrics,
305 ratchet_noise_percent: ratchet_noise_percent,
306 save_metrics: save_metrics,
307 test_shard: test_shard,
314 pub fn opt_shard(maybestr: Option<~str>) -> Option<(uint,uint)> {
318 match s.split('.').to_owned_vec() {
319 [a, b] => match (from_str::<uint>(a), from_str::<uint>(b)) {
320 (Some(a), Some(b)) => Some((a,b)),
330 #[deriving(Clone, Eq)]
331 pub struct BenchSamples {
332 priv ns_iter_summ: stats::Summary,
336 #[deriving(Clone, Eq)]
337 pub enum TestResult {
341 TrMetrics(MetricMap),
342 TrBench(BenchSamples),
345 enum OutputLocation<T> {
346 Pretty(term::Terminal<T>),
350 struct ConsoleTestState<T> {
351 log_out: Option<File>,
352 out: OutputLocation<T>,
360 failures: ~[(TestDesc, ~[u8])],
361 max_name_len: uint, // number of columns to fill when aligning names
364 impl<T: Writer> ConsoleTestState<T> {
365 pub fn new(opts: &TestOpts,
366 _: Option<T>) -> io::IoResult<ConsoleTestState<StdWriter>> {
367 let log_out = match opts.logfile {
368 Some(ref path) => Some(if_ok!(File::create(path))),
371 let out = match term::Terminal::new(io::stdout()) {
372 Err(_) => Raw(io::stdout()),
375 Ok(ConsoleTestState {
378 use_color: use_color(),
384 metrics: MetricMap::new(),
390 pub fn write_ok(&mut self) -> io::IoResult<()> {
391 self.write_pretty("ok", term::color::GREEN)
394 pub fn write_failed(&mut self) -> io::IoResult<()> {
395 self.write_pretty("FAILED", term::color::RED)
398 pub fn write_ignored(&mut self) -> io::IoResult<()> {
399 self.write_pretty("ignored", term::color::YELLOW)
402 pub fn write_metric(&mut self) -> io::IoResult<()> {
403 self.write_pretty("metric", term::color::CYAN)
406 pub fn write_bench(&mut self) -> io::IoResult<()> {
407 self.write_pretty("bench", term::color::CYAN)
410 pub fn write_added(&mut self) -> io::IoResult<()> {
411 self.write_pretty("added", term::color::GREEN)
414 pub fn write_improved(&mut self) -> io::IoResult<()> {
415 self.write_pretty("improved", term::color::GREEN)
418 pub fn write_removed(&mut self) -> io::IoResult<()> {
419 self.write_pretty("removed", term::color::YELLOW)
422 pub fn write_regressed(&mut self) -> io::IoResult<()> {
423 self.write_pretty("regressed", term::color::RED)
426 pub fn write_pretty(&mut self,
428 color: term::color::Color) -> io::IoResult<()> {
430 Pretty(ref mut term) => {
432 if_ok!(term.fg(color));
434 if_ok!(term.write(word.as_bytes()));
436 if_ok!(term.reset());
440 Raw(ref mut stdout) => stdout.write(word.as_bytes())
444 pub fn write_plain(&mut self, s: &str) -> io::IoResult<()> {
446 Pretty(ref mut term) => term.write(s.as_bytes()),
447 Raw(ref mut stdout) => stdout.write(s.as_bytes())
451 pub fn write_run_start(&mut self, len: uint) -> io::IoResult<()> {
453 let noun = if len != 1 { &"tests" } else { &"test" };
454 self.write_plain(format!("\nrunning {} {}\n", len, noun))
457 pub fn write_test_start(&mut self, test: &TestDesc,
458 align: NamePadding) -> io::IoResult<()> {
459 let name = test.padded_name(self.max_name_len, align);
460 self.write_plain(format!("test {} ... ", name))
463 pub fn write_result(&mut self, result: &TestResult) -> io::IoResult<()> {
464 if_ok!(match *result {
465 TrOk => self.write_ok(),
466 TrFailed => self.write_failed(),
467 TrIgnored => self.write_ignored(),
468 TrMetrics(ref mm) => {
469 if_ok!(self.write_metric());
470 self.write_plain(format!(": {}", fmt_metrics(mm)))
473 if_ok!(self.write_bench());
474 self.write_plain(format!(": {}", fmt_bench_samples(bs)))
477 self.write_plain("\n")
480 pub fn write_log(&mut self, test: &TestDesc,
481 result: &TestResult) -> io::IoResult<()> {
485 let s = format!("{} {}\n", match *result {
487 TrFailed => ~"failed",
488 TrIgnored => ~"ignored",
489 TrMetrics(ref mm) => fmt_metrics(mm),
490 TrBench(ref bs) => fmt_bench_samples(bs)
491 }, test.name.to_str());
492 o.write(s.as_bytes())
497 pub fn write_failures(&mut self) -> io::IoResult<()> {
498 if_ok!(self.write_plain("\nfailures:\n"));
499 let mut failures = ~[];
500 let mut fail_out = ~"";
501 for &(ref f, ref stdout) in self.failures.iter() {
502 failures.push(f.name.to_str());
503 if stdout.len() > 0 {
504 fail_out.push_str(format!("---- {} stdout ----\n\t",
506 let output = str::from_utf8_lossy(*stdout);
507 fail_out.push_str(output.as_slice().replace("\n", "\n\t"));
508 fail_out.push_str("\n");
511 if fail_out.len() > 0 {
512 if_ok!(self.write_plain("\n"));
513 if_ok!(self.write_plain(fail_out));
516 if_ok!(self.write_plain("\nfailures:\n"));
518 for name in failures.iter() {
519 if_ok!(self.write_plain(format!(" {}\n", name.to_str())));
524 pub fn write_metric_diff(&mut self, diff: &MetricDiff) -> io::IoResult<()> {
526 let mut improved = 0;
527 let mut regressed = 0;
531 for (k, v) in diff.iter() {
533 LikelyNoise => noise += 1,
536 if_ok!(self.write_added());
537 if_ok!(self.write_plain(format!(": {}\n", *k)));
541 if_ok!(self.write_removed());
542 if_ok!(self.write_plain(format!(": {}\n", *k)));
544 Improvement(pct) => {
546 if_ok!(self.write_plain(format!(": {}", *k)));
547 if_ok!(self.write_improved());
548 if_ok!(self.write_plain(format!(" by {:.2f}%\n", pct as f64)));
552 if_ok!(self.write_plain(format!(": {}", *k)));
553 if_ok!(self.write_regressed());
554 if_ok!(self.write_plain(format!(" by {:.2f}%\n", pct as f64)));
558 if_ok!(self.write_plain(format!("result of ratchet: {} matrics added, \
559 {} removed, {} improved, {} regressed, \
561 added, removed, improved, regressed,
564 if_ok!(self.write_plain("updated ratchet file\n"));
566 if_ok!(self.write_plain("left ratchet file untouched\n"));
571 pub fn write_run_finish(&mut self,
572 ratchet_metrics: &Option<Path>,
573 ratchet_pct: Option<f64>) -> io::IoResult<bool> {
574 assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
576 let ratchet_success = match *ratchet_metrics {
579 if_ok!(self.write_plain(format!("\nusing metrics ratcher: {}\n",
584 if_ok!(self.write_plain(format!("with noise-tolerance \
588 let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
589 if_ok!(self.write_metric_diff(&diff));
594 let test_success = self.failed == 0u;
596 if_ok!(self.write_failures());
599 let success = ratchet_success && test_success;
601 if_ok!(self.write_plain("\ntest result: "));
603 // There's no parallelism at this point so it's safe to use color
604 if_ok!(self.write_ok());
606 if_ok!(self.write_failed());
608 let s = format!(". {} passed; {} failed; {} ignored; {} measured\n\n",
609 self.passed, self.failed, self.ignored, self.measured);
610 if_ok!(self.write_plain(s));
615 pub fn fmt_metrics(mm: &MetricMap) -> ~str {
616 let MetricMap(ref mm) = *mm;
617 let v : ~[~str] = mm.iter()
618 .map(|(k,v)| format!("{}: {} (+/- {})",
626 pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
628 format!("{:>9} ns/iter (+/- {}) = {} MB/s",
629 bs.ns_iter_summ.median as uint,
630 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
633 format!("{:>9} ns/iter (+/- {})",
634 bs.ns_iter_summ.median as uint,
635 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
639 // A simple console test runner
640 pub fn run_tests_console(opts: &TestOpts,
641 tests: ~[TestDescAndFn]) -> io::IoResult<bool> {
642 fn callback<T: Writer>(event: &TestEvent,
643 st: &mut ConsoleTestState<T>) -> io::IoResult<()> {
644 debug!("callback(event={:?})", event);
645 match (*event).clone() {
646 TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
647 TeWait(ref test, padding) => st.write_test_start(test, padding),
648 TeResult(test, result, stdout) => {
649 if_ok!(st.write_log(&test, &result));
650 if_ok!(st.write_result(&result));
652 TrOk => st.passed += 1,
653 TrIgnored => st.ignored += 1,
655 let tname = test.name.to_str();
656 let MetricMap(mm) = mm;
657 for (k,v) in mm.iter() {
658 st.metrics.insert_metric(tname + "." + *k,
664 st.metrics.insert_metric(test.name.to_str(),
665 bs.ns_iter_summ.median,
666 bs.ns_iter_summ.max - bs.ns_iter_summ.min);
671 st.failures.push((test, stdout));
678 let mut st = if_ok!(ConsoleTestState::new(opts, None::<StdWriter>));
679 fn len_if_padded(t: &TestDescAndFn) -> uint {
680 match t.testfn.padding() {
682 PadOnLeft | PadOnRight => t.desc.name.to_str().len(),
685 match tests.iter().max_by(|t|len_if_padded(*t)) {
687 let n = t.desc.name.to_str();
688 debug!("Setting max_name_len from: {}", n);
689 st.max_name_len = n.len();
693 if_ok!(run_tests(opts, tests, |x| callback(&x, &mut st)));
694 match opts.save_metrics {
697 if_ok!(st.metrics.save(pth));
698 if_ok!(st.write_plain(format!("\nmetrics saved to: {}",
702 return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
706 fn should_sort_failures_before_printing_them() {
707 use std::io::MemWriter;
710 let test_a = TestDesc {
711 name: StaticTestName("a"),
716 let test_b = TestDesc {
717 name: StaticTestName("b"),
722 let mut st = ConsoleTestState {
724 out: Raw(MemWriter::new()),
732 metrics: MetricMap::new(),
733 failures: ~[(test_b, ~[]), (test_a, ~[])]
736 st.write_failures().unwrap();
737 let s = match st.out {
738 Raw(ref m) => str::from_utf8_lossy(m.get_ref()),
739 Pretty(_) => unreachable!()
742 let apos = s.as_slice().find_str("a").unwrap();
743 let bpos = s.as_slice().find_str("b").unwrap();
744 assert!(apos < bpos);
747 fn use_color() -> bool { return get_concurrency() == 1; }
751 TeFiltered(~[TestDesc]),
752 TeWait(TestDesc, NamePadding),
753 TeResult(TestDesc, TestResult, ~[u8] /* stdout */),
756 pub type MonitorMsg = (TestDesc, TestResult, ~[u8] /* stdout */);
758 fn run_tests(opts: &TestOpts,
759 tests: ~[TestDescAndFn],
760 callback: |e: TestEvent| -> io::IoResult<()>) -> io::IoResult<()> {
761 let filtered_tests = filter_tests(opts, tests);
762 let filtered_descs = filtered_tests.map(|t| t.desc.clone());
764 if_ok!(callback(TeFiltered(filtered_descs)));
766 let (filtered_tests, filtered_benchs_and_metrics) =
767 filtered_tests.partition(|e| {
769 StaticTestFn(_) | DynTestFn(_) => true,
774 // It's tempting to just spawn all the tests at once, but since we have
775 // many tests that run in other processes we would be making a big mess.
776 let concurrency = get_concurrency();
777 debug!("using {} test tasks", concurrency);
779 let mut remaining = filtered_tests;
783 let (p, ch) = Chan::new();
785 while pending > 0 || !remaining.is_empty() {
786 while pending < concurrency && !remaining.is_empty() {
787 let test = remaining.pop().unwrap();
788 if concurrency == 1 {
789 // We are doing one test at a time so we can print the name
790 // of the test before we run it. Useful for debugging tests
791 // that hang forever.
792 if_ok!(callback(TeWait(test.desc.clone(), test.testfn.padding())));
794 run_test(!opts.run_tests, test, ch.clone());
798 let (desc, result, stdout) = p.recv();
799 if concurrency != 1 {
800 if_ok!(callback(TeWait(desc.clone(), PadNone)));
802 if_ok!(callback(TeResult(desc, result, stdout)));
806 // All benchmarks run at the end, in serial.
807 // (this includes metric fns)
808 for b in filtered_benchs_and_metrics.move_iter() {
809 if_ok!(callback(TeWait(b.desc.clone(), b.testfn.padding())));
810 run_test(!opts.run_benchmarks, b, ch.clone());
811 let (test, result, stdout) = p.recv();
812 if_ok!(callback(TeResult(test, result, stdout)));
817 fn get_concurrency() -> uint {
819 match os::getenv("RUST_TEST_TASKS") {
821 let opt_n: Option<uint> = FromStr::from_str(s);
823 Some(n) if n > 0 => n,
824 _ => fail!("RUST_TEST_TASKS is `{}`, should be a positive integer.", s)
828 rt::default_sched_threads()
835 tests: ~[TestDescAndFn]) -> ~[TestDescAndFn]
837 let mut filtered = tests;
839 // Remove tests that don't match the test filter
840 filtered = if opts.filter.is_none() {
843 let filter_str = match opts.filter {
844 Some(ref f) => (*f).clone(),
848 fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
849 Option<TestDescAndFn> {
850 if test.desc.name.to_str().contains(filter_str) {
857 filtered.move_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
860 // Maybe pull out the ignored test and unignore them
861 filtered = if !opts.run_ignored {
864 fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
865 if test.desc.ignore {
866 let TestDescAndFn {desc, testfn} = test;
868 desc: TestDesc {ignore: false, ..desc},
875 filtered.move_iter().filter_map(|x| filter(x)).collect()
878 // Sort the tests alphabetically
879 filtered.sort_by(|t1, t2| t1.desc.name.to_str().cmp(&t2.desc.name.to_str()));
881 // Shard the remaining tests, if sharding requested.
882 match opts.test_shard {
885 filtered.move_iter().enumerate()
886 .filter(|&(i,_)| i % b == a)
892 pub fn run_test(force_ignore: bool,
894 monitor_ch: Chan<MonitorMsg>) {
896 let TestDescAndFn {desc, testfn} = test;
898 if force_ignore || desc.ignore {
899 monitor_ch.send((desc, TrIgnored, ~[]));
903 fn run_test_inner(desc: TestDesc,
904 monitor_ch: Chan<MonitorMsg>,
907 let (p, c) = Chan::new();
908 let mut reader = PortReader::new(p);
909 let stdout = ChanWriter::new(c.clone());
910 let stderr = ChanWriter::new(c);
911 let mut task = task::task().named(match desc.name {
912 DynTestName(ref name) => name.clone().into_maybe_owned(),
913 StaticTestName(name) => name.into_maybe_owned(),
915 task.opts.stdout = Some(~stdout as ~Writer);
916 task.opts.stderr = Some(~stderr as ~Writer);
917 let result_future = task.future_result();
920 let stdout = reader.read_to_end().unwrap();
921 let task_result = result_future.recv();
922 let test_result = calc_result(&desc, task_result.is_ok());
923 monitor_ch.send((desc.clone(), test_result, stdout));
928 DynBenchFn(bencher) => {
929 let bs = ::test::bench::benchmark(|harness| bencher.run(harness));
930 monitor_ch.send((desc, TrBench(bs), ~[]));
933 StaticBenchFn(benchfn) => {
934 let bs = ::test::bench::benchmark(|harness| benchfn(harness));
935 monitor_ch.send((desc, TrBench(bs), ~[]));
939 let mut mm = MetricMap::new();
941 monitor_ch.send((desc, TrMetrics(mm), ~[]));
944 StaticMetricFn(f) => {
945 let mut mm = MetricMap::new();
947 monitor_ch.send((desc, TrMetrics(mm), ~[]));
950 DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
951 StaticTestFn(f) => run_test_inner(desc, monitor_ch, proc() f())
955 fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
957 if desc.should_fail { TrFailed }
960 if desc.should_fail { TrOk }
966 impl ToJson for Metric {
967 fn to_json(&self) -> json::Json {
968 let mut map = ~TreeMap::new();
969 map.insert(~"value", json::Number(self.value));
970 map.insert(~"noise", json::Number(self.noise));
977 pub fn new() -> MetricMap {
978 MetricMap(TreeMap::new())
981 /// Load MetricDiff from a file.
985 /// This function will fail if the path does not exist or the path does not
986 /// contain a valid metric map.
987 pub fn load(p: &Path) -> MetricMap {
989 let mut f = File::open(p).unwrap();
990 let value = json::from_reader(&mut f as &mut io::Reader).unwrap();
991 let mut decoder = json::Decoder::new(value);
992 MetricMap(Decodable::decode(&mut decoder))
995 /// Write MetricDiff to a file.
996 pub fn save(&self, p: &Path) -> io::IoResult<()> {
997 let mut file = if_ok!(File::create(p));
998 let MetricMap(ref map) = *self;
999 map.to_json().to_pretty_writer(&mut file)
1002 /// Compare against another MetricMap. Optionally compare all
1003 /// measurements in the maps using the provided `noise_pct` as a
1004 /// percentage of each value to consider noise. If `None`, each
1005 /// measurement's noise threshold is independently chosen as the
1006 /// maximum of that measurement's recorded noise quantity in either
1008 pub fn compare_to_old(&self, old: &MetricMap,
1009 noise_pct: Option<f64>) -> MetricDiff {
1010 let mut diff : MetricDiff = TreeMap::new();
1011 let MetricMap(ref selfmap) = *self;
1012 let MetricMap(ref old) = *old;
1013 for (k, vold) in old.iter() {
1014 let r = match selfmap.find(k) {
1015 None => MetricRemoved,
1017 let delta = (v.value - vold.value);
1018 let noise = match noise_pct {
1019 None => f64::max(vold.noise.abs(), v.noise.abs()),
1020 Some(pct) => vold.value * pct / 100.0
1022 if delta.abs() <= noise {
1025 let pct = delta.abs() / cmp::max(vold.value, f64::EPSILON) * 100.0;
1026 if vold.noise < 0.0 {
1027 // When 'noise' is negative, it means we want
1028 // to see deltas that go up over time, and can
1029 // only tolerate slight negative movement.
1036 // When 'noise' is positive, it means we want
1037 // to see deltas that go down over time, and
1038 // can only tolerate slight positive movements.
1048 diff.insert((*k).clone(), r);
1050 let MetricMap(ref map) = *self;
1051 for (k, _) in map.iter() {
1052 if !diff.contains_key(k) {
1053 diff.insert((*k).clone(), MetricAdded);
1059 /// Insert a named `value` (+/- `noise`) metric into the map. The value
1060 /// must be non-negative. The `noise` indicates the uncertainty of the
1061 /// metric, which doubles as the "noise range" of acceptable
1062 /// pairwise-regressions on this named value, when comparing from one
1063 /// metric to the next using `compare_to_old`.
1065 /// If `noise` is positive, then it means this metric is of a value
1066 /// you want to see grow smaller, so a change larger than `noise` in the
1067 /// positive direction represents a regression.
1069 /// If `noise` is negative, then it means this metric is of a value
1070 /// you want to see grow larger, so a change larger than `noise` in the
1071 /// negative direction represents a regression.
1072 pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
1077 let MetricMap(ref mut map) = *self;
1078 map.insert(name.to_owned(), m);
1081 /// Attempt to "ratchet" an external metric file. This involves loading
1082 /// metrics from a metric file (if it exists), comparing against
1083 /// the metrics in `self` using `compare_to_old`, and rewriting the
1084 /// file to contain the metrics in `self` if none of the
1085 /// `MetricChange`s are `Regression`. Returns the diff as well
1086 /// as a boolean indicating whether the ratchet succeeded.
1087 pub fn ratchet(&self, p: &Path, pct: Option<f64>) -> (MetricDiff, bool) {
1088 let old = if p.exists() {
1094 let diff : MetricDiff = self.compare_to_old(&old, pct);
1095 let ok = diff.iter().all(|(_, v)| {
1097 Regression(_) => false,
1103 debug!("rewriting file '{:?}' with updated metrics", p);
1104 self.save(p).unwrap();
1113 /// A function that is opaque to the optimizer, to allow benchmarks to
1114 /// pretend to use outputs to assist in avoiding dead-code
1117 /// This function is a no-op, and does not even read from `dummy`.
1118 pub fn black_box<T>(dummy: T) {
1119 // we need to "use" the argument in some way LLVM can't
1121 unsafe {asm!("" : : "r"(&dummy))}
1126 /// Callback for benchmark functions to run in their body.
1127 pub fn iter<T>(&mut self, inner: || -> T) {
1128 self.ns_start = precise_time_ns();
1129 let k = self.iterations;
1130 for _ in range(0u64, k) {
1133 self.ns_end = precise_time_ns();
1136 pub fn ns_elapsed(&mut self) -> u64 {
1137 if self.ns_start == 0 || self.ns_end == 0 {
1140 self.ns_end - self.ns_start
1144 pub fn ns_per_iter(&mut self) -> u64 {
1145 if self.iterations == 0 {
1148 self.ns_elapsed() / cmp::max(self.iterations, 1)
1152 pub fn bench_n(&mut self, n: u64, f: |&mut BenchHarness|) {
1153 self.iterations = n;
1154 debug!("running benchmark for {} iterations",
1159 // This is a more statistics-driven benchmark algorithm
1160 pub fn auto_bench(&mut self, f: |&mut BenchHarness|) -> stats::Summary {
1162 // Initial bench run to get ballpark figure.
1164 self.bench_n(n, |x| f(x));
1166 // Try to estimate iter count for 1ms falling back to 1m
1167 // iterations if first run took < 1ns.
1168 if self.ns_per_iter() == 0 {
1171 n = 1_000_000 / cmp::max(self.ns_per_iter(), 1);
1173 // if the first run took more than 1ms we don't want to just
1174 // be left doing 0 iterations on every loop. The unfortunate
1175 // side effect of not being able to do as many runs is
1176 // automatically handled by the statistical analysis below
1177 // (i.e. larger error bars).
1178 if n == 0 { n = 1; }
1180 debug!("Initial run took {} ns, iter count that takes 1ms estimated as {}",
1181 self.ns_per_iter(), n);
1183 let mut total_run = 0;
1184 let samples : &mut [f64] = [0.0_f64, ..50];
1186 let loop_start = precise_time_ns();
1188 for p in samples.mut_iter() {
1189 self.bench_n(n, |x| f(x));
1190 *p = self.ns_per_iter() as f64;
1193 stats::winsorize(samples, 5.0);
1194 let summ = stats::Summary::new(samples);
1196 for p in samples.mut_iter() {
1197 self.bench_n(5 * n, |x| f(x));
1198 *p = self.ns_per_iter() as f64;
1201 stats::winsorize(samples, 5.0);
1202 let summ5 = stats::Summary::new(samples);
1204 debug!("{} samples, median {}, MAD={}, MADP={}",
1207 summ.median_abs_dev as f64,
1208 summ.median_abs_dev_pct as f64);
1210 let now = precise_time_ns();
1211 let loop_run = now - loop_start;
1213 // If we've run for 100ms and seem to have converged to a
1215 if loop_run > 100_000_000 &&
1216 summ.median_abs_dev_pct < 1.0 &&
1217 summ.median - summ5.median < summ5.median_abs_dev {
1221 total_run += loop_run;
1222 // Longest we ever run for is 3s.
1223 if total_run > 3_000_000_000 {
1238 use test::{BenchHarness, BenchSamples};
1240 pub fn benchmark(f: |&mut BenchHarness|) -> BenchSamples {
1241 let mut bs = BenchHarness {
1248 let ns_iter_summ = bs.auto_bench(f);
1250 let ns_iter = cmp::max(ns_iter_summ.median as u64, 1);
1251 let iter_s = 1_000_000_000 / ns_iter;
1252 let mb_s = (bs.bytes * iter_s) / 1_000_000;
1255 ns_iter_summ: ns_iter_summ,
1263 use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
1264 TestDesc, TestDescAndFn,
1265 Metric, MetricMap, MetricAdded, MetricRemoved,
1266 Improvement, Regression, LikelyNoise,
1267 StaticTestName, DynTestName, DynTestFn};
1268 use test::{TestOpts, run_test};
1270 use tempfile::TempDir;
1273 pub fn do_not_run_ignored_tests() {
1275 let desc = TestDescAndFn {
1277 name: StaticTestName("whatever"),
1281 testfn: DynTestFn(proc() f()),
1283 let (p, ch) = Chan::new();
1284 run_test(false, desc, ch);
1285 let (_, res, _) = p.recv();
1286 assert!(res != TrOk);
1290 pub fn ignored_tests_result_in_ignored() {
1292 let desc = TestDescAndFn {
1294 name: StaticTestName("whatever"),
1298 testfn: DynTestFn(proc() f()),
1300 let (p, ch) = Chan::new();
1301 run_test(false, desc, ch);
1302 let (_, res, _) = p.recv();
1303 assert_eq!(res, TrIgnored);
1307 fn test_should_fail() {
1309 let desc = TestDescAndFn {
1311 name: StaticTestName("whatever"),
1315 testfn: DynTestFn(proc() f()),
1317 let (p, ch) = Chan::new();
1318 run_test(false, desc, ch);
1319 let (_, res, _) = p.recv();
1320 assert_eq!(res, TrOk);
1324 fn test_should_fail_but_succeeds() {
1326 let desc = TestDescAndFn {
1328 name: StaticTestName("whatever"),
1332 testfn: DynTestFn(proc() f()),
1334 let (p, ch) = Chan::new();
1335 run_test(false, desc, ch);
1336 let (_, res, _) = p.recv();
1337 assert_eq!(res, TrFailed);
1341 fn first_free_arg_should_be_a_filter() {
1342 let args = ~[~"progname", ~"filter"];
1343 let opts = match parse_opts(args) {
1345 _ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
1347 assert!("filter" == opts.filter.clone().unwrap());
1351 fn parse_ignored_flag() {
1352 let args = ~[~"progname", ~"filter", ~"--ignored"];
1353 let opts = match parse_opts(args) {
1355 _ => fail!("Malformed arg in parse_ignored_flag")
1357 assert!((opts.run_ignored));
1361 pub fn filter_for_ignored_option() {
1362 // When we run ignored tests the test filter should filter out all the
1363 // unignored tests and flip the ignore flag on the rest to false
1365 let opts = TestOpts {
1370 run_benchmarks: false,
1371 ratchet_noise_percent: None,
1372 ratchet_metrics: None,
1380 name: StaticTestName("1"),
1384 testfn: DynTestFn(proc() {}),
1388 name: StaticTestName("2"),
1392 testfn: DynTestFn(proc() {}),
1395 let filtered = filter_tests(&opts, tests);
1397 assert_eq!(filtered.len(), 1);
1398 assert_eq!(filtered[0].desc.name.to_str(), ~"1");
1399 assert!(filtered[0].desc.ignore == false);
1403 pub fn sort_tests() {
1404 let opts = TestOpts {
1409 run_benchmarks: false,
1410 ratchet_noise_percent: None,
1411 ratchet_metrics: None,
1417 ~[~"sha1::test", ~"int::test_to_str", ~"int::test_pow",
1418 ~"test::do_not_run_ignored_tests",
1419 ~"test::ignored_tests_result_in_ignored",
1420 ~"test::first_free_arg_should_be_a_filter",
1421 ~"test::parse_ignored_flag", ~"test::filter_for_ignored_option",
1422 ~"test::sort_tests"];
1426 let mut tests = ~[];
1427 for name in names.iter() {
1428 let test = TestDescAndFn {
1430 name: DynTestName((*name).clone()),
1434 testfn: DynTestFn(testfn),
1440 let filtered = filter_tests(&opts, tests);
1443 ~[~"int::test_pow", ~"int::test_to_str", ~"sha1::test",
1444 ~"test::do_not_run_ignored_tests",
1445 ~"test::filter_for_ignored_option",
1446 ~"test::first_free_arg_should_be_a_filter",
1447 ~"test::ignored_tests_result_in_ignored",
1448 ~"test::parse_ignored_flag",
1449 ~"test::sort_tests"];
1451 for (a, b) in expected.iter().zip(filtered.iter()) {
1452 assert!(*a == b.desc.name.to_str());
1457 pub fn test_metricmap_compare() {
1458 let mut m1 = MetricMap::new();
1459 let mut m2 = MetricMap::new();
1460 m1.insert_metric("in-both-noise", 1000.0, 200.0);
1461 m2.insert_metric("in-both-noise", 1100.0, 200.0);
1463 m1.insert_metric("in-first-noise", 1000.0, 2.0);
1464 m2.insert_metric("in-second-noise", 1000.0, 2.0);
1466 m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
1467 m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
1469 m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
1470 m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
1472 m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
1473 m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
1475 m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
1476 m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
1478 let diff1 = m2.compare_to_old(&m1, None);
1480 assert_eq!(*(diff1.find(&~"in-both-noise").unwrap()), LikelyNoise);
1481 assert_eq!(*(diff1.find(&~"in-first-noise").unwrap()), MetricRemoved);
1482 assert_eq!(*(diff1.find(&~"in-second-noise").unwrap()), MetricAdded);
1483 assert_eq!(*(diff1.find(&~"in-both-want-downwards-but-regressed").unwrap()),
1485 assert_eq!(*(diff1.find(&~"in-both-want-downwards-and-improved").unwrap()),
1487 assert_eq!(*(diff1.find(&~"in-both-want-upwards-but-regressed").unwrap()),
1489 assert_eq!(*(diff1.find(&~"in-both-want-upwards-and-improved").unwrap()),
1490 Improvement(100.0));
1491 assert_eq!(diff1.len(), 7);
1493 let diff2 = m2.compare_to_old(&m1, Some(200.0));
1495 assert_eq!(*(diff2.find(&~"in-both-noise").unwrap()), LikelyNoise);
1496 assert_eq!(*(diff2.find(&~"in-first-noise").unwrap()), MetricRemoved);
1497 assert_eq!(*(diff2.find(&~"in-second-noise").unwrap()), MetricAdded);
1498 assert_eq!(*(diff2.find(&~"in-both-want-downwards-but-regressed").unwrap()), LikelyNoise);
1499 assert_eq!(*(diff2.find(&~"in-both-want-downwards-and-improved").unwrap()), LikelyNoise);
1500 assert_eq!(*(diff2.find(&~"in-both-want-upwards-but-regressed").unwrap()), LikelyNoise);
1501 assert_eq!(*(diff2.find(&~"in-both-want-upwards-and-improved").unwrap()), LikelyNoise);
1502 assert_eq!(diff2.len(), 7);
1506 pub fn ratchet_test() {
1508 let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet");
1509 let pth = dpth.path().join("ratchet.json");
1511 let mut m1 = MetricMap::new();
1512 m1.insert_metric("runtime", 1000.0, 2.0);
1513 m1.insert_metric("throughput", 50.0, 2.0);
1515 let mut m2 = MetricMap::new();
1516 m2.insert_metric("runtime", 1100.0, 2.0);
1517 m2.insert_metric("throughput", 50.0, 2.0);
1519 m1.save(&pth).unwrap();
1521 // Ask for a ratchet that should fail to advance.
1522 let (diff1, ok1) = m2.ratchet(&pth, None);
1523 assert_eq!(ok1, false);
1524 assert_eq!(diff1.len(), 2);
1525 assert_eq!(*(diff1.find(&~"runtime").unwrap()), Regression(10.0));
1526 assert_eq!(*(diff1.find(&~"throughput").unwrap()), LikelyNoise);
1528 // Check that it was not rewritten.
1529 let m3 = MetricMap::load(&pth);
1530 let MetricMap(m3) = m3;
1531 assert_eq!(m3.len(), 2);
1532 assert_eq!(*(m3.find(&~"runtime").unwrap()), Metric { value: 1000.0, noise: 2.0 });
1533 assert_eq!(*(m3.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });
1535 // Ask for a ratchet with an explicit noise-percentage override,
1536 // that should advance.
1537 let (diff2, ok2) = m2.ratchet(&pth, Some(10.0));
1538 assert_eq!(ok2, true);
1539 assert_eq!(diff2.len(), 2);
1540 assert_eq!(*(diff2.find(&~"runtime").unwrap()), LikelyNoise);
1541 assert_eq!(*(diff2.find(&~"throughput").unwrap()), LikelyNoise);
1543 // Check that it was rewritten.
1544 let m4 = MetricMap::load(&pth);
1545 let MetricMap(m4) = m4;
1546 assert_eq!(m4.len(), 2);
1547 assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric { value: 1100.0, noise: 2.0 });
1548 assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });