Passed
Branch benchmark (b81757)
by Jose
02:27
created

BenchmarkRunner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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