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;
28 use time::precise_time_ns;
31 use std::clone::Clone;
32 use std::comm::{stream, SharedChan};
38 use std::to_str::ToStr;
43 // The name of a test. By convention this follows the rules for rust
44 // paths; i.e. it should be a series of identifiers separated by double
45 // colons. This way if some test runner wants to arrange the tests
46 // hierarchically it may.
50 StaticTestName(&'static str),
53 impl ToStr for TestName {
54 fn to_str(&self) -> ~str {
55 match (*self).clone() {
56 StaticTestName(s) => s.to_str(),
57 DynTestName(s) => s.to_str()
62 // A function that runs a test. If the function returns successfully,
63 // the test succeeds; if the function fails then the test fails. We
64 // may need to come up with a more clever definition of test in order
65 // to support isolation of tests into tasks.
67 StaticTestFn(extern fn()),
68 StaticBenchFn(extern fn(&mut BenchHarness)),
69 StaticMetricFn(~fn(&mut MetricMap)),
71 DynMetricFn(~fn(&mut MetricMap)),
72 DynBenchFn(~fn(&mut BenchHarness))
75 // Structure passed to BenchFns
76 pub struct BenchHarness {
83 // The definition of a single test. A test runner will run a list of
92 pub struct TestDescAndFn {
97 #[deriving(Clone, Encodable, Decodable, Eq)]
104 pub struct MetricMap(TreeMap<~str,Metric>);
106 impl Clone for MetricMap {
107 pub fn clone(&self) -> MetricMap {
108 MetricMap((**self).clone())
112 /// Analysis of a single change in metric
114 pub enum MetricChange {
122 pub type MetricDiff = TreeMap<~str,MetricChange>;
124 // The default console test runner. It accepts the command line
125 // arguments and a vector of test_descs.
126 pub fn test_main(args: &[~str], tests: ~[TestDescAndFn]) {
128 match parse_opts(args) {
129 either::Left(o) => o,
130 either::Right(m) => fail!(m)
132 if !run_tests_console(&opts, tests) { fail!("Some tests failed"); }
135 // A variant optimized for invocation with a static test vector.
136 // This will fail (intentionally) when fed any dynamic tests, because
137 // it is copying the static values out into a dynamic vector and cannot
138 // copy dynamic values. It is doing this because from this point on
139 // a ~[TestDescAndFn] is used in order to effect ownership-transfer
140 // semantics into parallel test runners, which in turn requires a ~[]
141 // rather than a &[].
142 pub fn test_main_static(args: &[~str], tests: &[TestDescAndFn]) {
143 let owned_tests = do tests.map |t| {
146 TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
149 TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
152 fail!("non-static tests passed to test::test_main_static");
156 test_main(args, owned_tests)
159 pub struct TestOpts {
160 filter: Option<~str>,
163 run_benchmarks: bool,
164 ratchet_metrics: Option<Path>,
165 ratchet_noise_percent: Option<f64>,
166 save_metrics: Option<Path>,
167 logfile: Option<Path>
170 type OptRes = Either<TestOpts, ~str>;
172 fn optgroups() -> ~[getopts::groups::OptGroup] {
173 ~[groups::optflag("", "ignored", "Run ignored tests"),
174 groups::optflag("", "test", "Run tests and not benchmarks"),
175 groups::optflag("", "bench", "Run benchmarks instead of tests"),
176 groups::optflag("h", "help", "Display this message (longer with --help)"),
177 groups::optopt("", "save-metrics", "Location to save bench metrics",
179 groups::optopt("", "ratchet-metrics",
180 "Location to load and save metrics from. The metrics \
181 loaded are cause benchmarks to fail if they run too \
183 groups::optopt("", "ratchet-noise-percent",
184 "Tests within N% of the recorded metrics will be \
185 considered as passing", "PERCENTAGE"),
186 groups::optopt("", "logfile", "Write logs to the specified file instead \
190 fn usage(binary: &str, helpstr: &str) -> ! {
191 let message = fmt!("Usage: %s [OPTIONS] [FILTER]", binary);
192 println(groups::usage(message, optgroups()));
194 if helpstr == "help" {
196 The FILTER is matched against the name of all tests to run, and if any tests
197 have a substring match, only those tests are run.
199 By default, all tests are run in parallel. This can be altered with the
200 RUST_THREADS environment variable when running tests (set it to 1).
204 #[test] - Indicates a function is a test to be run. This function
206 #[bench] - Indicates a function is a benchmark to be run. This
207 function takes one argument (extra::test::BenchHarness).
208 #[should_fail] - This function (also labeled with #[test]) will only pass if
209 the code causes a failure (an assertion failure or fail!)
210 #[ignore] - When applied to a function which is already attributed as a
211 test, then the test runner will ignore these tests during
212 normal test runs. Running with --ignored will run these
213 tests. This may also be written as #[ignore(cfg(...))] to
214 ignore the test on certain configurations.");
216 unsafe { libc::exit(0) }
219 // Parses command line arguments into test options
220 pub fn parse_opts(args: &[~str]) -> OptRes {
221 let args_ = args.tail();
223 match groups::getopts(args_, optgroups()) {
225 Err(f) => return either::Right(getopts::fail_str(f))
228 if getopts::opt_present(&matches, "h") { usage(args[0], "h"); }
229 if getopts::opt_present(&matches, "help") { usage(args[0], "help"); }
232 if matches.free.len() > 0 {
233 Some((matches).free[0].clone())
238 let run_ignored = getopts::opt_present(&matches, "ignored");
240 let logfile = getopts::opt_maybe_str(&matches, "logfile");
241 let logfile = logfile.map_move(|s| Path(s));
243 let run_benchmarks = getopts::opt_present(&matches, "bench");
244 let run_tests = ! run_benchmarks ||
245 getopts::opt_present(&matches, "test");
247 let ratchet_metrics = getopts::opt_maybe_str(&matches, "ratchet-metrics");
248 let ratchet_metrics = ratchet_metrics.map_move(|s| Path(s));
250 let ratchet_noise_percent = getopts::opt_maybe_str(&matches, "ratchet-noise-percent");
251 let ratchet_noise_percent = ratchet_noise_percent.map_move(|s| f64::from_str(s).unwrap());
253 let save_metrics = getopts::opt_maybe_str(&matches, "save-metrics");
254 let save_metrics = save_metrics.map_move(|s| Path(s));
256 let test_opts = TestOpts {
258 run_ignored: run_ignored,
259 run_tests: run_tests,
260 run_benchmarks: run_benchmarks,
261 ratchet_metrics: ratchet_metrics,
262 ratchet_noise_percent: ratchet_noise_percent,
263 save_metrics: save_metrics,
267 either::Left(test_opts)
270 #[deriving(Clone, Eq)]
271 pub struct BenchSamples {
272 ns_iter_summ: stats::Summary,
276 #[deriving(Clone, Eq)]
277 pub enum TestResult {
281 TrMetrics(MetricMap),
282 TrBench(BenchSamples),
285 struct ConsoleTestState {
287 log_out: Option<@io::Writer>,
288 term: Option<term::Terminal>,
296 failures: ~[TestDesc]
299 impl ConsoleTestState {
300 pub fn new(opts: &TestOpts) -> ConsoleTestState {
301 let log_out = match opts.logfile {
302 Some(ref path) => match io::file_writer(path,
305 result::Ok(w) => Some(w),
306 result::Err(ref s) => {
307 fail!("can't open output file: %s", *s)
312 let out = io::stdout();
313 let term = match term::Terminal::new(out) {
320 use_color: use_color(),
327 metrics: MetricMap::new(),
332 pub fn write_ok(&self) {
333 self.write_pretty("ok", term::color::GREEN);
336 pub fn write_failed(&self) {
337 self.write_pretty("FAILED", term::color::RED);
340 pub fn write_ignored(&self) {
341 self.write_pretty("ignored", term::color::YELLOW);
344 pub fn write_metric(&self) {
345 self.write_pretty("metric", term::color::CYAN);
348 pub fn write_bench(&self) {
349 self.write_pretty("bench", term::color::CYAN);
352 pub fn write_added(&self) {
353 self.write_pretty("added", term::color::GREEN);
356 pub fn write_improved(&self) {
357 self.write_pretty("improved", term::color::GREEN);
360 pub fn write_removed(&self) {
361 self.write_pretty("removed", term::color::YELLOW);
364 pub fn write_regressed(&self) {
365 self.write_pretty("regressed", term::color::RED);
368 pub fn write_pretty(&self,
370 color: term::color::Color) {
372 None => self.out.write_str(word),
377 self.out.write_str(word);
385 pub fn write_run_start(&mut self, len: uint) {
387 let noun = if len != 1 { &"tests" } else { &"test" };
388 self.out.write_line(fmt!("\nrunning %u %s", len, noun));
391 pub fn write_test_start(&self, test: &TestDesc) {
392 self.out.write_str(fmt!("test %s ... ", test.name.to_str()));
395 pub fn write_result(&self, result: &TestResult) {
397 TrOk => self.write_ok(),
398 TrFailed => self.write_failed(),
399 TrIgnored => self.write_ignored(),
400 TrMetrics(ref mm) => {
402 self.out.write_str(": " + fmt_metrics(mm));
406 self.out.write_str(": " + fmt_bench_samples(bs))
409 self.out.write_str(&"\n");
412 pub fn write_log(&self, test: &TestDesc, result: &TestResult) {
416 out.write_line(fmt!("%s %s",
419 TrFailed => ~"failed",
420 TrIgnored => ~"ignored",
421 TrMetrics(ref mm) => fmt_metrics(mm),
422 TrBench(ref bs) => fmt_bench_samples(bs)
423 }, test.name.to_str()));
428 pub fn write_failures(&self) {
429 self.out.write_line("\nfailures:");
430 let mut failures = ~[];
431 for f in self.failures.iter() {
432 failures.push(f.name.to_str());
434 sort::tim_sort(failures);
435 for name in failures.iter() {
436 self.out.write_line(fmt!(" %s", name.to_str()));
440 pub fn write_metric_diff(&self, diff: &MetricDiff) {
442 let mut improved = 0;
443 let mut regressed = 0;
447 for (k, v) in diff.iter() {
449 LikelyNoise => noise += 1,
453 self.out.write_line(fmt!(": %s", *k));
457 self.write_removed();
458 self.out.write_line(fmt!(": %s", *k));
460 Improvement(pct) => {
462 self.out.write_str(*k);
463 self.out.write_str(": ");
464 self.write_improved();
465 self.out.write_line(fmt!(" by %.2f%%", pct as float))
469 self.out.write_str(*k);
470 self.out.write_str(": ");
471 self.write_regressed();
472 self.out.write_line(fmt!(" by %.2f%%", pct as float))
476 self.out.write_line(fmt!("result of ratchet: %u matrics added, %u removed, \
477 %u improved, %u regressed, %u noise",
478 added, removed, improved, regressed, noise));
480 self.out.write_line("updated ratchet file")
482 self.out.write_line("left ratchet file untouched")
486 pub fn write_run_finish(&self,
487 ratchet_metrics: &Option<Path>,
488 ratchet_pct: Option<f64>) -> bool {
489 assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
491 let ratchet_success = match *ratchet_metrics {
494 self.out.write_str(fmt!("\nusing metrics ratchet: %s\n", pth.to_str()));
498 self.out.write_str(fmt!("with noise-tolerance forced to: %f%%\n",
501 let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
502 self.write_metric_diff(&diff);
507 let test_success = self.failed == 0u;
509 self.write_failures();
512 let success = ratchet_success && test_success;
514 self.out.write_str("\ntest result: ");
516 // There's no parallelism at this point so it's safe to use color
521 self.out.write_str(fmt!(". %u passed; %u failed; %u ignored; %u measured\n\n",
522 self.passed, self.failed, self.ignored, self.measured));
527 pub fn fmt_metrics(mm: &MetricMap) -> ~str {
528 use std::iterator::IteratorUtil;
529 let v : ~[~str] = mm.iter()
530 .transform(|(k,v)| fmt!("%s: %f (+/- %f)",
538 pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
540 fmt!("%u ns/iter (+/- %u) = %u MB/s",
541 bs.ns_iter_summ.median as uint,
542 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
545 fmt!("%u ns/iter (+/- %u)",
546 bs.ns_iter_summ.median as uint,
547 (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
551 // A simple console test runner
552 pub fn run_tests_console(opts: &TestOpts,
553 tests: ~[TestDescAndFn]) -> bool {
554 fn callback(event: &TestEvent, st: &mut ConsoleTestState) {
555 debug!("callback(event=%?)", event);
556 match (*event).clone() {
557 TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
558 TeWait(ref test) => st.write_test_start(test),
559 TeResult(test, result) => {
560 st.write_log(&test, &result);
561 st.write_result(&result);
563 TrOk => st.passed += 1,
564 TrIgnored => st.ignored += 1,
566 let tname = test.name.to_str();
567 for (k,v) in mm.iter() {
568 st.metrics.insert_metric(tname + "." + *k,
574 st.metrics.insert_metric(test.name.to_str(),
575 bs.ns_iter_summ.median,
576 bs.ns_iter_summ.max - bs.ns_iter_summ.min);
581 st.failures.push(test);
587 let st = @mut ConsoleTestState::new(opts);
588 run_tests(opts, tests, |x| callback(&x, st));
589 match opts.save_metrics {
592 st.metrics.save(pth);
593 st.out.write_str(fmt!("\nmetrics saved to: %s", pth.to_str()));
596 return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
600 fn should_sort_failures_before_printing_them() {
603 let s = do io::with_str_writer |wr| {
604 let test_a = TestDesc {
605 name: StaticTestName("a"),
610 let test_b = TestDesc {
611 name: StaticTestName("b"),
616 let st = @ConsoleTestState {
626 metrics: MetricMap::new(),
627 failures: ~[test_b, test_a]
633 let apos = s.find_str("a").unwrap();
634 let bpos = s.find_str("b").unwrap();
635 assert!(apos < bpos);
638 fn use_color() -> bool { return get_concurrency() == 1; }
642 TeFiltered(~[TestDesc]),
644 TeResult(TestDesc, TestResult),
647 type MonitorMsg = (TestDesc, TestResult);
649 fn run_tests(opts: &TestOpts,
650 tests: ~[TestDescAndFn],
651 callback: &fn(e: TestEvent)) {
653 let filtered_tests = filter_tests(opts, tests);
654 let filtered_descs = filtered_tests.map(|t| t.desc.clone());
656 callback(TeFiltered(filtered_descs));
658 let (filtered_tests, filtered_benchs_and_metrics) =
659 do filtered_tests.partition |e| {
661 StaticTestFn(_) | DynTestFn(_) => true,
666 // It's tempting to just spawn all the tests at once, but since we have
667 // many tests that run in other processes we would be making a big mess.
668 let concurrency = get_concurrency();
669 debug!("using %u test tasks", concurrency);
671 let mut remaining = filtered_tests;
675 let (p, ch) = stream();
676 let ch = SharedChan::new(ch);
678 while pending > 0 || !remaining.is_empty() {
679 while pending < concurrency && !remaining.is_empty() {
680 let test = remaining.pop();
681 if concurrency == 1 {
682 // We are doing one test at a time so we can print the name
683 // of the test before we run it. Useful for debugging tests
684 // that hang forever.
685 callback(TeWait(test.desc.clone()));
687 run_test(!opts.run_tests, test, ch.clone());
691 let (desc, result) = p.recv();
692 if concurrency != 1 {
693 callback(TeWait(desc.clone()));
695 callback(TeResult(desc, result));
699 // All benchmarks run at the end, in serial.
700 // (this includes metric fns)
701 for b in filtered_benchs_and_metrics.consume_iter() {
702 callback(TeWait(b.desc.clone()));
703 run_test(!opts.run_benchmarks, b, ch.clone());
704 let (test, result) = p.recv();
705 callback(TeResult(test, result));
709 // Windows tends to dislike being overloaded with threads.
711 static SCHED_OVERCOMMIT : uint = 1;
714 static SCHED_OVERCOMMIT : uint = 4u;
716 fn get_concurrency() -> uint {
718 let threads = rt::util::default_sched_threads();
719 if threads == 1 { 1 }
720 else { threads * SCHED_OVERCOMMIT }
725 tests: ~[TestDescAndFn]) -> ~[TestDescAndFn]
727 let mut filtered = tests;
729 // Remove tests that don't match the test filter
730 filtered = if opts.filter.is_none() {
733 let filter_str = match opts.filter {
734 Some(ref f) => (*f).clone(),
738 fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
739 Option<TestDescAndFn> {
740 if test.desc.name.to_str().contains(filter_str) {
747 filtered.consume_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
750 // Maybe pull out the ignored test and unignore them
751 filtered = if !opts.run_ignored {
754 fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
755 if test.desc.ignore {
756 let TestDescAndFn {desc, testfn} = test;
758 desc: TestDesc {ignore: false, ..desc},
765 filtered.consume_iter().filter_map(|x| filter(x)).collect()
768 // Sort the tests alphabetically
769 fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
770 t1.desc.name.to_str() < t2.desc.name.to_str()
772 sort::quick_sort(filtered, lteq);
779 wait: @fn() -> TestResult,
782 pub fn run_test(force_ignore: bool,
784 monitor_ch: SharedChan<MonitorMsg>) {
786 let TestDescAndFn {desc, testfn} = test;
788 if force_ignore || desc.ignore {
789 monitor_ch.send((desc, TrIgnored));
793 fn run_test_inner(desc: TestDesc,
794 monitor_ch: SharedChan<MonitorMsg>,
796 let testfn_cell = ::std::cell::Cell::new(testfn);
798 let mut result_future = None; // task::future_result(builder);
800 let mut task = task::task();
802 task.future_result(|r| { result_future = Some(r) });
803 task.spawn(testfn_cell.take());
805 let task_result = result_future.unwrap().recv();
806 let test_result = calc_result(&desc,
807 task_result == task::Success);
808 monitor_ch.send((desc.clone(), test_result));
813 DynBenchFn(benchfn) => {
814 let bs = ::test::bench::benchmark(benchfn);
815 monitor_ch.send((desc, TrBench(bs)));
818 StaticBenchFn(benchfn) => {
819 let bs = ::test::bench::benchmark(benchfn);
820 monitor_ch.send((desc, TrBench(bs)));
824 let mut mm = MetricMap::new();
826 monitor_ch.send((desc, TrMetrics(mm)));
829 StaticMetricFn(f) => {
830 let mut mm = MetricMap::new();
832 monitor_ch.send((desc, TrMetrics(mm)));
835 DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
836 StaticTestFn(f) => run_test_inner(desc, monitor_ch, || f())
840 fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
842 if desc.should_fail { TrFailed }
845 if desc.should_fail { TrOk }
851 impl ToJson for Metric {
852 fn to_json(&self) -> json::Json {
853 let mut map = ~TreeMap::new();
854 map.insert(~"value", json::Number(self.value as float));
855 map.insert(~"noise", json::Number(self.noise as float));
862 pub fn new() -> MetricMap {
863 MetricMap(TreeMap::new())
866 /// Load MetricDiff from a file.
867 pub fn load(p: &Path) -> MetricMap {
868 assert!(os::path_exists(p));
869 let f = io::file_reader(p).unwrap();
870 let mut decoder = json::Decoder(json::from_reader(f).unwrap());
871 MetricMap(Decodable::decode(&mut decoder))
874 /// Write MetricDiff to a file.
875 pub fn save(&self, p: &Path) {
876 let f = io::file_writer(p, [io::Create, io::Truncate]).unwrap();
877 json::to_pretty_writer(f, &self.to_json());
880 /// Compare against another MetricMap. Optionally compare all
881 /// measurements in the maps using the provided `noise_pct` as a
882 /// percentage of each value to consider noise. If `None`, each
883 /// measurement's noise threshold is independently chosen as the
884 /// maximum of that measurement's recorded noise quantity in either
886 pub fn compare_to_old(&self, old: &MetricMap,
887 noise_pct: Option<f64>) -> MetricDiff {
888 let mut diff : MetricDiff = TreeMap::new();
889 for (k, vold) in old.iter() {
890 let r = match self.find(k) {
891 None => MetricRemoved,
893 let delta = (v.value - vold.value);
894 let noise = match noise_pct {
895 None => f64::max(vold.noise.abs(), v.noise.abs()),
896 Some(pct) => vold.value * pct / 100.0
898 if delta.abs() <= noise {
901 let pct = delta.abs() / (vold.value).max(&f64::epsilon) * 100.0;
902 if vold.noise < 0.0 {
903 // When 'noise' is negative, it means we want
904 // to see deltas that go up over time, and can
905 // only tolerate slight negative movement.
912 // When 'noise' is positive, it means we want
913 // to see deltas that go down over time, and
914 // can only tolerate slight positive movements.
924 diff.insert((*k).clone(), r);
926 for (k, _) in self.iter() {
927 if !diff.contains_key(k) {
928 diff.insert((*k).clone(), MetricAdded);
934 /// Insert a named `value` (+/- `noise`) metric into the map. The value
935 /// must be non-negative. The `noise` indicates the uncertainty of the
936 /// metric, which doubles as the "noise range" of acceptable
937 /// pairwise-regressions on this named value, when comparing from one
938 /// metric to the next using `compare_to_old`.
940 /// If `noise` is positive, then it means this metric is of a value
941 /// you want to see grow smaller, so a change larger than `noise` in the
942 /// positive direction represents a regression.
944 /// If `noise` is negative, then it means this metric is of a value
945 /// you want to see grow larger, so a change larger than `noise` in the
946 /// negative direction represents a regression.
947 pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
952 self.insert(name.to_owned(), m);
955 /// Attempt to "ratchet" an external metric file. This involves loading
956 /// metrics from a metric file (if it exists), comparing against
957 /// the metrics in `self` using `compare_to_old`, and rewriting the
958 /// file to contain the metrics in `self` if none of the
959 /// `MetricChange`s are `Regression`. Returns the diff as well
960 /// as a boolean indicating whether the ratchet succeeded.
961 pub fn ratchet(&self, p: &Path, pct: Option<f64>) -> (MetricDiff, bool) {
962 let old = if os::path_exists(p) {
968 let diff : MetricDiff = self.compare_to_old(&old, pct);
969 let ok = do diff.iter().all() |(_, v)| {
971 Regression(_) => false,
977 debug!("rewriting file '%s' with updated metrics");
988 /// Callback for benchmark functions to run in their body.
989 pub fn iter(&mut self, inner:&fn()) {
990 self.ns_start = precise_time_ns();
991 let k = self.iterations;
992 for _ in range(0u64, k) {
995 self.ns_end = precise_time_ns();
998 pub fn ns_elapsed(&mut self) -> u64 {
999 if self.ns_start == 0 || self.ns_end == 0 {
1002 self.ns_end - self.ns_start
1006 pub fn ns_per_iter(&mut self) -> u64 {
1007 if self.iterations == 0 {
1010 self.ns_elapsed() / self.iterations.max(&1)
1014 pub fn bench_n(&mut self, n: u64, f: &fn(&mut BenchHarness)) {
1015 self.iterations = n;
1016 debug!("running benchmark for %u iterations",
1021 // This is a more statistics-driven benchmark algorithm
1022 pub fn auto_bench(&mut self, f: &fn(&mut BenchHarness)) -> stats::Summary {
1024 // Initial bench run to get ballpark figure.
1026 self.bench_n(n, |x| f(x));
1028 // Try to estimate iter count for 1ms falling back to 1m
1029 // iterations if first run took < 1ns.
1030 if self.ns_per_iter() == 0 {
1033 n = 1_000_000 / self.ns_per_iter().max(&1);
1036 let mut total_run = 0;
1037 let samples : &mut [f64] = [0.0_f64, ..50];
1039 let loop_start = precise_time_ns();
1041 for p in samples.mut_iter() {
1042 self.bench_n(n as u64, |x| f(x));
1043 *p = self.ns_per_iter() as f64;
1046 stats::winsorize(samples, 5.0);
1047 let summ = stats::Summary::new(samples);
1049 for p in samples.mut_iter() {
1050 self.bench_n(5 * n as u64, |x| f(x));
1051 *p = self.ns_per_iter() as f64;
1054 stats::winsorize(samples, 5.0);
1055 let summ5 = stats::Summary::new(samples);
1057 debug!("%u samples, median %f, MAD=%f, MADP=%f",
1059 summ.median as float,
1060 summ.median_abs_dev as float,
1061 summ.median_abs_dev_pct as float);
1063 let now = precise_time_ns();
1064 let loop_run = now - loop_start;
1066 // If we've run for 100ms an seem to have converged to a
1068 if loop_run > 100_000_000 &&
1069 summ.median_abs_dev_pct < 1.0 &&
1070 summ.median - summ5.median < summ5.median_abs_dev {
1074 total_run += loop_run;
1075 // Longest we ever run for is 3s.
1076 if total_run > 3_000_000_000 {
1090 use test::{BenchHarness, BenchSamples};
1092 pub fn benchmark(f: &fn(&mut BenchHarness)) -> BenchSamples {
1094 let mut bs = BenchHarness {
1101 let ns_iter_summ = bs.auto_bench(f);
1103 let ns_iter = (ns_iter_summ.median as u64).max(&1);
1104 let iter_s = 1_000_000_000 / ns_iter;
1105 let mb_s = (bs.bytes * iter_s) / 1_000_000;
1108 ns_iter_summ: ns_iter_summ,
1116 use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
1117 TestDesc, TestDescAndFn,
1118 Metric, MetricMap, MetricAdded, MetricRemoved,
1119 Improvement, Regression, LikelyNoise,
1120 StaticTestName, DynTestName, DynTestFn};
1121 use test::{TestOpts, run_test};
1124 use std::comm::{stream, SharedChan};
1130 pub fn do_not_run_ignored_tests() {
1132 let desc = TestDescAndFn {
1134 name: StaticTestName("whatever"),
1138 testfn: DynTestFn(|| f()),
1140 let (p, ch) = stream();
1141 let ch = SharedChan::new(ch);
1142 run_test(false, desc, ch);
1143 let (_, res) = p.recv();
1144 assert!(res != TrOk);
1148 pub fn ignored_tests_result_in_ignored() {
1150 let desc = TestDescAndFn {
1152 name: StaticTestName("whatever"),
1156 testfn: DynTestFn(|| f()),
1158 let (p, ch) = stream();
1159 let ch = SharedChan::new(ch);
1160 run_test(false, desc, ch);
1161 let (_, res) = p.recv();
1162 assert_eq!(res, TrIgnored);
1166 #[ignore(cfg(windows))]
1167 fn test_should_fail() {
1169 let desc = TestDescAndFn {
1171 name: StaticTestName("whatever"),
1175 testfn: DynTestFn(|| f()),
1177 let (p, ch) = stream();
1178 let ch = SharedChan::new(ch);
1179 run_test(false, desc, ch);
1180 let (_, res) = p.recv();
1181 assert_eq!(res, TrOk);
1185 fn test_should_fail_but_succeeds() {
1187 let desc = TestDescAndFn {
1189 name: StaticTestName("whatever"),
1193 testfn: DynTestFn(|| f()),
1195 let (p, ch) = stream();
1196 let ch = SharedChan::new(ch);
1197 run_test(false, desc, ch);
1198 let (_, res) = p.recv();
1199 assert_eq!(res, TrFailed);
1203 fn first_free_arg_should_be_a_filter() {
1204 let args = ~[~"progname", ~"filter"];
1205 let opts = match parse_opts(args) {
1206 either::Left(o) => o,
1207 _ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
1209 assert!("filter" == opts.filter.clone().unwrap());
1213 fn parse_ignored_flag() {
1214 let args = ~[~"progname", ~"filter", ~"--ignored"];
1215 let opts = match parse_opts(args) {
1216 either::Left(o) => o,
1217 _ => fail!("Malformed arg in parse_ignored_flag")
1219 assert!((opts.run_ignored));
1223 pub fn filter_for_ignored_option() {
1226 // When we run ignored tests the test filter should filter out all the
1227 // unignored tests and flip the ignore flag on the rest to false
1229 let opts = TestOpts {
1234 run_benchmarks: false,
1235 ratchet_noise_percent: None,
1236 ratchet_metrics: None,
1243 name: StaticTestName("1"),
1247 testfn: DynTestFn(|| {}),
1251 name: StaticTestName("2"),
1255 testfn: DynTestFn(|| {}),
1258 let filtered = filter_tests(&opts, tests);
1260 assert_eq!(filtered.len(), 1);
1261 assert_eq!(filtered[0].desc.name.to_str(), ~"1");
1262 assert!(filtered[0].desc.ignore == false);
1266 pub fn sort_tests() {
1267 let opts = TestOpts {
1272 run_benchmarks: false,
1273 ratchet_noise_percent: None,
1274 ratchet_metrics: None,
1279 ~[~"sha1::test", ~"int::test_to_str", ~"int::test_pow",
1280 ~"test::do_not_run_ignored_tests",
1281 ~"test::ignored_tests_result_in_ignored",
1282 ~"test::first_free_arg_should_be_a_filter",
1283 ~"test::parse_ignored_flag", ~"test::filter_for_ignored_option",
1284 ~"test::sort_tests"];
1288 let mut tests = ~[];
1289 for name in names.iter() {
1290 let test = TestDescAndFn {
1292 name: DynTestName((*name).clone()),
1296 testfn: DynTestFn(testfn),
1302 let filtered = filter_tests(&opts, tests);
1305 ~[~"int::test_pow", ~"int::test_to_str", ~"sha1::test",
1306 ~"test::do_not_run_ignored_tests",
1307 ~"test::filter_for_ignored_option",
1308 ~"test::first_free_arg_should_be_a_filter",
1309 ~"test::ignored_tests_result_in_ignored",
1310 ~"test::parse_ignored_flag",
1311 ~"test::sort_tests"];
1313 let pairs = vec::zip(expected, filtered);
1315 for p in pairs.iter() {
1318 assert!(*a == b.desc.name.to_str());
1325 pub fn test_metricmap_compare() {
1326 let mut m1 = MetricMap::new();
1327 let mut m2 = MetricMap::new();
1328 m1.insert_metric("in-both-noise", 1000.0, 200.0);
1329 m2.insert_metric("in-both-noise", 1100.0, 200.0);
1331 m1.insert_metric("in-first-noise", 1000.0, 2.0);
1332 m2.insert_metric("in-second-noise", 1000.0, 2.0);
1334 m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
1335 m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
1337 m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
1338 m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
1340 m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
1341 m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
1343 m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
1344 m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
1346 let diff1 = m2.compare_to_old(&m1, None);
1348 assert_eq!(*(diff1.find(&~"in-both-noise").unwrap()), LikelyNoise);
1349 assert_eq!(*(diff1.find(&~"in-first-noise").unwrap()), MetricRemoved);
1350 assert_eq!(*(diff1.find(&~"in-second-noise").unwrap()), MetricAdded);
1351 assert_eq!(*(diff1.find(&~"in-both-want-downwards-but-regressed").unwrap()),
1353 assert_eq!(*(diff1.find(&~"in-both-want-downwards-and-improved").unwrap()),
1355 assert_eq!(*(diff1.find(&~"in-both-want-upwards-but-regressed").unwrap()),
1357 assert_eq!(*(diff1.find(&~"in-both-want-upwards-and-improved").unwrap()),
1358 Improvement(100.0));
1359 assert_eq!(diff1.len(), 7);
1361 let diff2 = m2.compare_to_old(&m1, Some(200.0));
1363 assert_eq!(*(diff2.find(&~"in-both-noise").unwrap()), LikelyNoise);
1364 assert_eq!(*(diff2.find(&~"in-first-noise").unwrap()), MetricRemoved);
1365 assert_eq!(*(diff2.find(&~"in-second-noise").unwrap()), MetricAdded);
1366 assert_eq!(*(diff2.find(&~"in-both-want-downwards-but-regressed").unwrap()), LikelyNoise);
1367 assert_eq!(*(diff2.find(&~"in-both-want-downwards-and-improved").unwrap()), LikelyNoise);
1368 assert_eq!(*(diff2.find(&~"in-both-want-upwards-but-regressed").unwrap()), LikelyNoise);
1369 assert_eq!(*(diff2.find(&~"in-both-want-upwards-and-improved").unwrap()), LikelyNoise);
1370 assert_eq!(diff2.len(), 7);
1373 pub fn ratchet_test() {
1375 let dpth = tempfile::mkdtemp(&os::tmpdir(),
1376 "test-ratchet").expect("missing test for ratchet");
1377 let pth = dpth.push("ratchet.json");
1379 let mut m1 = MetricMap::new();
1380 m1.insert_metric("runtime", 1000.0, 2.0);
1381 m1.insert_metric("throughput", 50.0, 2.0);
1383 let mut m2 = MetricMap::new();
1384 m2.insert_metric("runtime", 1100.0, 2.0);
1385 m2.insert_metric("throughput", 50.0, 2.0);
1389 // Ask for a ratchet that should fail to advance.
1390 let (diff1, ok1) = m2.ratchet(&pth, None);
1391 assert_eq!(ok1, false);
1392 assert_eq!(diff1.len(), 2);
1393 assert_eq!(*(diff1.find(&~"runtime").unwrap()), Regression(10.0));
1394 assert_eq!(*(diff1.find(&~"throughput").unwrap()), LikelyNoise);
1396 // Check that it was not rewritten.
1397 let m3 = MetricMap::load(&pth);
1398 assert_eq!(m3.len(), 2);
1399 assert_eq!(*(m3.find(&~"runtime").unwrap()), Metric { value: 1000.0, noise: 2.0 });
1400 assert_eq!(*(m3.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });
1402 // Ask for a ratchet with an explicit noise-percentage override,
1403 // that should advance.
1404 let (diff2, ok2) = m2.ratchet(&pth, Some(10.0));
1405 assert_eq!(ok2, true);
1406 assert_eq!(diff2.len(), 2);
1407 assert_eq!(*(diff2.find(&~"runtime").unwrap()), LikelyNoise);
1408 assert_eq!(*(diff2.find(&~"throughput").unwrap()), LikelyNoise);
1410 // Check that it was rewritten.
1411 let m4 = MetricMap::load(&pth);
1412 assert_eq!(m4.len(), 2);
1413 assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric { value: 1100.0, noise: 2.0 });
1414 assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });
1416 os::remove_dir_recursive(&dpth);