-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rs
116 lines (103 loc) · 2.59 KB
/
benchmark.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
mod position;
mod solver;
use crate::solver::{Outcome, Solver};
fn main() {
for test in [
"end_easy",
"middle_easy",
"middle_medium",
"begin_easy",
"begin_medium",
"begin_hard",
] {
for strongly in [false, true] {
if let Err(err) =
test_file(
test,
format!("{}/data/{}", env!("CARGO_MANIFEST_DIR"), test),
strongly,
)
{
println!("{}", err);
break
}
}
}
}
fn test_file(title: &str, filename: String, strongly: bool) -> Result<(), String> {
let start = std::time::Instant::now();
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
let mut sum_durations = 0u128;
let mut sum_positions_checked = 0u128;
let mut count = 0u16;
for line in read_lines(filename).unwrap() {
if start.elapsed().as_secs() > 1_200 { break }
let line_str = line.unwrap();
let mut split = line_str.split(' ');
let pos_str = split.next().unwrap().to_string();
let expected_outcome: Outcome = split.next().unwrap().parse::<i8>().unwrap().into();
let now = std::time::Instant::now();
let (_best_mov, positions_checked, actual_outcome) =
if strongly {
Solver::solve_str(pos_str).unwrap()
} else {
Solver::weakly_solve_str(pos_str).unwrap()
};
let duration = now.elapsed().as_nanos();
if strongly {
if actual_outcome != expected_outcome {
return Err(format!(
"{}:{}: {}\n\
\texpected {:?}, got {:?}",
title,
count,
line_str.trim(),
actual_outcome,
expected_outcome
))
}
} else {
if std::mem::discriminant(&actual_outcome) != std::mem::discriminant(&expected_outcome) {
return Err(format!(
"{}:{}: {}\n\
\texpected {:?}, got {:?} (weak solver)",
title,
count,
line_str.trim(),
actual_outcome,
expected_outcome
))
}
}
sum_durations += duration;
sum_positions_checked += positions_checked;
count += 1;
}
let mean_nanos = sum_durations as f64/ count as f64;
println!("test={} mean_time={} mean_nb_pos={:.1} strongly_solved={} completion={}",
title,
if mean_nanos > 1e9 {
format!("{:.4}s", mean_nanos / 1e9)
} else if mean_nanos > 1e6 {
format!("{:.4}ms", mean_nanos / 1e6)
} else if mean_nanos > 1e3 {
format!("{:.4}μs", mean_nanos / 1e3)
} else {
format!("{:.4}ns", mean_nanos)
},
sum_positions_checked as f64 / count as f64,
strongly,
count,
);
match count {
1_000 => Ok(()),
_ => Err("Could not finish in time.".to_string())
}
}