Skip to content

Commit

Permalink
Merge pull request #2 from Slach/master
Browse files Browse the repository at this point in the history
fix convertSampleDataToCommonFormat for properly count
  • Loading branch information
shagtv authored Mar 5, 2019
2 parents 02d2c55 + 77b5a7d commit d15026b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
composer.lock
*.log
28 changes: 14 additions & 14 deletions src/Badoo/LiveProfiler/LiveProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,22 @@ public function useXhprofSample()

protected function convertSampleDataToCommonFormat(array $sampling_data)
{
$result_data = [
'main()' => [
'ct' => 1,
'wt' => 0,
]
];
$result_data = [];
$prev_time = XHPROF_SAMPLING_BEGIN;
$prev_callstack = null;
foreach ($sampling_data as $time => $callstack) {
$wt = (int)(($time - $prev_time) * 1000000);
$functions = explode('==>', $callstack);
$prev_i = 0;
$main_key = $functions[$prev_i];
if (!isset($result_data[$main_key])) {
$result_data[$main_key] = [
'ct' => 0,
'wt' => 0,
];
}
$result_data[$main_key]['ct'] ++;
$result_data[$main_key]['wt'] += $wt;

$func_cnt = count($functions);
for ($i = 1; $i < $func_cnt; $i++) {
$key = $functions[$prev_i] . '==>' . $functions[$i];
Expand All @@ -225,16 +229,12 @@ protected function convertSampleDataToCommonFormat(array $sampling_data)
}

$result_data[$key]['wt'] += $wt;
if ($i === $func_cnt - 1) {
if ($callstack !== $prev_callstack) {
$result_data[$key]['ct']++;
}
$result_data['main()']['wt'] += $wt;
}
$result_data[$key]['ct']++;

$prev_i = $i;
}

$prev_time = $time;
$prev_callstack = $callstack;
}

return $result_data;
Expand Down
14 changes: 10 additions & 4 deletions tests/unit/Badoo/LiveProfiler/LiveProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,24 @@ public function testConvertSampleDataToCommonFormat()
define('XHPROF_SAMPLING_BEGIN', 1000);

$sampling_data = [
1001 => 'main()==>func'
1001 => 'main()==>func',
1002 => 'main()==>func==>func2',
1003 => 'main()'
];
$result = $this->invokeMethod($Profiler, 'convertSampleDataToCommonFormat', [$sampling_data]);
$expected = [
'main()' => [
'ct' => 1,
'wt' => 1000000
'ct' => 3,
'wt' => 3000000
],
'main()==>func' => [
'ct' => 2,
'wt' => 2000000
],
'func==>func2' => [
'ct' => 1,
'wt' => 1000000
],
]
];
self::assertEquals($expected, $result);
}
Expand Down

0 comments on commit d15026b

Please sign in to comment.