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
|
|
|
|
16
|
2 |
|
public function __construct(ProgressBar $progressBar) |
17
|
|
|
{ |
18
|
2 |
|
$this->progressBar = $progressBar; |
19
|
2 |
|
$this->terrainGenerator = new TerrainGenerator(); |
20
|
2 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int[] $sizes |
24
|
|
|
* @param int $iterations |
25
|
|
|
* @param int | null $seed |
26
|
|
|
* @return Result[] |
27
|
|
|
*/ |
28
|
2 |
|
public function run(array $sizes, $iterations, $seed) |
29
|
|
|
{ |
30
|
2 |
|
$results = array(); |
31
|
|
|
|
32
|
2 |
|
$steps = count($sizes) * $iterations; |
33
|
2 |
|
$this->progressBar->start($steps); |
34
|
|
|
|
35
|
2 |
|
foreach ($sizes as $size) { |
36
|
2 |
|
for ($i = 0; $i < $iterations; $i++) { |
37
|
2 |
|
$terrain = $this->terrainGenerator->generate($size, $size, $seed); |
38
|
2 |
|
$aStar = new MyAStar($terrain); |
39
|
|
|
|
40
|
2 |
|
$start = new MyNode(0, 0); |
41
|
2 |
|
$goal = new MyNode($size - 1, $size - 1); |
42
|
|
|
|
43
|
2 |
|
$stopwatch = new Stopwatch(); |
44
|
2 |
|
$stopwatch->start('benchmark'); |
45
|
|
|
|
46
|
2 |
|
$solution = $aStar->run($start, $goal); |
47
|
|
|
|
48
|
2 |
|
$event = $stopwatch->stop('benchmark'); |
49
|
|
|
|
50
|
2 |
|
$solutionFound = !empty($solution); |
51
|
|
|
|
52
|
2 |
|
$results[] = new Result($size, $event->getDuration(), $solutionFound); |
|
|
|
|
53
|
|
|
|
54
|
2 |
|
$this->progressBar->advance(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
$this->progressBar->finish(); |
59
|
|
|
|
60
|
2 |
|
return $results; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|