Skip to content

Commit 54eabc5

Browse files
committed
initial tests added
1 parent 1c9281e commit 54eabc5

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

app/V1Module/presenters/RegistrationPresenter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,18 @@ public function actionAcceptInvitation()
414414
IResponse::S201_CREATED
415415
);
416416
}
417+
418+
/**
419+
* Endpoint for performance testing.
420+
* @POST
421+
* @param int $a
422+
* @Param(type="query", name="b", validation="email")
423+
* @Param(type="post", name="c", validation="float")
424+
* @throws BadRequestException
425+
* @throws InvalidArgumentException
426+
*/
427+
public function actionTestLoose(int $a, ?string $email)
428+
{
429+
$this->sendSuccessResponse("OK");
430+
}
417431
}

app/V1Module/router/RouterFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ private static function createUsersRoutes(string $prefix): RouteList
478478
$router[] = new PostRoute("$prefix/list", "Users:listByIds");
479479
$router[] = new GetRoute("$prefix/ical/<id>", "UserCalendars:");
480480
$router[] = new DeleteRoute("$prefix/ical/<id>", "UserCalendars:expireCalendar");
481+
$router[] = new PostRoute("$prefix/testLoose/<a>", "Registration:testLoose");
481482
$router[] = new PostRoute("$prefix/invite", "Registration:createInvitation");
482483
$router[] = new PostRoute("$prefix/accept-invitation", "Registration:acceptInvitation");
483484

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

app/config/config.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ services:
321321
- App\Console\GeneralStatsNotification
322322
- App\Console\ExportDatabase
323323
- App\Console\GenerateSwagger
324+
- App\Console\TestFormatPerformance
324325
- App\Console\SwaggerAnnotator
325326
- App\Console\CleanupLocalizedTexts
326327
- App\Console\CleanupExerciseConfigs

0 commit comments

Comments
 (0)