154 lines
6.7 KiB
PHP
154 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests\Profile;
|
|
|
|
use JumPHP\Ide\Lang\Messages;
|
|
use JumPHP\Ide\Profile\TraceParser;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** Xdebug's function trace (computer-readable format): merged call tree, memory deltas, memory over time, slices. */
|
|
final class TraceParserTest 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
|
|
{
|
|
$f = tempnam(sys_get_temp_dir(), 'tr');
|
|
file_put_contents($f, $content);
|
|
$this->temp[] = $f;
|
|
return $f;
|
|
}
|
|
|
|
/** a trace: main (0..10 s) calls load twice (1..3 s, +1000 B; 4..9 s, +5000 B) and load calls read once each */
|
|
private const HAND = "Version: 3.5.3\nFile format: 4\nTRACE START [2026-01-01 00:00:00]\n"
|
|
. "1\t0\t0\t0.000000\t1000\t{main}\t1\t\t/app/main.php\t0\n"
|
|
. "2\t1\t0\t1.000000\t1000\tload\t1\t\t/app/main.php\t5\n"
|
|
. "3\t2\t0\t1.500000\t1200\tread\t0\t\t/app/main.php\t9\n"
|
|
. "3\t2\t1\t2.000000\t1700\n"
|
|
. "2\t1\t1\t3.000000\t2000\n"
|
|
. "2\t3\t0\t4.000000\t2000\tload\t1\t\t/app/main.php\t6\n"
|
|
. "3\t4\t0\t4.500000\t2500\tread\t0\t\t/app/main.php\t9\n"
|
|
. "3\t4\t1\t8.000000\t7000\n"
|
|
. "2\t3\t1\t9.000000\t7000\n"
|
|
. "1\t0\t1\t10.000000\t7000\n"
|
|
. "\t\t\t10.000000\t7000\n"
|
|
. "TRACE END [2026-01-01 00:00:10]\n";
|
|
|
|
public function testTheMergedCallTreeHasTimeCallsAndMemoryPerNode(): void
|
|
{
|
|
$t = (new TraceParser())->parseAll($this->file(self::HAND));
|
|
$main = $t->root->children['{main}'];
|
|
self::assertEqualsWithDelta(10.0, $main->time, 1e-9);
|
|
$load = $main->children['load'];
|
|
self::assertSame(2, $load->calls); // two calls merged into one node
|
|
self::assertEqualsWithDelta(2.0 + 5.0, $load->time, 1e-9);
|
|
self::assertEqualsWithDelta(1000.0 + 5000.0, $load->mem, 1e-9);
|
|
$read = $load->children['read'];
|
|
self::assertSame(2, $read->calls);
|
|
self::assertEqualsWithDelta(0.5 + 3.5, $read->time, 1e-9);
|
|
self::assertEqualsWithDelta(500.0 + 4500.0, $read->mem, 1e-9);
|
|
self::assertEqualsWithDelta(7000.0, $t->peak, 1e-9);
|
|
self::assertEqualsWithDelta(10.0, $t->duration, 1e-9);
|
|
}
|
|
|
|
public function testMemoryOverTimeIsDownsampledAndKeepsThePeak(): void
|
|
{
|
|
$t = (new TraceParser())->parseAll($this->file(self::HAND));
|
|
self::assertLessThanOrEqual(TraceParser::POINTS, count($t->timeline));
|
|
self::assertGreaterThan(100, count($t->timeline));
|
|
self::assertEqualsWithDelta(7000.0, max(array_column($t->timeline, 1)), 1e-9);
|
|
self::assertEqualsWithDelta(1000.0, $t->timeline[0][1], 1e-9);
|
|
self::assertEqualsWithDelta(2000.0, $t->timeline[(int)(3.5 / 10 * TraceParser::POINTS)][1], 1e-9); // between the two loads
|
|
}
|
|
|
|
public function testSlicesGiveEachFunctionItsOwnTimeAndMemoryOfThatPartOfTheRun(): void
|
|
{
|
|
$t = (new TraceParser())->parseAll($this->file(self::HAND));
|
|
self::assertCount(100, $t->slices === [] ? [] : array_fill(0, 100, 1));
|
|
// slice 0..9 = seconds 0..1: {main} runs alone; the second load (4..9) is in slices 40..89
|
|
$own = static function (array $slices, int $from, int $to, string $name): array {
|
|
$t = $m = 0.0;
|
|
for ($i = $from; $i <= $to; $i++) {
|
|
$t += $slices[$i][$name][0] ?? 0;
|
|
$m += $slices[$i][$name][1] ?? 0;
|
|
}
|
|
return [$t, $m];
|
|
};
|
|
[$mainT] = $own($t->slices, 0, 9, '{main}');
|
|
self::assertEqualsWithDelta(1.0, $mainT, 1e-6);
|
|
[$readT, $readM] = $own($t->slices, 45, 80, 'read');
|
|
self::assertEqualsWithDelta(3.5, $readT, 1e-6);
|
|
self::assertEqualsWithDelta(4500.0, $readM, 1e-6);
|
|
[$allT] = [array_sum(array_map(fn ($s) => array_sum(array_column($s, 0)), $t->slices))];
|
|
self::assertEqualsWithDelta(10.0, $allT, 1e-6); // every second of the run belongs to someone
|
|
}
|
|
|
|
public function testATruncatedTraceClosesTheOpenCalls(): void
|
|
{
|
|
$cut = implode("\n", array_slice(explode("\n", self::HAND), 0, 8)) . "\n";
|
|
$t = (new TraceParser())->parseAll($this->file($cut));
|
|
self::assertGreaterThan(0.0, $t->root->children['{main}']->time);
|
|
}
|
|
|
|
public function testACapOnTheBytesTruncatesAndSaysSo(): void
|
|
{
|
|
$t = (new TraceParser(maxBytes: 200))->parseAll($this->file(self::HAND));
|
|
self::assertTrue($t->truncated);
|
|
self::assertStringContainsString('cut after', $t->notes[0]);
|
|
}
|
|
|
|
public function testTheFramesOfATrackedFunctionAreTimedOneByOne(): void
|
|
{
|
|
$t = (new TraceParser(trackPattern: '/^load$/'))->parseAll($this->file(self::HAND));
|
|
self::assertCount(2, $t->tracked);
|
|
self::assertEqualsWithDelta(1.0, $t->tracked[0][0], 1e-9);
|
|
self::assertEqualsWithDelta(2.0, $t->tracked[0][1], 1e-9);
|
|
self::assertEqualsWithDelta(5.0, $t->tracked[1][1], 1e-9);
|
|
}
|
|
|
|
public function testARealXdebugTrace(): void
|
|
{
|
|
$t = (new TraceParser())->parseAll(self::FIXTURES . '/workload.xt');
|
|
$main = $t->root->children['{main}'];
|
|
self::assertSame(1, $main->calls);
|
|
self::assertArrayHasKey('allocate', $main->children);
|
|
self::assertArrayHasKey('fib', $main->children);
|
|
$alloc = $main->children['allocate'];
|
|
self::assertSame(200, $alloc->children['str_repeat']->calls);
|
|
self::assertGreaterThan(200 * 100, $alloc->mem); // it keeps 200 strings of 100 bytes
|
|
self::assertGreaterThan(0, $main->children['fib']->children['fib']->calls);
|
|
self::assertGreaterThan($alloc->mem, $t->peak);
|
|
}
|
|
|
|
public function testTheNodeCapFoldsTheDeepestCallsIntoTheirCaller(): void
|
|
{
|
|
$lines = "1\t0\t0\t0.0\t100\t{main}\t1\t\t/a.php\t0\n";
|
|
$time = 0.0;
|
|
for ($i = 0; $i < 50; $i++) {
|
|
$lines .= "2\t" . ($i + 1) . "\t0\t" . ($time += 0.001) . "\t100\tf$i\t1\t\t/a.php\t1\n2\t" . ($i + 1) . "\t1\t" . ($time += 0.001) . "\t100\n";
|
|
}
|
|
$lines .= "1\t0\t1\t" . ($time += 0.001) . "\t100\n\t\t\t$time\t100\n";
|
|
$t = new TraceParser();
|
|
$r = new \ReflectionProperty($t, 'nodes');
|
|
$r->setValue($t, TraceParser::MAX_NODES - 10);
|
|
$t->parseAll($this->file($lines));
|
|
self::assertTrue($t->nodesCapped);
|
|
self::assertCount(9, $t->root->children['{main}']->children);
|
|
self::assertEqualsWithDelta($time, $t->root->children['{main}']->time, 1e-9);
|
|
}
|
|
}
|