Skip to content

Commit bbdbd3c

Browse files
committed
extra: add explicit ratchet-noise-percent option to benchmark ratchet, plus a few test breaking fixes.
1 parent 83fb3d2 commit bbdbd3c

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

src/compiletest/compiletest.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn parse_config(args: ~[~str]) -> config {
7979
let args_ = args.tail();
8080
if args[1] == ~"-h" || args[1] == ~"--help" {
8181
let message = fmt!("Usage: %s [OPTIONS] [TESTNAME...]", argv0);
82-
io::println(getopts::groups::usage(message, groups));
82+
println(getopts::groups::usage(message, groups));
8383
fail!()
8484
}
8585

@@ -91,7 +91,7 @@ pub fn parse_config(args: ~[~str]) -> config {
9191

9292
if getopts::opt_present(matches, "h") || getopts::opt_present(matches, "help") {
9393
let message = fmt!("Usage: %s [OPTIONS] [TESTNAME...]", argv0);
94-
io::println(getopts::groups::usage(message, groups));
94+
println(getopts::groups::usage(message, groups));
9595
fail!()
9696
}
9797

@@ -216,8 +216,9 @@ pub fn test_opts(config: &config) -> test::TestOpts {
216216
logfile: copy config.logfile,
217217
run_tests: true,
218218
run_benchmarks: false,
219-
save_results: None,
220-
compare_results: None
219+
ratchet_metrics: None,
220+
ratchet_noise_percent: None,
221+
save_metrics: None,
221222
}
222223
}
223224

src/libextra/json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ impl<A:ToJson> ToJson for ~[A] {
13311331
fn to_json(&self) -> Json { List(self.map(|elt| elt.to_json())) }
13321332
}
13331333

1334-
impl<A:ToJson + Copy> ToJson for HashMap<~str, A> {
1334+
impl<A:ToJson> ToJson for HashMap<~str, A> {
13351335
fn to_json(&self) -> Json {
13361336
let mut d = HashMap::new();
13371337
for self.iter().advance |(key, value)| {
@@ -1341,7 +1341,7 @@ impl<A:ToJson + Copy> ToJson for HashMap<~str, A> {
13411341
}
13421342
}
13431343

1344-
impl<A:ToJson + Copy> ToJson for TreeMap<~str, A> {
1344+
impl<A:ToJson> ToJson for TreeMap<~str, A> {
13451345
fn to_json(&self) -> Json {
13461346
let mut d = HashMap::new();
13471347
for self.iter().advance |(key, value)| {

src/libextra/test.rs

+50-24
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ use treemap::TreeMap;
3030
use std::comm::{stream, SharedChan};
3131
use std::either;
3232
use std::io;
33-
use std::option;
3433
use std::result;
3534
use std::task;
3635
use std::to_str::ToStr;
3736
use std::u64;
37+
use std::f64;
3838
use std::hashmap::HashMap;
3939
use std::os;
4040

@@ -149,6 +149,7 @@ pub struct TestOpts {
149149
run_tests: bool,
150150
run_benchmarks: bool,
151151
ratchet_metrics: Option<Path>,
152+
ratchet_noise_percent: Option<f64>,
152153
save_metrics: Option<Path>,
153154
logfile: Option<Path>
154155
}
@@ -163,6 +164,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
163164
getopts::optflag("bench"),
164165
getopts::optopt("save-metrics"),
165166
getopts::optopt("ratchet-metrics"),
167+
getopts::optopt("ratchet-noise-percent"),
166168
getopts::optopt("logfile")];
167169
let matches =
168170
match getopts::getopts(args_, opts) {
@@ -172,8 +174,8 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
172174

173175
let filter =
174176
if matches.free.len() > 0 {
175-
option::Some(copy (matches).free[0])
176-
} else { option::None };
177+
Some(copy (matches).free[0])
178+
} else { None };
177179

178180
let run_ignored = getopts::opt_present(&matches, "ignored");
179181

@@ -187,6 +189,10 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
187189
let ratchet_metrics = getopts::opt_maybe_str(&matches, "ratchet-metrics");
188190
let ratchet_metrics = ratchet_metrics.map(|s| Path(*s));
189191

192+
let ratchet_noise_percent =
193+
getopts::opt_maybe_str(&matches, "ratchet-noise-percent");
194+
let ratchet_noise_percent = ratchet_noise_percent.map(|s| f64::from_str(*s).get());
195+
190196
let save_metrics = getopts::opt_maybe_str(&matches, "save-metrics");
191197
let save_metrics = save_metrics.map(|s| Path(*s));
192198

@@ -196,6 +202,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
196202
run_tests: run_tests,
197203
run_benchmarks: run_benchmarks,
198204
ratchet_metrics: ratchet_metrics,
205+
ratchet_noise_percent: ratchet_noise_percent,
199206
save_metrics: save_metrics,
200207
logfile: logfile
201208
};
@@ -405,14 +412,22 @@ impl ConsoleTestState {
405412
}
406413
}
407414
408-
pub fn write_run_finish(&self, ratchet_metrics: &Option<Path>) -> bool {
415+
pub fn write_run_finish(&self,
416+
ratchet_metrics: &Option<Path>,
417+
ratchet_pct: Option<f64>) -> bool {
409418
assert!(self.passed + self.failed + self.ignored + self.benchmarked == self.total);
410419
411420
let ratchet_success = match *ratchet_metrics {
412421
None => true,
413422
Some(ref pth) => {
414423
self.out.write_str(fmt!("\nusing metrics ratchet: %s\n", pth.to_str()));
415-
let (diff, ok) = self.metrics.ratchet(pth);
424+
match ratchet_pct {
425+
None => (),
426+
Some(pct) =>
427+
self.out.write_str(fmt!("with noise-tolerance forced to: %f%%\n",
428+
pct as float))
429+
}
430+
let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
416431
self.write_metric_diff(&diff);
417432
ok
418433
}
@@ -488,7 +503,7 @@ pub fn run_tests_console(opts: &TestOpts,
488503
st.out.write_str(fmt!("\nmetrics saved to: %s", pth.to_str()));
489504
}
490505
}
491-
return st.write_run_finish(&opts.ratchet_metrics);
506+
return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
492507
}
493508

494509
#[test]
@@ -510,18 +525,19 @@ fn should_sort_failures_before_printing_them() {
510525

511526
let st = @ConsoleTestState {
512527
out: wr,
513-
log_out: option::None,
528+
log_out: None,
529+
term: None,
514530
use_color: false,
515531
total: 0u,
516532
passed: 0u,
517533
failed: 0u,
518534
ignored: 0u,
519535
benchmarked: 0u,
520-
metrics: MetricsMap::new(),
536+
metrics: MetricMap::new(),
521537
failures: ~[test_b, test_a]
522538
};
523539

524-
print_failures(st);
540+
st.write_failures();
525541
};
526542

527543
let apos = s.find_str("a").get();
@@ -624,15 +640,17 @@ pub fn filter_tests(
624640
filtered
625641
} else {
626642
let filter_str = match opts.filter {
627-
option::Some(ref f) => copy *f,
628-
option::None => ~""
643+
Some(ref f) => copy *f,
644+
None => ~""
629645
};
630646

631647
fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
632648
Option<TestDescAndFn> {
633649
if test.desc.name.to_str().contains(filter_str) {
634-
return option::Some(test);
635-
} else { return option::None; }
650+
return Some(test);
651+
} else {
652+
return None;
653+
}
636654
}
637655

638656
filtered.consume_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
@@ -757,14 +775,19 @@ impl MetricMap {
757775
}
758776

759777
/// Compare against another MetricMap
760-
pub fn compare_to_old(&self, old: MetricMap) -> MetricDiff {
778+
pub fn compare_to_old(&self, old: MetricMap,
779+
noise_pct: Option<f64>) -> MetricDiff {
761780
let mut diff : MetricDiff = TreeMap::new();
762781
for old.iter().advance |(k, vold)| {
763782
let r = match self.find(k) {
764783
None => MetricRemoved,
765784
Some(v) => {
766785
let delta = (v.value - vold.value);
767-
if delta.abs() < vold.noise.abs() {
786+
let noise = match noise_pct {
787+
None => f64::max(vold.noise.abs(), v.noise.abs()),
788+
Some(pct) => vold.value * pct / 100.0
789+
};
790+
if delta.abs() < noise {
768791
LikelyNoise
769792
} else {
770793
let pct = delta.abs() / v.value * 100.0;
@@ -827,14 +850,14 @@ impl MetricMap {
827850
/// file to contain the metrics in `self` if none of the
828851
/// `MetricChange`s are `Regression`. Returns the diff as well
829852
/// as a boolean indicating whether the ratchet succeeded.
830-
pub fn ratchet(&self, p: &Path) -> (MetricDiff, bool) {
853+
pub fn ratchet(&self, p: &Path, pct: Option<f64>) -> (MetricDiff, bool) {
831854
let old = if os::path_exists(p) {
832855
MetricMap::load(p)
833856
} else {
834857
MetricMap::new()
835858
};
836859

837-
let diff : MetricDiff = self.compare_to_old(old);
860+
let diff : MetricDiff = self.compare_to_old(old, pct);
838861
let ok = do diff.iter().all() |(_, v)| {
839862
match *v {
840863
Regression(_) => false,
@@ -1092,12 +1115,14 @@ mod tests {
10921115
// unignored tests and flip the ignore flag on the rest to false
10931116

10941117
let opts = TestOpts {
1095-
filter: option::None,
1118+
filter: None,
10961119
run_ignored: true,
1097-
logfile: option::None,
1120+
logfile: None,
10981121
run_tests: true,
10991122
run_benchmarks: false,
1100-
ratchet: option::None,
1123+
ratchet_noise_percent: None,
1124+
ratchet_metrics: None,
1125+
save_metrics: None,
11011126
};
11021127

11031128
let tests = ~[
@@ -1128,13 +1153,14 @@ mod tests {
11281153
#[test]
11291154
pub fn sort_tests() {
11301155
let opts = TestOpts {
1131-
filter: option::None,
1156+
filter: None,
11321157
run_ignored: false,
1133-
logfile: option::None,
1158+
logfile: None,
11341159
run_tests: true,
11351160
run_benchmarks: false,
1136-
ratchet_metrics: option::None,
1137-
save_metrics: option::None,
1161+
ratchet_noise_percent: None,
1162+
ratchet_metrics: None,
1163+
save_metrics: None,
11381164
};
11391165
11401166
let names =

0 commit comments

Comments
 (0)