BenchmarkCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 50
c 1
b 0
f 0
dl 0
loc 106
ccs 49
cts 49
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A addIterationsOption() 0 8 1
A addSizeOption() 0 14 1
A execute() 0 26 2
A printResults() 0 9 1
A addSeedOption() 0 8 1
1
<?php
2
3
namespace JMGQ\AStar\Benchmark;
4
5
use JMGQ\AStar\Benchmark\Result\Result;
6
use JMGQ\AStar\Benchmark\Result\ResultAggregator;
7
use JMGQ\AStar\Benchmark\Result\ResultPrinter;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\StyleInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
class BenchmarkCommand extends Command
16
{
17
    public const SIZE_OPTION = 'size';
18
    public const ITERATIONS_OPTION = 'iterations';
19
    public const SEED_OPTION = 'seed';
20
21
    private const SUCCESS_EXIT_CODE = 0;
22
    private const ERROR_EXIT_CODE = 1;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    protected function configure(): void
28
    {
29 2
        $name = 'benchmark';
30 2
        $description = 'Runs a benchmark using the Terrain example';
31 2
        $help = 'This commands allows you to run a benchmark using the Terrain example';
32
33 2
        $this->setName($name)
34 2
            ->setDescription($description)
35 2
            ->setHelp($help)
36 2
            ->addSizeOption()
37 2
            ->addIterationsOption()
38 2
            ->addSeedOption();
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46 2
        $styledOutput = new SymfonyStyle($input, $output);
47
48 2
        $inputValidator = new InputValidator($styledOutput);
49 2
        $hasValidInput = $inputValidator->validate($input);
50
51 2
        if (!$hasValidInput) {
52 1
            return self::ERROR_EXIT_CODE;
53
        }
54
55
        /** @var int[] $sizes */
56 1
        $sizes = $input->getOption(self::SIZE_OPTION);
57
        /** @var int $iterations */
58 1
        $iterations = $input->getOption(self::ITERATIONS_OPTION);
59
        /** @var int | null $seed */
60 1
        $seed = $input->getOption(self::SEED_OPTION);
61
62 1
        $progressBar = new SymfonyProgressBar($styledOutput->createProgressBar());
63 1
        $benchmarkRunner = new BenchmarkRunner($progressBar);
64
65 1
        $results = $benchmarkRunner->run($sizes, $iterations, $seed);
66
67 1
        $this->printResults($results, $styledOutput);
68
69 1
        return self::SUCCESS_EXIT_CODE;
70
    }
71
72 2
    private function addSizeOption(): BenchmarkCommand
73
    {
74 2
        $description = 'Number of rows and columns of the terrain';
75 2
        $defaultValue = ['5', '10', '15', '20', '25'];
76
77 2
        $this->addOption(
78 2
            self::SIZE_OPTION,
79 2
            's',
80 2
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
81
            $description,
82
            $defaultValue
83
        );
84
85 2
        return $this;
86
    }
87
88 2
    private function addIterationsOption(): BenchmarkCommand
89
    {
90 2
        $description = 'Number of times the algorithm will run against each terrain';
91 2
        $defaultValue = 10;
92
93 2
        $this->addOption(self::ITERATIONS_OPTION, 'i', InputOption::VALUE_REQUIRED, $description, $defaultValue);
94
95 2
        return $this;
96
    }
97
98 2
    private function addSeedOption(): BenchmarkCommand
99
    {
100 2
        $description = 'Integer used to generate random costs. Set the same value in order to replicate an execution';
101 2
        $defaultValue = null;
102
103 2
        $this->addOption(self::SEED_OPTION, 'e', InputOption::VALUE_REQUIRED, $description, $defaultValue);
104
105 2
        return $this;
106
    }
107
108
    /**
109
     * @param Result[] $results
110
     * @param StyleInterface $output
111
     */
112 1
    private function printResults(array $results, StyleInterface $output): void
113
    {
114 1
        $output->newLine();
115
116 1
        $resultAggregator = new ResultAggregator();
117 1
        $aggregatedResults = $resultAggregator->process($results);
118
119 1
        $resultPrinter = new ResultPrinter($output);
120 1
        $resultPrinter->display($aggregatedResults);
121 1
    }
122
}
123