Passed
Pull Request — master (#14)
by Jose
04:13
created

BenchmarkRunner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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
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);
0 ignored issues
show
Bug introduced by
It seems like $event->getDuration() can also be of type double; however, parameter $duration of JMGQ\AStar\Benchmark\Result\Result::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
                $results[] = new Result($size, /** @scrutinizer ignore-type */ $event->getDuration(), $solutionFound);
Loading history...
53
54 2
                $this->progressBar->advance();
55
            }
56
        }
57
58 2
        $this->progressBar->finish();
59
60 2
        return $results;
61
    }
62
}
63