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
|
|
|
protected function configure() |
|
|
|
|
28
|
|
|
{ |
|
|
|
|
29
|
|
|
$name = 'benchmark'; |
|
|
|
|
30
|
|
|
$description = 'Runs a benchmark using the Terrain example'; |
31
|
|
|
$help = 'This commands allows you to run a benchmark using the Terrain example'; |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->setName($name) |
34
|
|
|
->setDescription($description) |
|
|
|
|
35
|
|
|
->setHelp($help) |
|
|
|
|
36
|
|
|
->addSizeOption() |
|
|
|
|
37
|
|
|
->addIterationsOption() |
|
|
|
|
38
|
|
|
->addSeedOption(); |
|
|
|
|
39
|
|
|
} |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
|
|
|
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
|
|
|
|
46
|
|
|
$styledOutput = new SymfonyStyle($input, $output); |
47
|
|
|
|
48
|
|
|
$inputValidator = new InputValidator($styledOutput); |
49
|
|
|
$hasValidInput = $inputValidator->validate($input); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if (!$hasValidInput) { |
|
|
|
|
52
|
|
|
return self::ERROR_EXIT_CODE; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$sizes = $input->getOption(self::SIZE_OPTION); |
|
|
|
|
56
|
|
|
$iterations = $input->getOption(self::ITERATIONS_OPTION); |
57
|
|
|
$seed = $input->getOption(self::SEED_OPTION); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$progressBar = $styledOutput->createProgressBar(); |
|
|
|
|
60
|
|
|
$benchmarkRunner = new BenchmarkRunner($progressBar); |
61
|
|
|
|
62
|
|
|
$results = $benchmarkRunner->run($sizes, $iterations, $seed); |
63
|
|
|
|
64
|
|
|
$this->printResults($results, $styledOutput); |
65
|
|
|
|
66
|
|
|
return self::SUCCESS_EXIT_CODE; |
67
|
|
|
} |
|
|
|
|
68
|
|
|
|
69
|
|
|
private function addSizeOption() |
|
|
|
|
70
|
|
|
{ |
|
|
|
|
71
|
|
|
$description = 'Number of rows and columns of the terrain'; |
|
|
|
|
72
|
|
|
$defaultValue = array(5, 10, 15, 20, 25); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->addOption( |
75
|
|
|
self::SIZE_OPTION, |
76
|
|
|
's', |
77
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
|
|
|
78
|
|
|
$description, |
79
|
|
|
$defaultValue |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
private function addIterationsOption() |
|
|
|
|
86
|
|
|
{ |
|
|
|
|
87
|
|
|
$description = 'Number of times the algorithm will run against each terrain'; |
|
|
|
|
88
|
|
|
$defaultValue = 10; |
89
|
|
|
|
90
|
|
|
$this->addOption(self::ITERATIONS_OPTION, 'i', InputOption::VALUE_REQUIRED, $description, $defaultValue); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
|
|
|
|
94
|
|
|
|
95
|
|
|
private function addSeedOption() |
|
|
|
|
96
|
|
|
{ |
|
|
|
|
97
|
|
|
$description = 'Integer used to generate random costs. Set the same value in order to replicate an execution'; |
|
|
|
|
98
|
|
|
$defaultValue = null; |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->addOption(self::SEED_OPTION, 'e', InputOption::VALUE_REQUIRED, $description, $defaultValue); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
|
|
|
|
106
|
|
|
* @param Result[] $results |
|
|
|
|
107
|
|
|
* @param StyleInterface $output |
|
|
|
|
108
|
|
|
*/ |
|
|
|
|
109
|
|
|
private function printResults(array $results, StyleInterface $output) |
|
|
|
|
110
|
|
|
{ |
|
|
|
|
111
|
|
|
$output->newLine(); |
112
|
|
|
|
113
|
|
|
$resultAggregator = new ResultAggregator(); |
|
|
|
|
114
|
|
|
$aggregatedResults = $resultAggregator->process($results); |
115
|
|
|
|
116
|
|
|
$resultPrinter = new ResultPrinter($output); |
117
|
|
|
$resultPrinter->display($aggregatedResults); |
118
|
|
|
} |
|
|
|
|
119
|
|
|
} |
|
|
|
|
120
|
|
|
|