Skip to content

Commit 8032f77

Browse files
committed
speed up method usage page
1 parent 4891cbd commit 8032f77

File tree

10 files changed

+60
-11
lines changed

10 files changed

+60
-11
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
There are next changes:
66

7+
## 1.2.11
8+
9+
There are next changes:
10+
11+
- optimized a method usage page for huge tables
12+
- added a calls_count param to display calls count recorded during specified day
13+
714
## 1.2.10
815

916
There are next changes:

src/Badoo/LiveProfilerUI/DB/Validators/Functions.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Functions
1515
'sum',
1616
'date',
1717
'max',
18+
'min',
1819
];
1920

2021
public static function validate(string $function) : bool

src/Badoo/LiveProfilerUI/DataProviders/Interfaces/MethodDataInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public function getDataBySnapshotId(int $snapshot_id) : array;
2020
* @param int[] $snapshot_ids
2121
* @param int[] $method_ids
2222
* @param int $limit
23+
* @param int $start_snapshot_id
2324
* @return MethodData[]
2425
*/
25-
public function getDataByMethodIdsAndSnapshotIds(array $snapshot_ids, array $method_ids, int $limit = 0) : array;
26+
public function getDataByMethodIdsAndSnapshotIds(array $snapshot_ids, array $method_ids, int $limit = 0, int $start_snapshot_id = 0) : array;
2627
public function getOneParamDataBySnapshotIds(array $snapshot_ids, string $param, int $threshold = 1000) : array;
2728
public function deleteBySnapshotId(int $snapshot_id) : bool;
2829
public function insertMany(array $inserts) : bool;

src/Badoo/LiveProfilerUI/DataProviders/Interfaces/SnapshotInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function getOneByAppAndLabel(string $app, string$label) : Snapshot;
2222
public function getOneByAppAndLabelAndDate(string $app, string $label, string $date) : Snapshot;
2323
public function getSnapshotsByDates(array $dates, string $param = '') : array;
2424
public function getSnapshotIdsByDates(array $dates, string $app, string $label) : array;
25+
public function getMinSnapshotIdByDates(array $dates) : int;
2526
public function getOldSnapshots(int $keep_days = 200, int $limit = 2000) : array;
2627
public function getDatesByAppAndLabel(string $app, string $label) : array;
2728
public function getMaxCallsCntByAppAndLabel(string $app, string $label) : int;

src/Badoo/LiveProfilerUI/DataProviders/MethodData.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function getDataBySnapshotId(int $snapshot_id) : array
4040
public function getDataByMethodIdsAndSnapshotIds(
4141
array $snapshot_ids,
4242
array $method_ids,
43-
int $limit = 0
43+
int $limit = 0,
44+
int $start_snapshot_id = 0
4445
) : array {
4546
if (empty($snapshot_ids) && empty($method_ids)) {
4647
return [];
@@ -53,14 +54,17 @@ public function getDataByMethodIdsAndSnapshotIds(
5354
if (!empty($method_ids)) {
5455
$filters[] = ['method_id', $method_ids];
5556
}
57+
if ($start_snapshot_id) {
58+
$filters[] = ['snapshot_id', $start_snapshot_id, '>='];
59+
}
5660

5761
$records = $this->AggregatorStorage->getAll(
5862
self::TABLE_NAME,
5963
['all'],
6064
[
6165
'filter' => $filters,
6266
'order' => ['snapshot_id' => 'desc'],
63-
'limit' => $limit
67+
'limit' => $limit,
6468
]
6569
);
6670

src/Badoo/LiveProfilerUI/DataProviders/Snapshot.php

+19
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ public function getSnapshotIdsByDates(array $dates, string $app, string $label)
250250
return $result;
251251
}
252252

253+
public function getMinSnapshotIdByDates(array $dates) : int
254+
{
255+
if (empty($dates)) {
256+
return 0;
257+
}
258+
259+
$snapshot = $this->AggregatorStorage->getOne(
260+
self::TABLE_NAME,
261+
[['field' => 'id', 'function' => 'min']],
262+
[
263+
'filter' => [
264+
['date', $dates],
265+
],
266+
]
267+
);
268+
269+
return $snapshot ? (int)$snapshot['id'] : 0;
270+
}
271+
253272
public function getOldSnapshots(int $keep_days = 200, int $limit = 2000) : array
254273
{
255274
$snapshots = $this->AggregatorStorage->getAll(

src/Badoo/LiveProfilerUI/Pages/MethodUsagePage.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@ class MethodUsagePage extends BasePage
2525
protected $MethodData;
2626
/** @var FieldList */
2727
protected $FieldList;
28+
/** @var bool */
29+
protected $use_method_usage_optimisation = false;
2830

2931
public function __construct(
3032
View $View,
3133
SnapshotInterface $Snapshot,
3234
MethodInterface $Method,
3335
MethodDataInterface $MethodData,
34-
FieldList $FieldList
36+
FieldList $FieldList,
37+
bool $use_method_usage_optimisation = false
3538
) {
3639
$this->View = $View;
3740
$this->Snapshot = $Snapshot;
3841
$this->Method = $Method;
3942
$this->MethodData = $MethodData;
4043
$this->FieldList = $FieldList;
44+
$this->use_method_usage_optimisation = $use_method_usage_optimisation;
4145
}
4246

4347
protected function cleanData() : bool
@@ -73,10 +77,19 @@ public function getTemplateData() : array
7377

7478
$results = [];
7579
if (!empty($methods)) {
80+
$start_snapshot_id = 0;
81+
if ($this->use_method_usage_optimisation) {
82+
$last_two_days = \Badoo\LiveProfilerUI\DateGenerator::getDatesArray(date('Y-m-d'), 2, 2);
83+
$start_snapshot_id = in_array(current($methods)['date'], $last_two_days, true)
84+
? $this->Snapshot->getMinSnapshotIdByDates($last_two_days)
85+
: 0;
86+
}
87+
7688
$method_data = $this->MethodData->getDataByMethodIdsAndSnapshotIds(
7789
[],
7890
array_keys($methods),
79-
100
91+
200,
92+
$start_snapshot_id
8093
);
8194

8295
$snapshot_ids = [];
@@ -97,6 +110,7 @@ public function getTemplateData() : array
97110
foreach ($fields as $field) {
98111
$result['fields'][$field] = $values[$field];
99112
}
113+
$result['fields']['calls_count'] = $snapshots[$Row->getSnapshotId()]['calls_count'];
100114
$results[] = $result;
101115
}
102116
}

src/config/services.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ parameters:
88
aggregator.log_file: ''
99
aggregator.minimum_profiles_count: 0
1010
aggregator.aggregating_jobs_lock_file: /app/db_data/aggregator_processor.lock
11+
aggregator.use_method_usage_optimisation: false # returns only last day usage if method run for last 2 days
1112

1213
aggregator.fields_descriptions:
13-
ct: Average calls count in this context during specified day (µs)
14+
calls_count: Calls count recorded during specified day
15+
ct: Average calls count in this context during specified day
1416
min_ct: Minimum calls count in this context during specified day (µs)
1517
max_ct: Maximim calls count in this context during specified day (µs)
1618
percent_ct: 90 percentile of calls count in this context during specified day (µs)
@@ -93,7 +95,7 @@ services:
9395

9496
method_usage_page:
9597
class: \Badoo\LiveProfilerUI\Pages\MethodUsagePage
96-
arguments: ['@view', '@snapshot', '@method', '@method_data', '@fields']
98+
arguments: ['@view', '@snapshot', '@method', '@method_data', '@fields', '%aggregator.use_method_usage_optimisation%']
9799

98100
top_diff_page:
99101
class: \Badoo\LiveProfilerUI\Pages\TopDiffPage

src/templates/method_usage.php

-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<tr>
3333
<th class="sorter-false" style="width: 20px;">#</th>
3434
<th class="sorter-text">date</th>
35-
<th>method</th>
3635
<th>label</th>
3736
<th>app</th>
3837
<?php foreach ($data['results'][0]['fields'] as $field_name => $field_value) { ?>
@@ -53,7 +52,6 @@
5352
</a>
5453
</td>
5554
<td><?= $result['date'] ?></td>
56-
<td><?= $result['method_name'] ?></td>
5755
<td><?= $result['label'] ?></td>
5856
<td><?= $result['app'] ?></td>
5957
<?php foreach ($result['fields'] as $field) { ?>

tests/unit/Badoo/LiveProfilerUI/Pages/MethodUsagePageTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function providerGetTemplateData()
5959
'label' => 'label',
6060
'fields' => [
6161
'ct' => 1,
62-
'wt' => 1
62+
'wt' => 1,
63+
'calls_count' => 1
6364
]
6465
]
6566
],
@@ -92,7 +93,8 @@ public function testGetTemplateData($method_name, $found_methods, $methods_data,
9293
'id' => 1,
9394
'app' => 'app',
9495
'label' => 'label',
95-
'date' => 'date'
96+
'date' => 'date',
97+
'calls_count' => 1
9698
];
9799
$SnapshotMock = $this->getMockBuilder(\Badoo\LiveProfilerUI\DataProviders\Snapshot::class)
98100
->disableOriginalConstructor()

0 commit comments

Comments
 (0)