Changed benchmark output

This commit is contained in:
Lukas Wölfer
2020-02-13 20:00:34 +01:00
parent 1a8f9a83b0
commit e749c877f1
4 changed files with 60 additions and 26 deletions

4
.vscode/tasks.json vendored
View File

@@ -10,7 +10,8 @@
"args": [ "args": [
"-m", "-m",
"benchmark.timing" "benchmark.timing"
] ],
"problemMatcher": []
}, },
{ {
"label": "Unsolved benchmark", "label": "Unsolved benchmark",
@@ -20,6 +21,7 @@
"-m", "-m",
"benchmark.unsolved" "benchmark.unsolved"
], ],
"problemMatcher": []
} }
] ]
} }

View File

@@ -27,8 +27,9 @@ def main() -> None:
result = pool.imap_unordered( result = pool.imap_unordered(
run_benchmark, [Path(benchmark) for benchmark in benchmark_files] run_benchmark, [Path(benchmark) for benchmark in benchmark_files]
) )
for current_result in result: pool.close()
print(current_result) pool.join()
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -28,13 +28,17 @@ benchmark_files = [
] ]
def runner(benchmark: Path) -> None:
run_benchmark(benchmark, timeout=60)
def main() -> None: def main() -> None:
with multiprocessing.Pool() as pool: with multiprocessing.Pool() as pool:
result = pool.imap_unordered( result = pool.imap_unordered(
run_benchmark, [Path(benchmark) for benchmark in benchmark_files] runner, [Path(benchmark) for benchmark in benchmark_files]
) )
for current_result in result: pool.close()
print(current_result) pool.join()
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -6,31 +6,58 @@ import cv2
import shenzhen_solitaire.card_detection.configuration as configuration import shenzhen_solitaire.card_detection.configuration as configuration
import shenzhen_solitaire.solver.solver as solver import shenzhen_solitaire.solver.solver as solver
from shenzhen_solitaire.card_detection.board_parser import parse_board from shenzhen_solitaire.card_detection.board_parser import parse_board
from typing import Callable, List, Tuple
def run_benchmark(benchmark: Path) -> str: class SingleTimer:
def __init__(self, name: str, callback: Callable[[str, float], None]):
self.name = name
self.callback = callback
self.start = 0.0
def __enter__(self) -> None:
self.start = time.time()
return
def __exit__(self, *args) -> None:
self.callback(self.name, time.time() - self.start)
return
class BenchmarkTimer:
def __init__(self) -> None:
self.timing: List[Tuple[str, float]] = []
def addTiming(self, name: str, duration: float) -> None:
self.timing.append((name, duration))
def stopwatch(self, name: str) -> SingleTimer:
return SingleTimer(name, self.addTiming)
@property
def timings(self) -> List[float]:
return [x[1] for x in self.timing]
def run_benchmark(benchmark: Path, *, timeout: float = 10) -> None:
result = "" result = ""
result += f"{benchmark}:\n" result += f"{benchmark}:\n"
read_file_time = time.time() my_timer = BenchmarkTimer()
image = cv2.imread(str(benchmark)) with my_timer.stopwatch("Load image"):
load_config_time = time.time() image = cv2.imread(str(benchmark))
result += f"\tLoad image: {load_config_time - read_file_time:5.2f}\n"
conf = configuration.load("test_config.zip") with my_timer.stopwatch("Load configuration"):
parse_board_time = time.time() conf = configuration.load("test_config.zip")
result += f"\tLoad config: {parse_board_time - load_config_time:5.2f}\n"
board = parse_board(image, conf) with my_timer.stopwatch("Parse board"):
solve_time = time.time() board = parse_board(image, conf)
result += f"\tParse image: {solve_time - parse_board_time:5.2f}\n"
solution_iterator = next(solver.solve(board, timeout=10), None) with my_timer.stopwatch("Solve board"):
finished_time = time.time() solution_iterator = next(solver.solve(board, timeout=timeout), None)
result += f"\tSolve board: {finished_time - solve_time:5.2f}\n"
assert board.check_correct() solved_string = (
if solution_iterator is None: "[" + ("Solved" if solution_iterator is not None else "Unsolved") + "]"
result += "\tSolution timed out\n" )
else: timings_string = "\t".join(f"{x:>5.2f}" for x in my_timer.timings)
result += f"\tSolved in {len(list(solution_iterator))} steps\n" print(f"{solved_string:<10} {benchmark}")
return result print(f"{timings_string}")