rob-revisited/ide/tests/Profile/ProfilerEndToEndTest.php

69 lines
3.4 KiB
PHP

<?php
namespace JumPHP\Ide\Tests\Profile;
use JumPHP\Ide\Lang\Messages;
use JumPHP\Ide\Profile\Profiler;
use JumPHP\Ide\Profile\ProfileStore;
use PHPUnit\Framework\TestCase;
/**
* The real thing: games/hello-world run for 120 frames headless by bin/jumphp under Xdebug (profile + trace), then read.
* Needs Docker and the game's vendor/ (skipped without them, e.g. inside the IDE's own container); the first run builds the
* Xdebug image (a minute). Run from the repository root: docker is reached by the host, so run this one on the host's PHP:
* JUMPHP_E2E=1 php ide/vendor/bin/phpunit ide/tests/Profile/ProfilerEndToEndTest.php (the IDE container has no docker)
*/
final class ProfilerEndToEndTest extends TestCase
{
private string $repo;
protected function setUp(): void
{
Messages::setLocale('en');
$this->repo = dirname(__DIR__, 3);
if (getenv('JUMPHP_E2E') !== '1') {
self::markTestSkipped('set JUMPHP_E2E=1 (needs Docker through bin/jumphp)');
}
if (!is_file($this->repo . '/games/hello-world/vendor/autoload.php') || trim((string)shell_exec('command -v docker')) === '') {
self::markTestSkipped('needs Docker and games/hello-world/vendor');
}
}
public function testHelloWorldProfiledFor120Frames(): void
{
$events = [];
$pr = new Profiler($this->repo, function (string $e, array $p) use (&$events): void {
$events[] = $e;
});
$run = $pr->prepare(['mode' => 'both', 'durationKind' => 'frames', 'durationValue' => 120]);
$env = [];
foreach ($run['env'] as $k => $v) {
$env[] = $k . '=' . escapeshellarg($v);
}
exec('cd ' . escapeshellarg($this->repo) . ' && ' . implode(' ', $env) . ' bin/jumphp shot hello-world 120 2>&1', $out, $code);
self::assertSame(0, $code, implode("\n", $out));
$pr->parse($run['id'], ['label' => 'Screenshot hello-world', 'mode' => 'both', 'strip' => '', 'target' => ['key' => 'shot:hello-world']]);
$p = $pr->finishNow($run['id']);
try {
self::assertIsArray($p, 'no profile: ' . implode("\n", $out));
$f = array_column($p['functions'], null, 'n');
foreach (['JumPHP\Kernel\Engine->tick', 'JumPHP\Scene\SceneManager->update', 'JumPHP\Scene\SceneManager->render'] as $name) {
self::assertArrayHasKey($name, $f, "$name is not in the profile");
self::assertSame(120, $f[$name]['calls'], $name);
self::assertGreaterThan(0, $f[$name]['incl']);
}
self::assertGreaterThan($f['JumPHP\Scene\SceneManager->update']['incl'] * 0.5, $f['JumPHP\Kernel\Engine->tick']['incl']);
self::assertLessThanOrEqual($p['summary']['totalMs'] + 1, $f['JumPHP\Kernel\Engine->tick']['incl']);
self::assertSame(120, $p['insight']['frames']['count']);
self::assertGreaterThan(0, $p['insight']['frames']['avgMs']);
self::assertLessThan(1000, $p['insight']['frames']['avgMs']);
self::assertGreaterThan(1_000_000, $p['summary']['peakBytes']);
self::assertSame('trace', $p['flame']['source']);
self::assertGreaterThan(100, count($p['timeline']['t']));
self::assertSame('engine', $f['JumPHP\Kernel\Engine->tick']['ns']);
self::assertContains('profile.done', $events);
} finally {
$pr->store()->delete($run['id']);
}
}
}