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

214 lines
7.4 KiB
PHP

<?php
namespace JumPHP\Ide\Tests\Profile;
use JumPHP\Ide\Lang\Messages;
use JumPHP\Ide\Profile\CachegrindParser;
use PHPUnit\Framework\TestCase;
/** Xdebug's cachegrind files: inclusive / exclusive costs, recursion, lines, edges, truncation, streaming. */
final class CachegrindParserTest extends TestCase
{
private const FIXTURES = __DIR__ . '/../fixtures/profile';
/** @var list<string> */
private array $temp = [];
protected function setUp(): void
{
Messages::setLocale('en');
}
protected function tearDown(): void
{
foreach ($this->temp as $f) {
@unlink($f);
}
Messages::reset();
}
private function file(string $content, string $ext = ''): string
{
$f = tempnam(sys_get_temp_dir(), 'cg') . $ext;
file_put_contents($f, $content);
$this->temp[] = $f;
return $f;
}
private const HAND = <<<'CG'
version: 1
creator: xdebug 3.5.3
cmd: /app/main.php
part: 1
positions: line
events: Time_(10ns) Memory_(bytes)
fl=(1) /app/main.php
fn=(1) {main}
1 10 0
cfl=(1)
cfn=(2) foo
calls=1 0 0
3 70 100
fl=(1)
fn=(2)
7 30 60
cfl=(1)
cfn=(3) php::bar
calls=2 0 0
8 40 40
fl=(2) php:internal
fn=(3) php::bar
12 25 20
13 15 20
fl=(1)
fn=(4) rec
20 5 0
cfl=(1)
cfn=(4)
calls=1 0 0
20 5 0
fl=(1)
fn=(4)
20 5 0
summary: 80 100
CG;
public function testSelfInclusiveCallsAndMemoryOfEachFunction(): void
{
$p = (new CachegrindParser())->parseAll([$this->file(self::HAND)]);
$f = $p->functions;
// times are in 10 ns units: x 10 ns
self::assertSame(100.0, $f['{main}']['self']);
self::assertSame(800.0, $f['{main}']['incl']);
self::assertSame(300.0, $f['foo']['self']);
self::assertSame(700.0, $f['foo']['incl']);
self::assertSame(1, $f['foo']['calls']);
self::assertSame(400.0, $f['php::bar']['self']);
self::assertSame(2, $f['php::bar']['calls']);
self::assertSame(40.0, $f['php::bar']['mem']);
self::assertSame('php:internal', $f['php::bar']['f']);
self::assertSame('/app/main.php', $f['foo']['f']);
self::assertSame(60.0, $f['foo']['mem']);
self::assertSame(100.0, $f['foo']['imem']); // own 60 + bar's 40
self::assertSame(800.0, $p->total());
}
public function testARecursiveCallIsNotCountedTwiceInTheInclusiveTime(): void
{
$p = (new CachegrindParser())->parseAll([$this->file(self::HAND)]);
self::assertSame(100.0, $p->functions['rec']['self']); // 5 + 5 units
self::assertSame(100.0, $p->functions['rec']['incl']);
self::assertSame(1, $p->functions['rec']['calls']);
}
public function testCostsPerLineAndPerEdge(): void
{
$p = (new CachegrindParser())->parseAll([$this->file(self::HAND)]);
self::assertSame(250.0, $p->lines['php::bar'][12][0]);
self::assertSame(150.0, $p->lines['php::bar'][13][0]);
self::assertSame(300.0, $p->lines['foo'][7][0]);
self::assertSame([2, 400.0, 40.0], $p->edges["foo\0php::bar"]);
self::assertSame([1, 700.0, 100.0], $p->edges["{main}\0foo"]);
self::assertSame(1, $p->functions['{main}']['line']);
}
public function testATruncatedFileKeepsWhatWasWritten(): void
{
$cut = substr(self::HAND, 0, (int)strpos(self::HAND, 'fl=(2)')) . "fl=(2) php:internal\nfn=(3) php::ba";
$p = (new CachegrindParser())->parseAll([$this->file($cut)]);
self::assertSame(300.0, $p->functions['foo']['self']);
self::assertFalse($p->summarySeen);
self::assertGreaterThan(0, $p->total()); // without a summary: the sum of the self costs
}
public function testSeveralFilesAreAdded(): void
{
$a = $this->file(self::HAND);
$p = (new CachegrindParser())->parseAll([$a, $a]);
self::assertSame(600.0, $p->functions['foo']['self']);
self::assertSame(2, $p->filesRead);
self::assertSame(1600.0, $p->total());
}
public function testAGzippedFileIsRead(): void
{
$f = $this->file(gzencode(self::HAND), '.gz');
$p = (new CachegrindParser())->parseAll([$f]);
self::assertSame(300.0, $p->functions['foo']['self']);
}
public function testItReadsInSlicesOfTime(): void
{
$p = new CachegrindParser();
$p->open([$this->file(self::HAND)]);
$rounds = 0;
while (!$p->run(0.0)) {
$rounds++;
}
self::assertSame(300.0, $p->functions['foo']['self']);
}
public function testAMissingFileIsSaidNotFatal(): void
{
$p = (new CachegrindParser())->parseAll(['/nonexistent/cachegrind.out.1']);
self::assertSame([], $p->functions);
self::assertStringContainsString('cachegrind.out.1', $p->notes[0]);
}
public function testARealXdebugFile(): void
{
$p = (new CachegrindParser(trackPattern: '/^fib$/'))->parseAll([self::FIXTURES . '/workload.cachegrind']);
$f = $p->functions;
self::assertArrayHasKey('fib', $f);
self::assertArrayHasKey('allocate', $f);
self::assertArrayHasKey('work', $f);
self::assertArrayHasKey('php::str_repeat', $f);
self::assertSame(200, $f['php::str_repeat']['calls']);
self::assertSame(67, $f['fib']['calls']); // fib(8): 66 recursive calls + the first
self::assertGreaterThan($f['fib']['self'], $f['fib']['incl'] + 1);
self::assertGreaterThan($f['allocate']['self'], $f['allocate']['incl']);
self::assertGreaterThan(200 * 100, $f['allocate']['imem']); // 200 strings of 100 bytes and more
self::assertGreaterThan($f['work']['self'], $f['{main}']['incl']);
self::assertGreaterThan(0, $p->total());
self::assertNotEmpty($p->tracked);
}
public function testAHugeFileIsStreamedInBoundedMemory(): void
{
// ~60 MB of cost lines of 50 functions: memory must stay far below the file's size
$f = tempnam(sys_get_temp_dir(), 'cg');
$this->temp[] = $f;
$h = fopen($f, 'wb');
fwrite($h, "version: 1\nevents: Time_(10ns) Memory_(bytes)\n\n");
for ($i = 0; $i < 50; $i++) {
fwrite($h, "fl=(1) /app/f.php\nfn=(" . ($i + 1) . ") fn$i\n");
for ($k = 0; $k < 20000; $k++) {
fwrite($h, 'cfl=(1)' . "\n" . 'cfn=(' . (($i + 1) % 50 + 1) . ') fn' . (($i + 1) % 50) . "\ncalls=1 0 0\n" . ($k % 500) . " 10 8\n" . ($k % 500) . " 3 1\n");
}
fwrite($h, "\n");
}
fclose($h);
self::assertGreaterThan(40_000_000, filesize($f));
$before = memory_get_usage();
$p = (new CachegrindParser())->parseAll([$f]);
$peak = memory_get_peak_usage() - $before;
self::assertLessThan(30_000_000, $peak, 'the parser holds the aggregates, not the file');
self::assertCount(50, $p->functions);
self::assertSame(20000, $p->functions['fn0']['calls']);
self::assertFalse($p->truncated);
}
public function testACapOnTheBytesTruncatesAndSaysSo(): void
{
$p = (new CachegrindParser(maxBytes: 300))->parseAll([$this->file(self::HAND)]);
self::assertTrue($p->truncated);
self::assertStringContainsString('cut after', $p->notes[0]);
}
}