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;
27 use time::precise_time_ns;
30 use std::clone::Clone;
34 use std::io::stdio::StdWriter;
36 use std::to_str::ToStr;
40 // The name of a test. By convention this follows the rules for rust
41 // paths; i.e. it should be a series of identifiers separated by double
42 // colons. This way if some test runner wants to arrange the tests
43 // hierarchically it may.
47 StaticTestName(&'static str),
50 impl ToStr for TestName {
51 fn to_str(&self) -> ~str {
52 match (*self).clone() {
53 StaticTestName(s) => s.to_str(),
54 DynTestName(s) => s.to_str()
60 enum NamePadding { PadNone, PadOnLeft, PadOnRight }
63 fn padded_name(&self, column_count: uint, align: NamePadding) -> ~str {
64 use std::num::Saturating;
65 let name = self.name.to_str();
66 let fill = column_count.saturating_sub(name.len());
67 let pad = " ".repeat(fill);
70 PadOnLeft => pad.append(name),
71 PadOnRight => name.append(pad),
76 /// Represents a benchmark function.
77 pub trait TDynBenchFn {
78 fn run(&self, harness: &mut BenchHarness);
81 // A function that runs a test. If the function returns successfully,
82 // the test succeeds; if the function fails then the test fails. We
83 // may need to come up with a more clever definition of test in order
84 // to support isolation of tests into tasks.
86 StaticTestFn(extern fn()),
87 StaticBenchFn(extern fn(&mut BenchHarness)),
88 StaticMetricFn(proc(&mut MetricMap)),
90 DynMetricFn(proc(&mut MetricMap)),
91 DynBenchFn(~TDynBenchFn)
95 fn padding(&self) -> NamePadding {
97 &StaticTestFn(..) => PadNone,
98 &StaticBenchFn(..) => PadOnRight,
99 &StaticMetricFn(..) => PadOnRight,
100 &DynTestFn(..) => PadNone,
101 &DynMetricFn(..) => PadOnRight,
102 &DynBenchFn(..) => PadOnRight,
107 // Structure passed to BenchFns
108 pub struct BenchHarness {
109 priv iterations: u64,
115 // The definition of a single test. A test runner will run a list of
118 pub struct TestDesc {
124 pub struct TestDescAndFn {
129 #[deriving(Clone, Encodable, Decodable, Eq)]
136 pub struct MetricMap(TreeMap<~str,Metric>);
138 impl Clone for MetricMap {
139 fn clone(&self) -> MetricMap {
140 let MetricMap(ref map) = *self;
141 MetricMap(map.clone())
145 /// Analysis of a single change in metric
147 pub enum MetricChange {
155 pub type MetricDiff = TreeMap<~str,MetricChange>;
157 // The default console test runner. It accepts the command line
158 // arguments and a vector of test_descs.
159 pub fn test_main(args: &[~str], tests: ~[TestDescAndFn]) {
161 match parse_opts(args) {
163 Some(Err(msg)) => fail!("{}", msg),
166 if !run_tests_console(&opts, tests) { fail!("Some tests failed"); }
169 // A variant optimized for invocation with a static test vector.
170 // This will fail (intentionally) when fed any dynamic tests, because
171 // it is copying the static values out into a dynamic vector and cannot
172 // copy dynamic values. It is doing this because from this point on
173 // a ~[TestDescAndFn] is used in order to effect ownership-transfer
174 // semantics into parallel test runners, which in turn requires a ~[]
175 // rather than a &[].
176 pub fn test_main_static(args: &[~str], tests: &[TestDescAndFn]) {
177 let owned_tests = tests.map(|t| {
180 TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
183 TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
186 fail!("non-static tests passed to test::test_main_static");
190 test_main(args, owned_tests)
193 pub struct TestOpts {
194 filter: Option<~str>,
197 run_benchmarks: bool,
198 ratchet_metrics: Option<Path>,
199 ratchet_noise_percent: Option<f64>,
200 save_metrics: Option<Path>,
201 test_shard: Option<(uint,uint)>,
202 logfile: Option<Path>
205 type OptRes = Result<TestOpts, ~str>;
207 fn optgroups() -> ~[getopts::groups::OptGroup] {
208 ~[groups::optflag("", "ignored", "Run ignored tests"),
209 groups::optflag("", "test", "Run tests and not benchmarks"),
210 groups::optflag("", "bench", "Run benchmarks instead of tests"),
211 groups::optflag("h", "help", "Display this message (longer with --help)"),
212 groups::optopt("", "save-metrics", "Location to save bench metrics",
214 groups::optopt("", "ratchet-metrics",
215 "Location to load and save metrics from. The metrics \
216 loaded are cause benchmarks to fail if they run too \
218 groups::optopt("", "ratchet-noise-percent",
219 "Tests within N% of the recorded metrics will be \
220 considered as passing", "PERCENTAGE"),
221 groups::optopt("", "logfile", "Write logs to the specified file instead \
223 groups::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
227 fn usage(binary: &str, helpstr: &str) {
228 let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
229 println!("{}", groups::usage(message, optgroups()));
231 if helpstr == "help" {
233 The FILTER is matched against the name of all tests to run, and if any tests
234 have a substring match, only those tests are run.
236 By default, all tests are run in parallel. This can be altered with the
237 RUST_TEST_TASKS environment variable when running tests (set it to 1).
241 #[test] - Indicates a function is a test to be run. This function
243 #[bench] - Indicates a function is a benchmark to be run. This
244 function takes one argument (extra::test::BenchHarness).
245 #[should_fail] - This function (also labeled with #[test]) will only pass if
246 the code causes a failure (an assertion failure or fail!)
247 #[ignore] - When applied to a function which is already attributed as a
248 test, then the test runner will ignore these tests during
249 normal test runs. Running with --ignored will run these
250 tests. This may also be written as #[ignore(cfg(...))] to
251 ignore the test on certain configurations.");
255 // Parses command line arguments into test options
256 pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
257 let args_ = args.tail();
259 match groups::getopts(args_, optgroups()) {
261 Err(f) => return Some(Err(f.to_err_msg()))
264 if matches.opt_present("h") { usage(args[0], "h"); return None; }
265 if matches.opt_present("help") { usage(args[0], "help"); return None; }
268 if matches.free.len() > 0 {
269 Some((matches).free[0].clone())
274 let run_ignored = matches.opt_present("ignored");
276 let logfile = matches.opt_str("logfile");
277 let logfile = logfile.map(|s| Path::new(s));
279 let run_benchmarks = matches.opt_present("bench");
280 let run_tests = ! run_benchmarks ||
281 matches.opt_present("test");
283 let ratchet_metrics = matches.opt_str("ratchet-metrics");
284 let ratchet_metrics = ratchet_metrics.map(|s| Path::new(s));
286 let ratchet_noise_percent = matches.opt_str("ratchet-noise-percent");
287 let ratchet_noise_percent = ratchet_noise_percent.map(|s| from_str::<f64>(s).unwrap());
289 let save_metrics = matches.opt_str("save-metrics");
290 let save_metrics = save_metrics.map(|s| Path::new(s));
292 let test_shard = matches.opt_str("test-shard");
293 let test_shard = opt_shard(test_shard);
295 let test_opts = TestOpts {
297 run_ignored: run_ignored,
298 run_tests: run_tests,
299 run_benchmarks: run_benchmarks,
300 ratchet_metrics: ratchet_metrics,
301 ratchet_noise_percent: ratchet_noise_percent,
302 save_metrics: save_metrics,
303 test_shard: test_shard,
310 pub fn opt_shard(maybestr: Option<~str>) -> Option<(uint,uint)> {
314 match s.split('.').to_owned_vec() {
315 [a, b] => match (from_str::<uint>(a), from_str::<uint>(b)) {
316 (Some(a), Some(b)) => Some((a,b)),
326 #[deriving(Clone, Eq)]
327 pub struct BenchSamples {
328 priv ns_iter_summ: stats::Summary,
332 #[deriving(Clone, Eq)]
333 pub enum TestResult {
337 TrMetrics(MetricMap),
338 TrBench(BenchSamples),
341 enum OutputLocation<T> {
342 Pretty(term::Terminal<T>),
346 struct ConsoleTestState<T> {
347 log_out: Option<File>,
348 out: OutputLocation<T>,
356 failures: ~[TestDesc],
357 max_name_len: uint, // number of columns to fill when aligning names
360 impl<T: Writer> ConsoleTestState<T> {
361 pub fn new(opts: &TestOpts, _: Option<T>) -> ConsoleTestState<StdWriter> {
362 let log_out = match opts.logfile {
363 Some(ref path) => File::create(path),
366 let out = match term::Terminal::new(io::stdout()) {
367 Err(_) => Raw(io::stdout()),
373 use_color: use_color(),
379 metrics: MetricMap::new(),
385 pub fn write_ok(&mut self) {
386 self.write_pretty("ok", term::color::GREEN);
389 pub fn write_failed(&mut self) {
390 self.write_pretty("FAILED", term::color::RED);
393 pub fn write_ignored(&mut self) {
394 self.write_pretty("ignored", term::color::YELLOW);
397 pub fn write_metric(&mut self) {
398 self.write_pretty("metric", term::color::CYAN);
401 pub fn write_bench(&mut self) {
402 self.write_pretty("bench", term::color::CYAN);
405 pub fn write_added(&mut self) {
406 self.write_pretty("added", term::color::GREEN);
409 pub fn write_improved(&mut self) {
410 self.write_pretty("improved", term::color::GREEN);
413 pub fn write_removed(&mut self) {
414 self.write_pretty("removed", term::color::YELLOW);
417 pub fn write_regressed(&mut self) {
418 self.write_pretty("regressed", term::color::RED);
421 pub fn write_pretty(&mut self,
423 color: term::color::Color) {
425 Pretty(ref mut term) => {
429 term.write(word.as_bytes());
434 Raw(ref mut stdout) => stdout.write(word.as_bytes())
438 pub fn write_plain(&mut self, s: &str) {
440 Pretty(ref mut term) => term.write(s.as_bytes()),
441 Raw(ref mut stdout) => stdout.write(s.as_bytes())
445 pub fn write_run_start(&mut self, len: uint) {
447 let noun = if len != 1 { &"tests" } else { &"test" };
448 self.write_plain(format!("\nrunning {} {}\n", len, noun));
451 pub fn write_test_start(&mut self, test: &TestDesc, align: NamePadding) {
452 let name = test.padded_name(self.max_name_len, align);
453 self.write_plain(format!("test {} ... ", name));
456 pub fn write_result(&mut self, result: &TestResult) {
458 TrOk => self.write_ok(),
459 TrFailed => self.write_failed(),
460 TrIgnored => self.write_ignored(),
461 TrMetrics(ref mm) => {
463 self.write_plain(format!(": {}", fmt_metrics(mm)));
467 self.write_plain(format!(": {}", fmt_bench_samples(bs)));
470 self.write_plain("\n");
473 pub fn write_log(&mut self, test: &TestDesc, result: &TestResult) {
477 let s = format!("{} {}\n", match *result {
479 TrFailed => ~"failed",
480 TrIgnored => ~"ignored",
481 TrMetrics(ref mm) => fmt_metrics(mm),
482 TrBench(ref bs) => fmt_bench_samples(bs)
483 }, test.name.to_str());
484 o.write(s.as_bytes());
489 pub fn write_failures(&mut self) {
490 self.write_plain("\nfailures:\n");
491 let mut failures = ~[];
492 for f in self.failures.iter() {
493 failures.push(f.name.to_str());
496 for name in failures.iter() {
497 self.write_plain(format!(" {}\n", name.to_str()));
501 pub fn write_metric_diff(&mut self, diff: &MetricDiff) {
503 let mut improved = 0;
504 let mut regressed = 0;
508 for (k, v) in diff.iter() {
510 LikelyNoise => noise += 1,
514 self.write_plain(format!(": {}\n", *k));
518 self.write_removed();
519 self.write_plain(format!(": {}\n", *k));
521 Improvement(pct) => {
523 self.write_plain(format!(": {}", *k));
524 self.write_improved();
525 self.write_plain(format!(" by {:.2f}%\n", pct as f64));
529 self.write_plain(format!(": {}", *k));
530 self.write_regressed();
531 self.write_plain(format!(" by {:.2f}%\n", pct as f64));
535 self.write_plain(format!("result of ratchet: {} matrics added, {} removed, \
536 {} improved, {} regressed, {} noise\n",
537 added, removed, improved, regressed, noise));
539 self.write_plain("updated ratchet file\n");
541 self.write_plain("left ratchet file untouched\n");
545 pub fn write_run_finish(&mut self,
546 ratchet_metrics: &Option<Path>,
547 ratchet_pct: Option<f64>) -> bool {
548 assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
550 let ratchet_success = match *ratchet_metrics {
553 self.write_plain(format!("\nusing metrics ratcher: {}\n", pth.display()));
557 self.write_plain(format!("with noise-tolerance forced to: {}%\n",
560 let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
561 self.write_metric_diff(&diff);
566 let test_success = self.failed == 0u;
568 self.write_failures();
571 let success = ratchet_success && test_success;
573 self.write_plain("\ntest result: ");
575 // There's no parallelism at this point so it's safe to use color
580 let s = format!(". {} passed; {} failed; {} ignored; {} measured\n\n",
581 self.passed, self.failed, self.ignored, self.measured);
587 pub fn fmt_metrics(mm: &MetricMap) -> ~str {
588 let MetricMap(ref mm) = *mm;
589 let v : ~[~str] = mm.iter()
590 .map(|(k,v)| format!("{}: {} (+/- {})",
598 pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
600 format!("{:>9} ns/iter (+/- {}) = {} MB/s",
601 bs.ns_iter_summ.median as uint,
602 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
605 format!("{:>9} ns/iter (+/- {})",
606 bs.ns_iter_summ.median as uint,
607 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
611 // A simple console test runner
612 pub fn run_tests_console(opts: &TestOpts,
613 tests: ~[TestDescAndFn]) -> bool {
614 fn callback<T: Writer>(event: &TestEvent, st: &mut ConsoleTestState<T>) {
615 debug!("callback(event={:?})", event);
616 match (*event).clone() {
617 TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
618 TeWait(ref test, padding) => st.write_test_start(test, padding),
619 TeResult(test, result) => {
620 st.write_log(&test, &result);
621 st.write_result(&result);
623 TrOk => st.passed += 1,
624 TrIgnored => st.ignored += 1,
626 let tname = test.name.to_str();
627 let MetricMap(mm) = mm;
628 for (k,v) in mm.iter() {
629 st.metrics.insert_metric(tname + "." + *k,
635 st.metrics.insert_metric(test.name.to_str(),
636 bs.ns_iter_summ.median,
637 bs.ns_iter_summ.max - bs.ns_iter_summ.min);
642 st.failures.push(test);
648 let mut st = ConsoleTestState::new(opts, None::<StdWriter>);
649 fn len_if_padded(t: &TestDescAndFn) -> uint {
650 match t.testfn.padding() {
652 PadOnLeft | PadOnRight => t.desc.name.to_str().len(),
655 match tests.iter().max_by(|t|len_if_padded(*t)) {
657 let n = t.desc.name.to_str();
658 debug!("Setting max_name_len from: {}", n);
659 st.max_name_len = n.len();
663 run_tests(opts, tests, |x| callback(&x, &mut st));
664 match opts.save_metrics {
667 st.metrics.save(pth);
668 st.write_plain(format!("\nmetrics saved to: {}", pth.display()));
671 return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
675 fn should_sort_failures_before_printing_them() {
676 use std::io::MemWriter;
679 let test_a = TestDesc {
680 name: StaticTestName("a"),
685 let test_b = TestDesc {
686 name: StaticTestName("b"),
691 let mut st = ConsoleTestState {
693 out: Raw(MemWriter::new()),
701 metrics: MetricMap::new(),
702 failures: ~[test_b, test_a]
706 let s = match st.out {
707 Raw(ref m) => str::from_utf8(m.get_ref()),
708 Pretty(_) => unreachable!()
711 let apos = s.find_str("a").unwrap();
712 let bpos = s.find_str("b").unwrap();
713 assert!(apos < bpos);
716 fn use_color() -> bool { return get_concurrency() == 1; }
720 TeFiltered(~[TestDesc]),
721 TeWait(TestDesc, NamePadding),
722 TeResult(TestDesc, TestResult),
725 type MonitorMsg = (TestDesc, TestResult);
727 fn run_tests(opts: &TestOpts,
728 tests: ~[TestDescAndFn],
729 callback: |e: TestEvent|) {
730 let filtered_tests = filter_tests(opts, tests);
731 let filtered_descs = filtered_tests.map(|t| t.desc.clone());
733 callback(TeFiltered(filtered_descs));
735 let (filtered_tests, filtered_benchs_and_metrics) =
736 filtered_tests.partition(|e| {
738 StaticTestFn(_) | DynTestFn(_) => true,
743 // It's tempting to just spawn all the tests at once, but since we have
744 // many tests that run in other processes we would be making a big mess.
745 let concurrency = get_concurrency();
746 debug!("using {} test tasks", concurrency);
748 let mut remaining = filtered_tests;
752 let (p, ch) = SharedChan::new();
754 while pending > 0 || !remaining.is_empty() {
755 while pending < concurrency && !remaining.is_empty() {
756 let test = remaining.pop();
757 if concurrency == 1 {
758 // We are doing one test at a time so we can print the name
759 // of the test before we run it. Useful for debugging tests
760 // that hang forever.
761 callback(TeWait(test.desc.clone(), test.testfn.padding()));
763 run_test(!opts.run_tests, test, ch.clone());
767 let (desc, result) = p.recv();
768 if concurrency != 1 {
769 callback(TeWait(desc.clone(), PadNone));
771 callback(TeResult(desc, result));
775 // All benchmarks run at the end, in serial.
776 // (this includes metric fns)
777 for b in filtered_benchs_and_metrics.move_iter() {
778 callback(TeWait(b.desc.clone(), b.testfn.padding()));
779 run_test(!opts.run_benchmarks, b, ch.clone());
780 let (test, result) = p.recv();
781 callback(TeResult(test, result));
785 fn get_concurrency() -> uint {
787 match os::getenv("RUST_TEST_TASKS") {
789 let opt_n: Option<uint> = FromStr::from_str(s);
791 Some(n) if n > 0 => n,
792 _ => fail!("RUST_TEST_TASKS is `{}`, should be a positive integer.", s)
796 rt::default_sched_threads()
803 tests: ~[TestDescAndFn]) -> ~[TestDescAndFn]
805 let mut filtered = tests;
807 // Remove tests that don't match the test filter
808 filtered = if opts.filter.is_none() {
811 let filter_str = match opts.filter {
812 Some(ref f) => (*f).clone(),
816 fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
817 Option<TestDescAndFn> {
818 if test.desc.name.to_str().contains(filter_str) {
825 filtered.move_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
828 // Maybe pull out the ignored test and unignore them
829 filtered = if !opts.run_ignored {
832 fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
833 if test.desc.ignore {
834 let TestDescAndFn {desc, testfn} = test;
836 desc: TestDesc {ignore: false, ..desc},
843 filtered.move_iter().filter_map(|x| filter(x)).collect()
846 // Sort the tests alphabetically
847 filtered.sort_by(|t1, t2| t1.desc.name.to_str().cmp(&t2.desc.name.to_str()));
849 // Shard the remaining tests, if sharding requested.
850 match opts.test_shard {
853 filtered.move_iter().enumerate()
854 .filter(|&(i,_)| i % b == a)
860 pub fn run_test(force_ignore: bool,
862 monitor_ch: SharedChan<MonitorMsg>) {
864 let TestDescAndFn {desc, testfn} = test;
866 if force_ignore || desc.ignore {
867 monitor_ch.send((desc, TrIgnored));
871 fn run_test_inner(desc: TestDesc,
872 monitor_ch: SharedChan<MonitorMsg>,
875 let mut task = task::task();
876 task.name(match desc.name {
877 DynTestName(ref name) => SendStrOwned(name.clone()),
878 StaticTestName(name) => SendStrStatic(name),
880 let result_future = task.future_result();
883 let task_result = result_future.recv();
884 let test_result = calc_result(&desc, task_result.is_ok());
885 monitor_ch.send((desc.clone(), test_result));
890 DynBenchFn(bencher) => {
891 let bs = ::test::bench::benchmark(|harness| bencher.run(harness));
892 monitor_ch.send((desc, TrBench(bs)));
895 StaticBenchFn(benchfn) => {
896 let bs = ::test::bench::benchmark(benchfn);
897 monitor_ch.send((desc, TrBench(bs)));
901 let mut mm = MetricMap::new();
903 monitor_ch.send((desc, TrMetrics(mm)));
906 StaticMetricFn(f) => {
907 let mut mm = MetricMap::new();
909 monitor_ch.send((desc, TrMetrics(mm)));
912 DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
913 StaticTestFn(f) => run_test_inner(desc, monitor_ch, proc() f())
917 fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
919 if desc.should_fail { TrFailed }
922 if desc.should_fail { TrOk }
928 impl ToJson for Metric {
929 fn to_json(&self) -> json::Json {
930 let mut map = ~TreeMap::new();
931 map.insert(~"value", json::Number(self.value));
932 map.insert(~"noise", json::Number(self.noise));
939 pub fn new() -> MetricMap {
940 MetricMap(TreeMap::new())
943 /// Load MetricDiff from a file.
944 pub fn load(p: &Path) -> MetricMap {
946 let mut f = File::open(p);
947 let value = json::from_reader(&mut f as &mut io::Reader).unwrap();
948 let mut decoder = json::Decoder::new(value);
949 MetricMap(Decodable::decode(&mut decoder))
952 /// Write MetricDiff to a file.
953 pub fn save(&self, p: &Path) {
954 let mut file = File::create(p);
955 let MetricMap(ref map) = *self;
956 map.to_json().to_pretty_writer(&mut file)
959 /// Compare against another MetricMap. Optionally compare all
960 /// measurements in the maps using the provided `noise_pct` as a
961 /// percentage of each value to consider noise. If `None`, each
962 /// measurement's noise threshold is independently chosen as the
963 /// maximum of that measurement's recorded noise quantity in either
965 pub fn compare_to_old(&self, old: &MetricMap,
966 noise_pct: Option<f64>) -> MetricDiff {
967 let mut diff : MetricDiff = TreeMap::new();
968 let MetricMap(ref selfmap) = *self;
969 let MetricMap(ref old) = *old;
970 for (k, vold) in old.iter() {
971 let r = match selfmap.find(k) {
972 None => MetricRemoved,
974 let delta = (v.value - vold.value);
975 let noise = match noise_pct {
976 None => f64::max(vold.noise.abs(), v.noise.abs()),
977 Some(pct) => vold.value * pct / 100.0
979 if delta.abs() <= noise {
982 let pct = delta.abs() / (vold.value).max(&f64::EPSILON) * 100.0;
983 if vold.noise < 0.0 {
984 // When 'noise' is negative, it means we want
985 // to see deltas that go up over time, and can
986 // only tolerate slight negative movement.
993 // When 'noise' is positive, it means we want
994 // to see deltas that go down over time, and
995 // can only tolerate slight positive movements.
1005 diff.insert((*k).clone(), r);
1007 let MetricMap(ref map) = *self;
1008 for (k, _) in map.iter() {
1009 if !diff.contains_key(k) {
1010 diff.insert((*k).clone(), MetricAdded);
1016 /// Insert a named `value` (+/- `noise`) metric into the map. The value
1017 /// must be non-negative. The `noise` indicates the uncertainty of the
1018 /// metric, which doubles as the "noise range" of acceptable
1019 /// pairwise-regressions on this named value, when comparing from one
1020 /// metric to the next using `compare_to_old`.
1022 /// If `noise` is positive, then it means this metric is of a value
1023 /// you want to see grow smaller, so a change larger than `noise` in the
1024 /// positive direction represents a regression.
1026 /// If `noise` is negative, then it means this metric is of a value
1027 /// you want to see grow larger, so a change larger than `noise` in the
1028 /// negative direction represents a regression.
1029 pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
1034 let MetricMap(ref mut map) = *self;
1035 map.insert(name.to_owned(), m);
1038 /// Attempt to "ratchet" an external metric file. This involves loading
1039 /// metrics from a metric file (if it exists), comparing against
1040 /// the metrics in `self` using `compare_to_old`, and rewriting the
1041 /// file to contain the metrics in `self` if none of the
1042 /// `MetricChange`s are `Regression`. Returns the diff as well
1043 /// as a boolean indicating whether the ratchet succeeded.
1044 pub fn ratchet(&self, p: &Path, pct: Option<f64>) -> (MetricDiff, bool) {
1045 let old = if p.exists() {
1051 let diff : MetricDiff = self.compare_to_old(&old, pct);
1052 let ok = diff.iter().all(|(_, v)| {
1054 Regression(_) => false,
1060 debug!("rewriting file '{:?}' with updated metrics", p);
1071 /// Callback for benchmark functions to run in their body.
1072 pub fn iter(&mut self, inner: ||) {
1073 self.ns_start = precise_time_ns();
1074 let k = self.iterations;
1075 for _ in range(0u64, k) {
1078 self.ns_end = precise_time_ns();
1081 pub fn ns_elapsed(&mut self) -> u64 {
1082 if self.ns_start == 0 || self.ns_end == 0 {
1085 self.ns_end - self.ns_start
1089 pub fn ns_per_iter(&mut self) -> u64 {
1090 if self.iterations == 0 {
1093 self.ns_elapsed() / self.iterations.max(&1)
1097 pub fn bench_n(&mut self, n: u64, f: |&mut BenchHarness|) {
1098 self.iterations = n;
1099 debug!("running benchmark for {} iterations",
1104 // This is a more statistics-driven benchmark algorithm
1105 pub fn auto_bench(&mut self, f: |&mut BenchHarness|) -> stats::Summary {
1107 // Initial bench run to get ballpark figure.
1109 self.bench_n(n, |x| f(x));
1111 // Try to estimate iter count for 1ms falling back to 1m
1112 // iterations if first run took < 1ns.
1113 if self.ns_per_iter() == 0 {
1116 n = 1_000_000 / self.ns_per_iter().max(&1);
1118 // if the first run took more than 1ms we don't want to just
1119 // be left doing 0 iterations on every loop. The unfortunate
1120 // side effect of not being able to do as many runs is
1121 // automatically handled by the statistical analysis below
1122 // (i.e. larger error bars).
1123 if n == 0 { n = 1; }
1125 debug!("Initial run took {} ns, iter count that takes 1ms estimated as {}",
1126 self.ns_per_iter(), n);
1128 let mut total_run = 0;
1129 let samples : &mut [f64] = [0.0_f64, ..50];
1131 let loop_start = precise_time_ns();
1133 for p in samples.mut_iter() {
1134 self.bench_n(n, |x| f(x));
1135 *p = self.ns_per_iter() as f64;
1138 stats::winsorize(samples, 5.0);
1139 let summ = stats::Summary::new(samples);
1141 for p in samples.mut_iter() {
1142 self.bench_n(5 * n, |x| f(x));
1143 *p = self.ns_per_iter() as f64;
1146 stats::winsorize(samples, 5.0);
1147 let summ5 = stats::Summary::new(samples);
1149 debug!("{} samples, median {}, MAD={}, MADP={}",
1152 summ.median_abs_dev as f64,
1153 summ.median_abs_dev_pct as f64);
1155 let now = precise_time_ns();
1156 let loop_run = now - loop_start;
1158 // If we've run for 100ms and seem to have converged to a
1160 if loop_run > 100_000_000 &&
1161 summ.median_abs_dev_pct < 1.0 &&
1162 summ.median - summ5.median < summ5.median_abs_dev {
1166 total_run += loop_run;
1167 // Longest we ever run for is 3s.
1168 if total_run > 3_000_000_000 {
1182 use test::{BenchHarness, BenchSamples};
1184 pub fn benchmark(f: |&mut BenchHarness|) -> BenchSamples {
1185 let mut bs = BenchHarness {
1192 let ns_iter_summ = bs.auto_bench(f);
1194 let ns_iter = (ns_iter_summ.median as u64).max(&1);
1195 let iter_s = 1_000_000_000 / ns_iter;
1196 let mb_s = (bs.bytes * iter_s) / 1_000_000;
1199 ns_iter_summ: ns_iter_summ,
1207 use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
1208 TestDesc, TestDescAndFn,
1209 Metric, MetricMap, MetricAdded, MetricRemoved,
1210 Improvement, Regression, LikelyNoise,
1211 StaticTestName, DynTestName, DynTestFn};
1212 use test::{TestOpts, run_test};
1214 use tempfile::TempDir;
1217 pub fn do_not_run_ignored_tests() {
1219 let desc = TestDescAndFn {
1221 name: StaticTestName("whatever"),
1225 testfn: DynTestFn(proc() f()),
1227 let (p, ch) = SharedChan::new();
1228 run_test(false, desc, ch);
1229 let (_, res) = p.recv();
1230 assert!(res != TrOk);
1234 pub fn ignored_tests_result_in_ignored() {
1236 let desc = TestDescAndFn {
1238 name: StaticTestName("whatever"),
1242 testfn: DynTestFn(proc() f()),
1244 let (p, ch) = SharedChan::new();
1245 run_test(false, desc, ch);
1246 let (_, res) = p.recv();
1247 assert_eq!(res, TrIgnored);
1251 fn test_should_fail() {
1253 let desc = TestDescAndFn {
1255 name: StaticTestName("whatever"),
1259 testfn: DynTestFn(proc() f()),
1261 let (p, ch) = SharedChan::new();
1262 run_test(false, desc, ch);
1263 let (_, res) = p.recv();
1264 assert_eq!(res, TrOk);
1268 fn test_should_fail_but_succeeds() {
1270 let desc = TestDescAndFn {
1272 name: StaticTestName("whatever"),
1276 testfn: DynTestFn(proc() f()),
1278 let (p, ch) = SharedChan::new();
1279 run_test(false, desc, ch);
1280 let (_, res) = p.recv();
1281 assert_eq!(res, TrFailed);
1285 fn first_free_arg_should_be_a_filter() {
1286 let args = ~[~"progname", ~"filter"];
1287 let opts = match parse_opts(args) {
1289 _ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
1291 assert!("filter" == opts.filter.clone().unwrap());
1295 fn parse_ignored_flag() {
1296 let args = ~[~"progname", ~"filter", ~"--ignored"];
1297 let opts = match parse_opts(args) {
1299 _ => fail!("Malformed arg in parse_ignored_flag")
1301 assert!((opts.run_ignored));
1305 pub fn filter_for_ignored_option() {
1306 // When we run ignored tests the test filter should filter out all the
1307 // unignored tests and flip the ignore flag on the rest to false
1309 let opts = TestOpts {
1314 run_benchmarks: false,
1315 ratchet_noise_percent: None,
1316 ratchet_metrics: None,
1324 name: StaticTestName("1"),
1328 testfn: DynTestFn(proc() {}),
1332 name: StaticTestName("2"),
1336 testfn: DynTestFn(proc() {}),
1339 let filtered = filter_tests(&opts, tests);
1341 assert_eq!(filtered.len(), 1);
1342 assert_eq!(filtered[0].desc.name.to_str(), ~"1");
1343 assert!(filtered[0].desc.ignore == false);
1347 pub fn sort_tests() {
1348 let opts = TestOpts {
1353 run_benchmarks: false,
1354 ratchet_noise_percent: None,
1355 ratchet_metrics: None,
1361 ~[~"sha1::test", ~"int::test_to_str", ~"int::test_pow",
1362 ~"test::do_not_run_ignored_tests",
1363 ~"test::ignored_tests_result_in_ignored",
1364 ~"test::first_free_arg_should_be_a_filter",
1365 ~"test::parse_ignored_flag", ~"test::filter_for_ignored_option",
1366 ~"test::sort_tests"];
1370 let mut tests = ~[];
1371 for name in names.iter() {
1372 let test = TestDescAndFn {
1374 name: DynTestName((*name).clone()),
1378 testfn: DynTestFn(testfn),
1384 let filtered = filter_tests(&opts, tests);
1387 ~[~"int::test_pow", ~"int::test_to_str", ~"sha1::test",
1388 ~"test::do_not_run_ignored_tests",
1389 ~"test::filter_for_ignored_option",
1390 ~"test::first_free_arg_should_be_a_filter",
1391 ~"test::ignored_tests_result_in_ignored",
1392 ~"test::parse_ignored_flag",
1393 ~"test::sort_tests"];
1395 for (a, b) in expected.iter().zip(filtered.iter()) {
1396 assert!(*a == b.desc.name.to_str());
1401 pub fn test_metricmap_compare() {
1402 let mut m1 = MetricMap::new();
1403 let mut m2 = MetricMap::new();
1404 m1.insert_metric("in-both-noise", 1000.0, 200.0);
1405 m2.insert_metric("in-both-noise", 1100.0, 200.0);
1407 m1.insert_metric("in-first-noise", 1000.0, 2.0);
1408 m2.insert_metric("in-second-noise", 1000.0, 2.0);
1410 m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
1411 m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
1413 m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
1414 m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
1416 m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
1417 m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
1419 m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
1420 m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
1422 let diff1 = m2.compare_to_old(&m1, None);
1424 assert_eq!(*(diff1.find(&~"in-both-noise").unwrap()), LikelyNoise);
1425 assert_eq!(*(diff1.find(&~"in-first-noise").unwrap()), MetricRemoved);
1426 assert_eq!(*(diff1.find(&~"in-second-noise").unwrap()), MetricAdded);
1427 assert_eq!(*(diff1.find(&~"in-both-want-downwards-but-regressed").unwrap()),
1429 assert_eq!(*(diff1.find(&~"in-both-want-downwards-and-improved").unwrap()),
1431 assert_eq!(*(diff1.find(&~"in-both-want-upwards-but-regressed").unwrap()),
1433 assert_eq!(*(diff1.find(&~"in-both-want-upwards-and-improved").unwrap()),
1434 Improvement(100.0));
1435 assert_eq!(diff1.len(), 7);
1437 let diff2 = m2.compare_to_old(&m1, Some(200.0));
1439 assert_eq!(*(diff2.find(&~"in-both-noise").unwrap()), LikelyNoise);
1440 assert_eq!(*(diff2.find(&~"in-first-noise").unwrap()), MetricRemoved);
1441 assert_eq!(*(diff2.find(&~"in-second-noise").unwrap()), MetricAdded);
1442 assert_eq!(*(diff2.find(&~"in-both-want-downwards-but-regressed").unwrap()), LikelyNoise);
1443 assert_eq!(*(diff2.find(&~"in-both-want-downwards-and-improved").unwrap()), LikelyNoise);
1444 assert_eq!(*(diff2.find(&~"in-both-want-upwards-but-regressed").unwrap()), LikelyNoise);
1445 assert_eq!(*(diff2.find(&~"in-both-want-upwards-and-improved").unwrap()), LikelyNoise);
1446 assert_eq!(diff2.len(), 7);
1450 pub fn ratchet_test() {
1452 let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet");
1453 let pth = dpth.path().join("ratchet.json");
1455 let mut m1 = MetricMap::new();
1456 m1.insert_metric("runtime", 1000.0, 2.0);
1457 m1.insert_metric("throughput", 50.0, 2.0);
1459 let mut m2 = MetricMap::new();
1460 m2.insert_metric("runtime", 1100.0, 2.0);
1461 m2.insert_metric("throughput", 50.0, 2.0);
1465 // Ask for a ratchet that should fail to advance.
1466 let (diff1, ok1) = m2.ratchet(&pth, None);
1467 assert_eq!(ok1, false);
1468 assert_eq!(diff1.len(), 2);
1469 assert_eq!(*(diff1.find(&~"runtime").unwrap()), Regression(10.0));
1470 assert_eq!(*(diff1.find(&~"throughput").unwrap()), LikelyNoise);
1472 // Check that it was not rewritten.
1473 let m3 = MetricMap::load(&pth);
1474 let MetricMap(m3) = m3;
1475 assert_eq!(m3.len(), 2);
1476 assert_eq!(*(m3.find(&~"runtime").unwrap()), Metric { value: 1000.0, noise: 2.0 });
1477 assert_eq!(*(m3.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });
1479 // Ask for a ratchet with an explicit noise-percentage override,
1480 // that should advance.
1481 let (diff2, ok2) = m2.ratchet(&pth, Some(10.0));
1482 assert_eq!(ok2, true);
1483 assert_eq!(diff2.len(), 2);
1484 assert_eq!(*(diff2.find(&~"runtime").unwrap()), LikelyNoise);
1485 assert_eq!(*(diff2.find(&~"throughput").unwrap()), LikelyNoise);
1487 // Check that it was rewritten.
1488 let m4 = MetricMap::load(&pth);
1489 let MetricMap(m4) = m4;
1490 assert_eq!(m4.len(), 2);
1491 assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric { value: 1100.0, noise: 2.0 });
1492 assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });