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
|
|
|
const SIZE_OPTION = 'size'; |
18
|
|
|
const ITERATIONS_OPTION = 'iterations'; |
19
|
|
|
const SEED_OPTION = 'seed'; |
20
|
|
|
|
21
|
|
|
const SUCCESS_EXIT_CODE = 0; |
22
|
|
|
const ERROR_EXIT_CODE = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
2 |
|
protected function configure() |
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) |
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
|
1 |
|
$sizes = $input->getOption(self::SIZE_OPTION); |
56
|
1 |
|
$iterations = $input->getOption(self::ITERATIONS_OPTION); |
57
|
1 |
|
$seed = $input->getOption(self::SEED_OPTION); |
58
|
|
|
|
59
|
1 |
|
$progressBar = $styledOutput->createProgressBar(); |
60
|
1 |
|
$benchmarkRunner = new BenchmarkRunner($progressBar); |
61
|
|
|
|
62
|
1 |
|
$results = $benchmarkRunner->run($sizes, $iterations, $seed); |
|
|
|
|
63
|
|
|
|
64
|
1 |
|
$this->printResults($results, $styledOutput); |
65
|
|
|
|
66
|
1 |
|
return self::SUCCESS_EXIT_CODE; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
private function addSizeOption() |
70
|
|
|
{ |
71
|
2 |
|
$description = 'Number of rows and columns of the terrain'; |
72
|
2 |
|
$defaultValue = array(5, 10, 15, 20, 25); |
73
|
|
|
|
74
|
2 |
|
$this->addOption( |
75
|
2 |
|
self::SIZE_OPTION, |
76
|
2 |
|
's', |
77
|
2 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
78
|
|
|
$description, |
79
|
|
|
$defaultValue |
80
|
|
|
); |
81
|
|
|
|
82
|
2 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
private function addIterationsOption() |
86
|
|
|
{ |
87
|
2 |
|
$description = 'Number of times the algorithm will run against each terrain'; |
88
|
2 |
|
$defaultValue = 10; |
89
|
|
|
|
90
|
2 |
|
$this->addOption(self::ITERATIONS_OPTION, 'i', InputOption::VALUE_REQUIRED, $description, $defaultValue); |
91
|
|
|
|
92
|
2 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
private function addSeedOption() |
96
|
|
|
{ |
97
|
2 |
|
$description = 'Integer used to generate random costs. Set the same value in order to replicate an execution'; |
98
|
2 |
|
$defaultValue = null; |
99
|
|
|
|
100
|
2 |
|
$this->addOption(self::SEED_OPTION, 'e', InputOption::VALUE_REQUIRED, $description, $defaultValue); |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Result[] $results |
107
|
|
|
* @param StyleInterface $output |
108
|
|
|
*/ |
109
|
1 |
|
private function printResults(array $results, StyleInterface $output) |
110
|
|
|
{ |
111
|
1 |
|
$output->newLine(); |
112
|
|
|
|
113
|
1 |
|
$resultAggregator = new ResultAggregator(); |
114
|
1 |
|
$aggregatedResults = $resultAggregator->process($results); |
115
|
|
|
|
116
|
1 |
|
$resultPrinter = new ResultPrinter($output); |
117
|
1 |
|
$resultPrinter->display($aggregatedResults); |
118
|
1 |
|
} |
119
|
|
|
} |
120
|
|
|
|