]> git.lizzy.rs Git - rust.git/blob - src/libextra/test.rs
auto merge of #8350 : dim-an/rust/fix-struct-match, r=pcwalton
[rust.git] / src / libextra / test.rs
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.
4 //
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.
10
11 #[doc(hidden)];
12
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.
17
18
19 use getopts;
20 use getopts::groups;
21 use json::ToJson;
22 use json;
23 use serialize::Decodable;
24 use sort;
25 use stats::Stats;
26 use stats;
27 use term;
28 use time::precise_time_ns;
29 use treemap::TreeMap;
30
31 use std::clone::Clone;
32 use std::comm::{stream, SharedChan};
33 use std::libc;
34 use std::either;
35 use std::io;
36 use std::result;
37 use std::task;
38 use std::to_str::ToStr;
39 use std::f64;
40 use std::os;
41
42
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.
47
48 #[deriving(Clone)]
49 pub enum TestName {
50     StaticTestName(&'static str),
51     DynTestName(~str)
52 }
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()
58         }
59     }
60 }
61
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.
66 pub enum TestFn {
67     StaticTestFn(extern fn()),
68     StaticBenchFn(extern fn(&mut BenchHarness)),
69     StaticMetricFn(~fn(&mut MetricMap)),
70     DynTestFn(~fn()),
71     DynMetricFn(~fn(&mut MetricMap)),
72     DynBenchFn(~fn(&mut BenchHarness))
73 }
74
75 // Structure passed to BenchFns
76 pub struct BenchHarness {
77     iterations: u64,
78     ns_start: u64,
79     ns_end: u64,
80     bytes: u64
81 }
82
83 // The definition of a single test. A test runner will run a list of
84 // these.
85 #[deriving(Clone)]
86 pub struct TestDesc {
87     name: TestName,
88     ignore: bool,
89     should_fail: bool
90 }
91
92 pub struct TestDescAndFn {
93     desc: TestDesc,
94     testfn: TestFn,
95 }
96
97 #[deriving(Clone, Encodable, Decodable, Eq)]
98 pub struct Metric {
99     value: f64,
100     noise: f64
101 }
102
103 #[deriving(Eq)]
104 pub struct MetricMap(TreeMap<~str,Metric>);
105
106 impl Clone for MetricMap {
107     pub fn clone(&self) -> MetricMap {
108         MetricMap((**self).clone())
109     }
110 }
111
112 /// Analysis of a single change in metric
113 #[deriving(Eq)]
114 pub enum MetricChange {
115     LikelyNoise,
116     MetricAdded,
117     MetricRemoved,
118     Improvement(f64),
119     Regression(f64)
120 }
121
122 pub type MetricDiff = TreeMap<~str,MetricChange>;
123
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]) {
127     let opts =
128         match parse_opts(args) {
129           either::Left(o) => o,
130           either::Right(m) => fail!(m)
131         };
132     if !run_tests_console(&opts, tests) { fail!("Some tests failed"); }
133 }
134
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| {
144         match t.testfn {
145             StaticTestFn(f) =>
146             TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
147
148             StaticBenchFn(f) =>
149             TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
150
151             _ => {
152                 fail!("non-static tests passed to test::test_main_static");
153             }
154         }
155     };
156     test_main(args, owned_tests)
157 }
158
159 pub struct TestOpts {
160     filter: Option<~str>,
161     run_ignored: bool,
162     run_tests: bool,
163     run_benchmarks: bool,
164     ratchet_metrics: Option<Path>,
165     ratchet_noise_percent: Option<f64>,
166     save_metrics: Option<Path>,
167     logfile: Option<Path>
168 }
169
170 type OptRes = Either<TestOpts, ~str>;
171
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",
178                      "PATH"),
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 \
182                       slowly", "PATH"),
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 \
187                           of stdout", "PATH")]
188 }
189
190 fn usage(binary: &str, helpstr: &str) -> ! {
191     let message = fmt!("Usage: %s [OPTIONS] [FILTER]", binary);
192     println(groups::usage(message, optgroups()));
193     println("");
194     if helpstr == "help" {
195         println("\
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.
198
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).
201
202 Test Attributes:
203
204     #[test]        - Indicates a function is a test to be run. This function
205                      takes no arguments.
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.");
215     }
216     unsafe { libc::exit(0) }
217 }
218
219 // Parses command line arguments into test options
220 pub fn parse_opts(args: &[~str]) -> OptRes {
221     let args_ = args.tail();
222     let matches =
223         match groups::getopts(args_, optgroups()) {
224           Ok(m) => m,
225           Err(f) => return either::Right(getopts::fail_str(f))
226         };
227
228     if getopts::opt_present(&matches, "h") { usage(args[0], "h"); }
229     if getopts::opt_present(&matches, "help") { usage(args[0], "help"); }
230
231     let filter =
232         if matches.free.len() > 0 {
233             Some((matches).free[0].clone())
234         } else {
235             None
236         };
237
238     let run_ignored = getopts::opt_present(&matches, "ignored");
239
240     let logfile = getopts::opt_maybe_str(&matches, "logfile");
241     let logfile = logfile.map_move(|s| Path(s));
242
243     let run_benchmarks = getopts::opt_present(&matches, "bench");
244     let run_tests = ! run_benchmarks ||
245         getopts::opt_present(&matches, "test");
246
247     let ratchet_metrics = getopts::opt_maybe_str(&matches, "ratchet-metrics");
248     let ratchet_metrics = ratchet_metrics.map_move(|s| Path(s));
249
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());
252
253     let save_metrics = getopts::opt_maybe_str(&matches, "save-metrics");
254     let save_metrics = save_metrics.map_move(|s| Path(s));
255
256     let test_opts = TestOpts {
257         filter: filter,
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,
264         logfile: logfile
265     };
266
267     either::Left(test_opts)
268 }
269
270 #[deriving(Clone, Eq)]
271 pub struct BenchSamples {
272     ns_iter_summ: stats::Summary,
273     mb_s: uint
274 }
275
276 #[deriving(Clone, Eq)]
277 pub enum TestResult {
278     TrOk,
279     TrFailed,
280     TrIgnored,
281     TrMetrics(MetricMap),
282     TrBench(BenchSamples),
283 }
284
285 struct ConsoleTestState {
286     out: @io::Writer,
287     log_out: Option<@io::Writer>,
288     term: Option<term::Terminal>,
289     use_color: bool,
290     total: uint,
291     passed: uint,
292     failed: uint,
293     ignored: uint,
294     measured: uint,
295     metrics: MetricMap,
296     failures: ~[TestDesc]
297 }
298
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,
303                                                     [io::Create,
304                                                      io::Truncate]) {
305                 result::Ok(w) => Some(w),
306                 result::Err(ref s) => {
307                     fail!("can't open output file: %s", *s)
308                 }
309             },
310             None => None
311         };
312         let out = io::stdout();
313         let term = match term::Terminal::new(out) {
314             Err(_) => None,
315             Ok(t) => Some(t)
316         };
317         ConsoleTestState {
318             out: out,
319             log_out: log_out,
320             use_color: use_color(),
321             term: term,
322             total: 0u,
323             passed: 0u,
324             failed: 0u,
325             ignored: 0u,
326             measured: 0u,
327             metrics: MetricMap::new(),
328             failures: ~[]
329         }
330     }
331
332     pub fn write_ok(&self) {
333         self.write_pretty("ok", term::color::GREEN);
334     }
335
336     pub fn write_failed(&self) {
337         self.write_pretty("FAILED", term::color::RED);
338     }
339
340     pub fn write_ignored(&self) {
341         self.write_pretty("ignored", term::color::YELLOW);
342     }
343
344     pub fn write_metric(&self) {
345         self.write_pretty("metric", term::color::CYAN);
346     }
347
348     pub fn write_bench(&self) {
349         self.write_pretty("bench", term::color::CYAN);
350     }
351
352     pub fn write_added(&self) {
353         self.write_pretty("added", term::color::GREEN);
354     }
355
356     pub fn write_improved(&self) {
357         self.write_pretty("improved", term::color::GREEN);
358     }
359
360     pub fn write_removed(&self) {
361         self.write_pretty("removed", term::color::YELLOW);
362     }
363
364     pub fn write_regressed(&self) {
365         self.write_pretty("regressed", term::color::RED);
366     }
367
368     pub fn write_pretty(&self,
369                         word: &str,
370                         color: term::color::Color) {
371         match self.term {
372             None => self.out.write_str(word),
373             Some(ref t) => {
374                 if self.use_color {
375                     t.fg(color);
376                 }
377                 self.out.write_str(word);
378                 if self.use_color {
379                     t.reset();
380                 }
381             }
382         }
383     }
384
385     pub fn write_run_start(&mut self, len: uint) {
386         self.total = len;
387         let noun = if len != 1 { &"tests" } else { &"test" };
388         self.out.write_line(fmt!("\nrunning %u %s", len, noun));
389     }
390
391     pub fn write_test_start(&self, test: &TestDesc) {
392         self.out.write_str(fmt!("test %s ... ", test.name.to_str()));
393     }
394
395     pub fn write_result(&self, result: &TestResult) {
396         match *result {
397             TrOk => self.write_ok(),
398             TrFailed => self.write_failed(),
399             TrIgnored => self.write_ignored(),
400             TrMetrics(ref mm) => {
401                 self.write_metric();
402                 self.out.write_str(": " + fmt_metrics(mm));
403             }
404             TrBench(ref bs) => {
405                 self.write_bench();
406                 self.out.write_str(": " + fmt_bench_samples(bs))
407             }
408         }
409         self.out.write_str(&"\n");
410     }
411
412     pub fn write_log(&self, test: &TestDesc, result: &TestResult) {
413         match self.log_out {
414             None => (),
415             Some(out) => {
416                 out.write_line(fmt!("%s %s",
417                                     match *result {
418                                         TrOk => ~"ok",
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()));
424             }
425         }
426     }
427
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());
433         }
434         sort::tim_sort(failures);
435         for name in failures.iter() {
436             self.out.write_line(fmt!("    %s", name.to_str()));
437         }
438     }
439
440     pub fn write_metric_diff(&self, diff: &MetricDiff) {
441         let mut noise = 0;
442         let mut improved = 0;
443         let mut regressed = 0;
444         let mut added = 0;
445         let mut removed = 0;
446
447         for (k, v) in diff.iter() {
448             match *v {
449                 LikelyNoise => noise += 1,
450                 MetricAdded => {
451                     added += 1;
452                     self.write_added();
453                     self.out.write_line(fmt!(": %s", *k));
454                 }
455                 MetricRemoved => {
456                     removed += 1;
457                     self.write_removed();
458                     self.out.write_line(fmt!(": %s", *k));
459                 }
460                 Improvement(pct) => {
461                     improved += 1;
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))
466                 }
467                 Regression(pct) => {
468                     regressed += 1;
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))
473                 }
474             }
475         }
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));
479         if regressed == 0 {
480             self.out.write_line("updated ratchet file")
481         } else {
482             self.out.write_line("left ratchet file untouched")
483         }
484     }
485
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);
490
491         let ratchet_success = match *ratchet_metrics {
492             None => true,
493             Some(ref pth) => {
494                 self.out.write_str(fmt!("\nusing metrics ratchet: %s\n", pth.to_str()));
495                 match ratchet_pct {
496                     None => (),
497                     Some(pct) =>
498                     self.out.write_str(fmt!("with noise-tolerance forced to: %f%%\n",
499                                             pct as float))
500                 }
501                 let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
502                 self.write_metric_diff(&diff);
503                 ok
504             }
505         };
506
507         let test_success = self.failed == 0u;
508         if !test_success {
509             self.write_failures();
510         }
511
512         let success = ratchet_success && test_success;
513
514         self.out.write_str("\ntest result: ");
515         if success {
516             // There's no parallelism at this point so it's safe to use color
517             self.write_ok();
518         } else {
519             self.write_failed();
520         }
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));
523         return success;
524     }
525 }
526
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)",
531                                 *k,
532                                 v.value as float,
533                                 v.noise as float))
534         .collect();
535     v.connect(", ")
536 }
537
538 pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
539     if bs.mb_s != 0 {
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,
543              bs.mb_s)
544     } else {
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)
548     }
549 }
550
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);
562                 match result {
563                     TrOk => st.passed += 1,
564                     TrIgnored => st.ignored += 1,
565                     TrMetrics(mm) => {
566                         let tname = test.name.to_str();
567                         for (k,v) in mm.iter() {
568                             st.metrics.insert_metric(tname + "." + *k,
569                                                      v.value, v.noise);
570                         }
571                         st.measured += 1
572                     }
573                     TrBench(bs) => {
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);
577                         st.measured += 1
578                     }
579                     TrFailed => {
580                         st.failed += 1;
581                         st.failures.push(test);
582                     }
583                 }
584             }
585         }
586     }
587     let st = @mut ConsoleTestState::new(opts);
588     run_tests(opts, tests, |x| callback(&x, st));
589     match opts.save_metrics {
590         None => (),
591         Some(ref pth) => {
592             st.metrics.save(pth);
593             st.out.write_str(fmt!("\nmetrics saved to: %s", pth.to_str()));
594         }
595     }
596     return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
597 }
598
599 #[test]
600 fn should_sort_failures_before_printing_them() {
601     fn dummy() {}
602
603     let s = do io::with_str_writer |wr| {
604         let test_a = TestDesc {
605             name: StaticTestName("a"),
606             ignore: false,
607             should_fail: false
608         };
609
610         let test_b = TestDesc {
611             name: StaticTestName("b"),
612             ignore: false,
613             should_fail: false
614         };
615
616         let st = @ConsoleTestState {
617             out: wr,
618             log_out: None,
619             term: None,
620             use_color: false,
621             total: 0u,
622             passed: 0u,
623             failed: 0u,
624             ignored: 0u,
625             measured: 0u,
626             metrics: MetricMap::new(),
627             failures: ~[test_b, test_a]
628         };
629
630         st.write_failures();
631     };
632
633     let apos = s.find_str("a").unwrap();
634     let bpos = s.find_str("b").unwrap();
635     assert!(apos < bpos);
636 }
637
638 fn use_color() -> bool { return get_concurrency() == 1; }
639
640 #[deriving(Clone)]
641 enum TestEvent {
642     TeFiltered(~[TestDesc]),
643     TeWait(TestDesc),
644     TeResult(TestDesc, TestResult),
645 }
646
647 type MonitorMsg = (TestDesc, TestResult);
648
649 fn run_tests(opts: &TestOpts,
650              tests: ~[TestDescAndFn],
651              callback: &fn(e: TestEvent)) {
652
653     let filtered_tests = filter_tests(opts, tests);
654     let filtered_descs = filtered_tests.map(|t| t.desc.clone());
655
656     callback(TeFiltered(filtered_descs));
657
658     let (filtered_tests, filtered_benchs_and_metrics) =
659         do filtered_tests.partition |e| {
660         match e.testfn {
661             StaticTestFn(_) | DynTestFn(_) => true,
662             _ => false
663         }
664     };
665
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);
670
671     let mut remaining = filtered_tests;
672     remaining.reverse();
673     let mut pending = 0;
674
675     let (p, ch) = stream();
676     let ch = SharedChan::new(ch);
677
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()));
686             }
687             run_test(!opts.run_tests, test, ch.clone());
688             pending += 1;
689         }
690
691         let (desc, result) = p.recv();
692         if concurrency != 1 {
693             callback(TeWait(desc.clone()));
694         }
695         callback(TeResult(desc, result));
696         pending -= 1;
697     }
698
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));
706     }
707 }
708
709 // Windows tends to dislike being overloaded with threads.
710 #[cfg(windows)]
711 static SCHED_OVERCOMMIT : uint = 1;
712
713 #[cfg(unix)]
714 static SCHED_OVERCOMMIT : uint = 4u;
715
716 fn get_concurrency() -> uint {
717     use std::rt;
718     let threads = rt::util::default_sched_threads();
719     if threads == 1 { 1 }
720     else { threads * SCHED_OVERCOMMIT }
721 }
722
723 pub fn filter_tests(
724     opts: &TestOpts,
725     tests: ~[TestDescAndFn]) -> ~[TestDescAndFn]
726 {
727     let mut filtered = tests;
728
729     // Remove tests that don't match the test filter
730     filtered = if opts.filter.is_none() {
731         filtered
732     } else {
733         let filter_str = match opts.filter {
734           Some(ref f) => (*f).clone(),
735           None => ~""
736         };
737
738         fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
739             Option<TestDescAndFn> {
740             if test.desc.name.to_str().contains(filter_str) {
741                 return Some(test);
742             } else {
743                 return None;
744             }
745         }
746
747         filtered.consume_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
748     };
749
750     // Maybe pull out the ignored test and unignore them
751     filtered = if !opts.run_ignored {
752         filtered
753     } else {
754         fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
755             if test.desc.ignore {
756                 let TestDescAndFn {desc, testfn} = test;
757                 Some(TestDescAndFn {
758                     desc: TestDesc {ignore: false, ..desc},
759                     testfn: testfn
760                 })
761             } else {
762                 None
763             }
764         };
765         filtered.consume_iter().filter_map(|x| filter(x)).collect()
766     };
767
768     // Sort the tests alphabetically
769     fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
770         t1.desc.name.to_str() < t2.desc.name.to_str()
771     }
772     sort::quick_sort(filtered, lteq);
773
774     filtered
775 }
776
777 struct TestFuture {
778     test: TestDesc,
779     wait: @fn() -> TestResult,
780 }
781
782 pub fn run_test(force_ignore: bool,
783                 test: TestDescAndFn,
784                 monitor_ch: SharedChan<MonitorMsg>) {
785
786     let TestDescAndFn {desc, testfn} = test;
787
788     if force_ignore || desc.ignore {
789         monitor_ch.send((desc, TrIgnored));
790         return;
791     }
792
793     fn run_test_inner(desc: TestDesc,
794                       monitor_ch: SharedChan<MonitorMsg>,
795                       testfn: ~fn()) {
796         let testfn_cell = ::std::cell::Cell::new(testfn);
797         do task::spawn {
798             let mut result_future = None; // task::future_result(builder);
799
800             let mut task = task::task();
801             task.unlinked();
802             task.future_result(|r| { result_future = Some(r) });
803             task.spawn(testfn_cell.take());
804
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));
809         }
810     }
811
812     match testfn {
813         DynBenchFn(benchfn) => {
814             let bs = ::test::bench::benchmark(benchfn);
815             monitor_ch.send((desc, TrBench(bs)));
816             return;
817         }
818         StaticBenchFn(benchfn) => {
819             let bs = ::test::bench::benchmark(benchfn);
820             monitor_ch.send((desc, TrBench(bs)));
821             return;
822         }
823         DynMetricFn(f) => {
824             let mut mm = MetricMap::new();
825             f(&mut mm);
826             monitor_ch.send((desc, TrMetrics(mm)));
827             return;
828         }
829         StaticMetricFn(f) => {
830             let mut mm = MetricMap::new();
831             f(&mut mm);
832             monitor_ch.send((desc, TrMetrics(mm)));
833             return;
834         }
835         DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
836         StaticTestFn(f) => run_test_inner(desc, monitor_ch, || f())
837     }
838 }
839
840 fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
841     if task_succeeded {
842         if desc.should_fail { TrFailed }
843         else { TrOk }
844     } else {
845         if desc.should_fail { TrOk }
846         else { TrFailed }
847     }
848 }
849
850
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));
856         json::Object(map)
857     }
858 }
859
860 impl MetricMap {
861
862     pub fn new() -> MetricMap {
863         MetricMap(TreeMap::new())
864     }
865
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))
872     }
873
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());
878     }
879
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
885     /// map.
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,
892                 Some(v) => {
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
897                     };
898                     if delta.abs() <= noise {
899                         LikelyNoise
900                     } else {
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.
906                             if delta < 0.0 {
907                                 Regression(pct)
908                             } else {
909                                 Improvement(pct)
910                             }
911                         } else {
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.
915                             if delta < 0.0 {
916                                 Improvement(pct)
917                             } else {
918                                 Regression(pct)
919                             }
920                         }
921                     }
922                 }
923             };
924             diff.insert((*k).clone(), r);
925         }
926         for (k, _) in self.iter() {
927             if !diff.contains_key(k) {
928                 diff.insert((*k).clone(), MetricAdded);
929             }
930         }
931         diff
932     }
933
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`.
939     ///
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.
943     ///
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) {
948         let m = Metric {
949             value: value,
950             noise: noise
951         };
952         self.insert(name.to_owned(), m);
953     }
954
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) {
963             MetricMap::load(p)
964         } else {
965             MetricMap::new()
966         };
967
968         let diff : MetricDiff = self.compare_to_old(&old, pct);
969         let ok = do diff.iter().all() |(_, v)| {
970             match *v {
971                 Regression(_) => false,
972                 _ => true
973             }
974         };
975
976         if ok {
977             debug!("rewriting file '%s' with updated metrics");
978             self.save(p);
979         }
980         return (diff, ok)
981     }
982 }
983
984
985 // Benchmarking
986
987 impl BenchHarness {
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) {
993             inner();
994         }
995         self.ns_end = precise_time_ns();
996     }
997
998     pub fn ns_elapsed(&mut self) -> u64 {
999         if self.ns_start == 0 || self.ns_end == 0 {
1000             0
1001         } else {
1002             self.ns_end - self.ns_start
1003         }
1004     }
1005
1006     pub fn ns_per_iter(&mut self) -> u64 {
1007         if self.iterations == 0 {
1008             0
1009         } else {
1010             self.ns_elapsed() / self.iterations.max(&1)
1011         }
1012     }
1013
1014     pub fn bench_n(&mut self, n: u64, f: &fn(&mut BenchHarness)) {
1015         self.iterations = n;
1016         debug!("running benchmark for %u iterations",
1017                n as uint);
1018         f(self);
1019     }
1020
1021     // This is a more statistics-driven benchmark algorithm
1022     pub fn auto_bench(&mut self, f: &fn(&mut BenchHarness)) -> stats::Summary {
1023
1024         // Initial bench run to get ballpark figure.
1025         let mut n = 1_u64;
1026         self.bench_n(n, |x| f(x));
1027
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 {
1031             n = 1_000_000;
1032         } else {
1033             n = 1_000_000 / self.ns_per_iter().max(&1);
1034         }
1035
1036         let mut total_run = 0;
1037         let samples : &mut [f64] = [0.0_f64, ..50];
1038         loop {
1039             let loop_start = precise_time_ns();
1040
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;
1044             };
1045
1046             stats::winsorize(samples, 5.0);
1047             let summ = stats::Summary::new(samples);
1048
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;
1052             };
1053
1054             stats::winsorize(samples, 5.0);
1055             let summ5 = stats::Summary::new(samples);
1056
1057             debug!("%u samples, median %f, MAD=%f, MADP=%f",
1058                    samples.len(),
1059                    summ.median as float,
1060                    summ.median_abs_dev as float,
1061                    summ.median_abs_dev_pct as float);
1062
1063             let now = precise_time_ns();
1064             let loop_run = now - loop_start;
1065
1066             // If we've run for 100ms an seem to have converged to a
1067             // stable median.
1068             if loop_run > 100_000_000 &&
1069                 summ.median_abs_dev_pct < 1.0 &&
1070                 summ.median - summ5.median < summ5.median_abs_dev {
1071                 return summ5;
1072             }
1073
1074             total_run += loop_run;
1075             // Longest we ever run for is 3s.
1076             if total_run > 3_000_000_000 {
1077                 return summ5;
1078             }
1079
1080             n *= 2;
1081         }
1082     }
1083
1084
1085
1086
1087 }
1088
1089 pub mod bench {
1090     use test::{BenchHarness, BenchSamples};
1091
1092     pub fn benchmark(f: &fn(&mut BenchHarness)) -> BenchSamples {
1093
1094         let mut bs = BenchHarness {
1095             iterations: 0,
1096             ns_start: 0,
1097             ns_end: 0,
1098             bytes: 0
1099         };
1100
1101         let ns_iter_summ = bs.auto_bench(f);
1102
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;
1106
1107         BenchSamples {
1108             ns_iter_summ: ns_iter_summ,
1109             mb_s: mb_s as uint
1110         }
1111     }
1112 }
1113
1114 #[cfg(test)]
1115 mod tests {
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};
1122
1123     use std::either;
1124     use std::comm::{stream, SharedChan};
1125     use std::vec;
1126     use tempfile;
1127     use std::os;
1128
1129     #[test]
1130     pub fn do_not_run_ignored_tests() {
1131         fn f() { fail!(); }
1132         let desc = TestDescAndFn {
1133             desc: TestDesc {
1134                 name: StaticTestName("whatever"),
1135                 ignore: true,
1136                 should_fail: false
1137             },
1138             testfn: DynTestFn(|| f()),
1139         };
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);
1145     }
1146
1147     #[test]
1148     pub fn ignored_tests_result_in_ignored() {
1149         fn f() { }
1150         let desc = TestDescAndFn {
1151             desc: TestDesc {
1152                 name: StaticTestName("whatever"),
1153                 ignore: true,
1154                 should_fail: false
1155             },
1156             testfn: DynTestFn(|| f()),
1157         };
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);
1163     }
1164
1165     #[test]
1166     #[ignore(cfg(windows))]
1167     fn test_should_fail() {
1168         fn f() { fail!(); }
1169         let desc = TestDescAndFn {
1170             desc: TestDesc {
1171                 name: StaticTestName("whatever"),
1172                 ignore: false,
1173                 should_fail: true
1174             },
1175             testfn: DynTestFn(|| f()),
1176         };
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);
1182     }
1183
1184     #[test]
1185     fn test_should_fail_but_succeeds() {
1186         fn f() { }
1187         let desc = TestDescAndFn {
1188             desc: TestDesc {
1189                 name: StaticTestName("whatever"),
1190                 ignore: false,
1191                 should_fail: true
1192             },
1193             testfn: DynTestFn(|| f()),
1194         };
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);
1200     }
1201
1202     #[test]
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")
1208         };
1209         assert!("filter" == opts.filter.clone().unwrap());
1210     }
1211
1212     #[test]
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")
1218         };
1219         assert!((opts.run_ignored));
1220     }
1221
1222     #[test]
1223     pub fn filter_for_ignored_option() {
1224         fn dummy() {}
1225
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
1228
1229         let opts = TestOpts {
1230             filter: None,
1231             run_ignored: true,
1232             logfile: None,
1233             run_tests: true,
1234             run_benchmarks: false,
1235             ratchet_noise_percent: None,
1236             ratchet_metrics: None,
1237             save_metrics: None,
1238         };
1239
1240         let tests = ~[
1241             TestDescAndFn {
1242                 desc: TestDesc {
1243                     name: StaticTestName("1"),
1244                     ignore: true,
1245                     should_fail: false,
1246                 },
1247                 testfn: DynTestFn(|| {}),
1248             },
1249             TestDescAndFn {
1250                 desc: TestDesc {
1251                     name: StaticTestName("2"),
1252                     ignore: false,
1253                     should_fail: false
1254                 },
1255                 testfn: DynTestFn(|| {}),
1256             },
1257         ];
1258         let filtered = filter_tests(&opts, tests);
1259
1260         assert_eq!(filtered.len(), 1);
1261         assert_eq!(filtered[0].desc.name.to_str(), ~"1");
1262         assert!(filtered[0].desc.ignore == false);
1263     }
1264
1265     #[test]
1266     pub fn sort_tests() {
1267         let opts = TestOpts {
1268             filter: None,
1269             run_ignored: false,
1270             logfile: None,
1271             run_tests: true,
1272             run_benchmarks: false,
1273             ratchet_noise_percent: None,
1274             ratchet_metrics: None,
1275             save_metrics: None,
1276         };
1277
1278         let names =
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"];
1285         let tests =
1286         {
1287             fn testfn() { }
1288             let mut tests = ~[];
1289             for name in names.iter() {
1290                 let test = TestDescAndFn {
1291                     desc: TestDesc {
1292                         name: DynTestName((*name).clone()),
1293                         ignore: false,
1294                         should_fail: false
1295                     },
1296                     testfn: DynTestFn(testfn),
1297                 };
1298                 tests.push(test);
1299             }
1300             tests
1301         };
1302         let filtered = filter_tests(&opts, tests);
1303
1304         let expected =
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"];
1312
1313         let pairs = vec::zip(expected, filtered);
1314
1315         for p in pairs.iter() {
1316             match *p {
1317                 (ref a, ref b) => {
1318                     assert!(*a == b.desc.name.to_str());
1319                 }
1320             }
1321         }
1322     }
1323
1324     #[test]
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);
1330
1331         m1.insert_metric("in-first-noise", 1000.0, 2.0);
1332         m2.insert_metric("in-second-noise", 1000.0, 2.0);
1333
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);
1336
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);
1339
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);
1342
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);
1345
1346         let diff1 = m2.compare_to_old(&m1, None);
1347
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()),
1352                    Regression(100.0));
1353         assert_eq!(*(diff1.find(&~"in-both-want-downwards-and-improved").unwrap()),
1354                    Improvement(50.0));
1355         assert_eq!(*(diff1.find(&~"in-both-want-upwards-but-regressed").unwrap()),
1356                    Regression(50.0));
1357         assert_eq!(*(diff1.find(&~"in-both-want-upwards-and-improved").unwrap()),
1358                    Improvement(100.0));
1359         assert_eq!(diff1.len(), 7);
1360
1361         let diff2 = m2.compare_to_old(&m1, Some(200.0));
1362
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);
1371     }
1372
1373     pub fn ratchet_test() {
1374
1375         let dpth = tempfile::mkdtemp(&os::tmpdir(),
1376                                      "test-ratchet").expect("missing test for ratchet");
1377         let pth = dpth.push("ratchet.json");
1378
1379         let mut m1 = MetricMap::new();
1380         m1.insert_metric("runtime", 1000.0, 2.0);
1381         m1.insert_metric("throughput", 50.0, 2.0);
1382
1383         let mut m2 = MetricMap::new();
1384         m2.insert_metric("runtime", 1100.0, 2.0);
1385         m2.insert_metric("throughput", 50.0, 2.0);
1386
1387         m1.save(&pth);
1388
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);
1395
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 });
1401
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);
1409
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 });
1415
1416         os::remove_dir_recursive(&dpth);
1417     }
1418 }