Last active
May 3, 2024 12:36
-
-
Save derickr/f30bc04c394e89573071171459f6a9d4 to your computer and use it in GitHub Desktop.
Revisions
-
derickr revised this gist
May 3, 2024 . No changes.There are no files selected for viewing
-
derickr created this gist
Dec 20, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ <?php /** 1 00:00:03,600 --> 00:00:09,740 Hello, today we're going to have a look at how to make a flame graph, a new feature 2 00:00:09,740 --> 00:00:11,920 in Xdebug 3.3. 3 00:00:12,620 --> 00:00:16,920 Flame graphs are an interesting way of showing where an application spends a lot of time. */ $f = json_decode( file_get_contents( $argv[1] ) ); $segmentCounter = 0; $segmentText = ''; $segmentLength = 0; $segmentStart = 0; $lastStart = 0; $lastEnd = 0; foreach ( $f->segments as $segment ) { foreach ( $segment->words as $word ) { if ($word->start > $lastEnd + 0.25) { $segmentEnd = $lastEnd; emit(); $segmentStart = $word->start; } if ($segmentLength > 60) { $segmentText = trim($segmentText) . "\n" . ltrim($word->word); $segmentLength = strlen($word->word); } else { $segmentText .= $word->word; $segmentLength += strlen($word->word); } $lastEnd = $word->end; } } $segmentEnd = $lastEnd; emit(); function emit() { global $segmentCounter, $segmentStart, $segmentEnd, $segmentText, $segmentLength; $secondsStart = (int) $segmentStart; $milliSecondsStart = (int) (($segmentStart - $secondsStart) * 1000); $secondsEnd = (int) $segmentEnd; $milliSecondsEnd = (int) (($segmentEnd - $secondsEnd) * 1000); $segmentStartFmt = sprintf("%02d:%02d:%02d,%03d", $secondsStart / 3600, $secondsStart / 60, $secondsStart % 60, $milliSecondsStart); $segmentEndFmt = sprintf("%02d:%02d:%02d,%03d", $secondsEnd / 3600, $secondsEnd / 60, $secondsEnd % 60, $milliSecondsEnd); if (strlen($segmentText) == 0) { return; } printf("%s\n%s --> %s\n%s\n\n", $segmentCounter, $segmentStartFmt, $segmentEndFmt, trim($segmentText)); $segmentText = ''; $segmentLength = 0; $segmentCounter++; }