1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace JMGQ\AStar\Benchmark; |
4
|
|
|
|
5
|
|
|
use JMGQ\AStar\Benchmark\Result\Result; |
6
|
|
|
use JMGQ\AStar\Example\Terrain\MyAStar; |
7
|
|
|
use JMGQ\AStar\Example\Terrain\MyNode; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
9
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
10
|
|
|
|
11
|
|
|
class BenchmarkRunner |
|
|
|
|
12
|
|
|
{ |
|
|
|
|
13
|
|
|
private $progressBar; |
|
|
|
|
14
|
|
|
private $terrainGenerator; |
|
|
|
|
15
|
|
|
private $stopwatch; |
|
|
|
|
16
|
|
|
|
17
|
|
|
public function __construct(ProgressBar $progressBar) |
|
|
|
|
18
|
|
|
{ |
|
|
|
|
19
|
|
|
$this->progressBar = $progressBar; |
|
|
|
|
20
|
|
|
$this->terrainGenerator = new TerrainGenerator(); |
21
|
|
|
$this->stopwatch = new Stopwatch(); |
|
|
|
|
22
|
|
|
} |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @param int[] $sizes |
|
|
|
|
26
|
|
|
* @param int $iterations |
|
|
|
|
27
|
|
|
* @param int | null $seed |
|
|
|
|
28
|
|
|
* @return Result[] |
29
|
|
|
*/ |
30
|
|
|
public function run(array $sizes, $iterations, $seed) |
|
|
|
|
31
|
|
|
{ |
|
|
|
|
32
|
|
|
$results = array(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$steps = count($sizes) * $iterations; |
|
|
|
|
35
|
|
|
$this->progressBar->start($steps); |
36
|
|
|
|
37
|
|
|
foreach ($sizes as $size) { |
38
|
|
|
for ($i = 0; $i < $iterations; $i++) { |
39
|
|
|
$terrain = $this->terrainGenerator->generate($size, $size, $seed); |
40
|
|
|
$aStar = new MyAStar($terrain); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$start = new MyNode(0, 0); |
43
|
|
|
$goal = new MyNode($size - 1, $size - 1); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->stopwatch->start('benchmark'); |
46
|
|
|
|
47
|
|
|
$solution = $aStar->run($start, $goal); |
48
|
|
|
|
49
|
|
|
$event = $this->stopwatch->stop('benchmark'); |
50
|
|
|
|
51
|
|
|
$solutionFound = !empty($solution); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$results[] = new Result($size, $event->getDuration(), $solutionFound); |
54
|
|
|
|
55
|
|
|
$this->stopwatch->reset(); |
56
|
|
|
|
57
|
|
|
$this->progressBar->advance(); |
58
|
|
|
} |
|
|
|
|
59
|
|
|
} |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->progressBar->finish(); |
62
|
|
|
|
63
|
|
|
return $results; |
64
|
|
|
} |
|
|
|
|
65
|
|
|
} |
|
|
|
|
66
|
|
|
|