|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console; |
| 4 | + |
| 5 | +use App\Exceptions\InternalServerException; |
| 6 | +use App\Helpers\Mocks\MockHelper; |
| 7 | +use App\V1Module\Presenters\RegistrationPresenter; |
| 8 | +use Nette\Application\Request; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | + |
| 13 | +class TestFormatPerformance extends Command |
| 14 | +{ |
| 15 | + protected static $defaultName = 'test:performance'; |
| 16 | + |
| 17 | + private static $warmupIterations = 100_000; |
| 18 | + private static $measureIterations = 1_000_000; |
| 19 | + private $tests = []; |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + parent::__construct(); |
| 24 | + |
| 25 | + $this->tests = [ |
| 26 | + "loose" => new Request( |
| 27 | + "name", |
| 28 | + method: "POST", |
| 29 | + params: ["action" => "testLoose", "a" => "1", "b" => "a@a.a"], |
| 30 | + post: ["c" => 1.1] |
| 31 | + ), |
| 32 | + "format" => new Request( |
| 33 | + "name", |
| 34 | + method: "POST", |
| 35 | + params: ["action" => "testFormat", "a" => "1", "b" => "a@a.a"], |
| 36 | + post: ["c" => 1.1] |
| 37 | + ), |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + protected function configure() |
| 43 | + { |
| 44 | + $this->setName(self::$defaultName)->setDescription( |
| 45 | + 'Generate an OpenAPI documentation from the temporary file created by the swagger:annotate command.' |
| 46 | + . ' The temporary file is deleted afterwards.' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + private function warmup(RegistrationPresenter $presenter, Request $request) |
| 51 | + { |
| 52 | + // test whether the request works |
| 53 | + for ($i = 0; $i < self::$warmupIterations; $i++) { |
| 54 | + $response = $presenter->run($request); |
| 55 | + if ($response->getPayload()["payload"] != "OK") { |
| 56 | + throw new InternalServerException("The endpoint did not run correctly."); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private function measure(RegistrationPresenter $presenter, Request $request): float |
| 62 | + { |
| 63 | + $startTime = microtime(true); |
| 64 | + for ($i = 0; $i < self::$measureIterations; $i++) { |
| 65 | + $response = $presenter->run($request); |
| 66 | + } |
| 67 | + $endTime = microtime(true); |
| 68 | + |
| 69 | + return $endTime - $startTime; |
| 70 | + } |
| 71 | + |
| 72 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 73 | + { |
| 74 | + $output->writeln("test"); |
| 75 | + |
| 76 | + $presenter = new RegistrationPresenter(); |
| 77 | + MockHelper::initPresenter($presenter); |
| 78 | + |
| 79 | + foreach ($this->tests as $name => $request) { |
| 80 | + $output->writeln("Executing test: " . $name); |
| 81 | + |
| 82 | + $this->warmup($presenter, $request); |
| 83 | + $elapsed = $this->measure($presenter, $request); |
| 84 | + |
| 85 | + $output->writeln("Time elapsed: " . $elapsed); |
| 86 | + } |
| 87 | + |
| 88 | + return Command::SUCCESS; |
| 89 | + } |
| 90 | +} |
0 commit comments