Skip to content

Commit

Permalink
Reuse frames in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Jul 14, 2023
1 parent c965f55 commit 5ce4cfe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
25 changes: 18 additions & 7 deletions src/Profiling/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,24 @@ public function getFormattedData(Event $event): ?array
}

$frames = [];
$frameIndex = 0;
$frameHashMap = [];

$registerFrame = static function (array $frame) use (&$frames, &$frameHashMap, &$frameIndex): int {
$frameHash = md5(serialize($frame));

if (false === \array_key_exists($frameHash, $frameHashMap)) {
$frames[] = $frame;
$frameHashMap[$frameHash] = $frameIndex;
++$frameIndex;
}

return $frameHashMap[$frameHash];
};

$samples = [];
$stacks = [];

$frameIndex = 0;
$duration = 0;

$loggedStacks = $this->prepareStacks();
Expand All @@ -176,24 +190,21 @@ public function getFormattedData(Event $event): ?array
$function = $file;
}

$frames[] = [
$stacks[$stackId][] = $registerFrame([
'filename' => $file,
'abs_path' => $absolutePath,
'module' => $module,
'function' => $function,
'lineno' => !empty($frame['line']) ? (int) $frame['line'] : null,
];

$stacks[$stackId][] = $frameIndex;
++$frameIndex;
]);
}

$duration = $stack['timestamp'];

$samples[] = [
'elapsed_since_start_ns' => (int) round($duration * 1e+9),
'stack_id' => $stackId,
'thread_id' => self::THREAD_ID,
'elapsed_since_start_ns' => (int) round($duration * 1e+9),
];
}

Expand Down
35 changes: 7 additions & 28 deletions tests/Profiling/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,6 @@ public static function formattedDataDataProvider(): \Generator
'version' => '1',
'profile' => [
'frames' => [
[
'filename' => '/var/www/html/index.php',
'abs_path' => '/var/www/html/index.php',
'module' => null,
'function' => '/var/www/html/index.php',
'lineno' => 42,
],
[
'filename' => '/var/www/html/index.php',
'abs_path' => '/var/www/html/index.php',
'module' => null,
'function' => '/var/www/html/index.php',
'lineno' => 42,
],
[
'filename' => '/var/www/html/function.php',
'abs_path' => '/var/www/html/function.php',
'module' => 'Function',
'function' => 'Function::doStuff',
'lineno' => 84,
],
[
'filename' => '/var/www/html/index.php',
'abs_path' => '/var/www/html/index.php',
Expand Down Expand Up @@ -183,34 +162,34 @@ public static function formattedDataDataProvider(): \Generator
],
'samples' => [
[
'elapsed_since_start_ns' => 1000000,
'stack_id' => 0,
'thread_id' => '0',
'elapsed_since_start_ns' => 1000000,
],
[
'elapsed_since_start_ns' => 2000000,
'stack_id' => 1,
'thread_id' => '0',
'elapsed_since_start_ns' => 2000000,
],
[
'elapsed_since_start_ns' => 3000000,
'stack_id' => 2,
'thread_id' => '0',
'elapsed_since_start_ns' => 3000000,
],
],
'stacks' => [
[
0,
],
[
0,
1,
2,
],
[
0,
1,
2,
3,
4,
5,
6,
],
],
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Serializer/PayloadSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public static function serializeAsJsonDataProvider(): iterable
{"type":"transaction","content_type":"application\/json"}
{"event_id":"fc9442f5aef34234bb22b9a615e30ccd","timestamp":1597790835,"platform":"php","sdk":{"name":"sentry.php","version":"$sdkVersion"},"transaction":"GET \/","release":"1.0.0","environment":"dev","contexts":{"os":{"name":"macOS","version":"13.2.1","build":"22D68","kernel_version":"Darwin Kernel Version 22.2.0"},"runtime":{"name":"php","version":"8.2.3"},"trace":{"trace_id":"21160e9b836d479f81611368b2aa3d2c","span_id":"5dd538dc297544cc"}},"spans":[{"span_id":"5dd538dc297544cc","trace_id":"21160e9b836d479f81611368b2aa3d2c","start_timestamp":1597790835},{"span_id":"b01b9f6349558cd1","trace_id":"1e57b752bc6e4544bbaa246cd1d05dee","start_timestamp":1597790835,"parent_span_id":"b0e6f15b45c36b12","timestamp":1598659060,"status":"ok","description":"GET \/sockjs-node\/info","op":"http","data":{"url":"http:\/\/localhost:8080\/sockjs-node\/info?t=1588601703755","status_code":200,"type":"xhr","method":"GET"},"tags":{"http.status_code":"200"}}]}
{"type":"profile","content_type":"application\/json"}
{"device":{"architecture":"aarch64"},"event_id":"fc9442f5aef34234bb22b9a615e30ccd","os":{"name":"macOS","version":"13.2.1","build_number":"22D68"},"platform":"php","release":"1.0.0","environment":"dev","runtime":{"name":"php","version":"8.2.3"},"timestamp":"2023-02-28T08:41:00.000+00:00","transaction":{"id":"fc9442f5aef34234bb22b9a615e30ccd","name":"GET \/","trace_id":"21160e9b836d479f81611368b2aa3d2c","active_thread_id":"0"},"version":"1","profile":{"frames":[{"filename":"\/var\/www\/html\/index.php","abs_path":"\/var\/www\/html\/index.php","module":null,"function":"\/var\/www\/html\/index.php","lineno":42},{"filename":"\/var\/www\/html\/index.php","abs_path":"\/var\/www\/html\/index.php","module":null,"function":"\/var\/www\/html\/index.php","lineno":42},{"filename":"\/var\/www\/html\/function.php","abs_path":"\/var\/www\/html\/function.php","module":"Function","function":"Function::doStuff","lineno":84}],"samples":[{"elapsed_since_start_ns":1000000,"stack_id":0,"thread_id":"0"},{"elapsed_since_start_ns":2000000,"stack_id":1,"thread_id":"0"}],"stacks":[[0],[1,2]]}}
{"device":{"architecture":"aarch64"},"event_id":"fc9442f5aef34234bb22b9a615e30ccd","os":{"name":"macOS","version":"13.2.1","build_number":"22D68"},"platform":"php","release":"1.0.0","environment":"dev","runtime":{"name":"php","version":"8.2.3"},"timestamp":"2023-02-28T08:41:00.000+00:00","transaction":{"id":"fc9442f5aef34234bb22b9a615e30ccd","name":"GET \/","trace_id":"21160e9b836d479f81611368b2aa3d2c","active_thread_id":"0"},"version":"1","profile":{"frames":[{"filename":"\/var\/www\/html\/index.php","abs_path":"\/var\/www\/html\/index.php","module":null,"function":"\/var\/www\/html\/index.php","lineno":42},{"filename":"\/var\/www\/html\/function.php","abs_path":"\/var\/www\/html\/function.php","module":"Function","function":"Function::doStuff","lineno":84}],"samples":[{"stack_id":0,"thread_id":"0","elapsed_since_start_ns":1000000},{"stack_id":1,"thread_id":"0","elapsed_since_start_ns":2000000}],"stacks":[[0],[0,1]]}}
TEXT
,
false,
Expand Down Expand Up @@ -891,7 +891,7 @@ public static function serializeAsEnvelopeDataProvider(): iterable
{"type":"transaction","content_type":"application\/json"}
{"event_id":"fc9442f5aef34234bb22b9a615e30ccd","timestamp":1597790835,"platform":"php","sdk":{"name":"sentry.php","version":"$sdkVersion"},"transaction":"GET \/","release":"1.0.0","environment":"dev","contexts":{"os":{"name":"macOS","version":"13.2.1","build":"22D68","kernel_version":"Darwin Kernel Version 22.2.0"},"runtime":{"name":"php","version":"8.2.3"},"trace":{"trace_id":"21160e9b836d479f81611368b2aa3d2c","span_id":"5dd538dc297544cc"}},"spans":[{"span_id":"5dd538dc297544cc","trace_id":"21160e9b836d479f81611368b2aa3d2c","start_timestamp":1597790835},{"span_id":"b01b9f6349558cd1","trace_id":"1e57b752bc6e4544bbaa246cd1d05dee","start_timestamp":1597790835,"parent_span_id":"b0e6f15b45c36b12","timestamp":1598659060,"status":"ok","description":"GET \/sockjs-node\/info","op":"http","data":{"url":"http:\/\/localhost:8080\/sockjs-node\/info?t=1588601703755","status_code":200,"type":"xhr","method":"GET"},"tags":{"http.status_code":"200"}}]}
{"type":"profile","content_type":"application\/json"}
{"device":{"architecture":"aarch64"},"event_id":"fc9442f5aef34234bb22b9a615e30ccd","os":{"name":"macOS","version":"13.2.1","build_number":"22D68"},"platform":"php","release":"1.0.0","environment":"dev","runtime":{"name":"php","version":"8.2.3"},"timestamp":"2023-02-28T08:41:00.000+00:00","transaction":{"id":"fc9442f5aef34234bb22b9a615e30ccd","name":"GET \/","trace_id":"21160e9b836d479f81611368b2aa3d2c","active_thread_id":"0"},"version":"1","profile":{"frames":[{"filename":"\/var\/www\/html\/index.php","abs_path":"\/var\/www\/html\/index.php","module":null,"function":"\/var\/www\/html\/index.php","lineno":42},{"filename":"\/var\/www\/html\/index.php","abs_path":"\/var\/www\/html\/index.php","module":null,"function":"\/var\/www\/html\/index.php","lineno":42},{"filename":"\/var\/www\/html\/function.php","abs_path":"\/var\/www\/html\/function.php","module":"Function","function":"Function::doStuff","lineno":84}],"samples":[{"elapsed_since_start_ns":1000000,"stack_id":0,"thread_id":"0"},{"elapsed_since_start_ns":2000000,"stack_id":1,"thread_id":"0"}],"stacks":[[0],[1,2]]}}
{"device":{"architecture":"aarch64"},"event_id":"fc9442f5aef34234bb22b9a615e30ccd","os":{"name":"macOS","version":"13.2.1","build_number":"22D68"},"platform":"php","release":"1.0.0","environment":"dev","runtime":{"name":"php","version":"8.2.3"},"timestamp":"2023-02-28T08:41:00.000+00:00","transaction":{"id":"fc9442f5aef34234bb22b9a615e30ccd","name":"GET \/","trace_id":"21160e9b836d479f81611368b2aa3d2c","active_thread_id":"0"},"version":"1","profile":{"frames":[{"filename":"\/var\/www\/html\/index.php","abs_path":"\/var\/www\/html\/index.php","module":null,"function":"\/var\/www\/html\/index.php","lineno":42},{"filename":"\/var\/www\/html\/function.php","abs_path":"\/var\/www\/html\/function.php","module":"Function","function":"Function::doStuff","lineno":84}],"samples":[{"stack_id":0,"thread_id":"0","elapsed_since_start_ns":1000000},{"stack_id":1,"thread_id":"0","elapsed_since_start_ns":2000000}],"stacks":[[0],[0,1]]}}
TEXT
,
];
Expand Down

0 comments on commit 5ce4cfe

Please sign in to comment.