Skip to content

Instantly share code, notes, and snippets.

@aiphee
Last active January 16, 2020 20:23
Show Gist options
  • Save aiphee/8004486cbd37b3f13efd271b8457cb38 to your computer and use it in GitHub Desktop.
Save aiphee/8004486cbd37b3f13efd271b8457cb38 to your computer and use it in GitHub Desktop.
Cakephp save execution time
CREATE TABLE `performance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`requestTime` float unsigned NOT NULL,
`memory` int(10) unsigned DEFAULT NULL,
`time` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18249 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_czech_ci
----
SELECT * FROM (
SELECT AVG(`requestTime`) AS `took`, MAX(`requestTime`) AS `tookmax`, DATE_FORMAT( FROM_UNIXTIME(`time`), '%Y-%m-%d %H' ) as date1, COUNT(*) AS requests
FROM `performance`
GROUP BY date1
ORDER BY id ASC
) t1
JOIN
(SELECT DATE_FORMAT( FROM_UNIXTIME(`time`), '%Y-%m-%d %H' ) as date2, COUNT(*) AS oversecond
FROM `performance`
WHERE requestTime > 1
GROUP BY date2
ORDER BY id ASC) t2 ON t1.`date1` = t2.`date2`
<?php
//...
public function shutdownProcess()
{
$result = parent::shutdownProcess();
try {
$this->savePerformance();
} catch (\Throwable $e) {
$this->log('Nepovedlo se uložit výkon', LogLevel::WARNING);
try {
apc_clear_cache('user');
} catch (\Throwable $e) {}
}
return $result;
}
private function savePerformance(): void
{
apc_add('counter', 0);
apc_inc('counter');
$requestNum = apc_fetch('counter');
//$dbTime = $db->getLogger()->totalTime();// Nejde bez debugu
$requestTime = DebugTimer::requestTime();
$memory = DebugMemory::getAll()['Controller shutdown'];
$time = time();
$toStore = compact( 'requestTime', 'memory', 'time');
apc_store('performance' . $requestNum, $toStore);
if ($requestNum > 20) { // Save every 21th request
$db = ConnectionManager::get('default');
$iterator = new \APCIterator('user');
$data = iterator_to_array($iterator);
apc_clear_cache('user');
unset($data['counter']);
$query = $db->newQuery()
->insert(['requestTime', 'memory', 'time'], [])
->into('performance');
foreach ($data as $row) {
$query->values([
'requestTime' => $row['value']['requestTime'],
'memory' => $row['value']['memory'],
'time' => $row['value']['time']
]);
}
$query->execute();
}
}
// ...
took tookmax date1 requests date2 oversecond
0.14548317245815112 1.77771 2019-12-13 13 23 2019-12-13 13 1
0.42955825442359563 7.6268 2019-12-13 17 42 2019-12-13 17 4
0.22201940108989848 2.6693 2019-12-13 19 29 2019-12-13 19 2
0.6334046125411987 1.11831 2019-12-13 18 2 2019-12-13 18 1
0.1959672655378069 1.29612 2019-12-14 01 14 2019-12-14 01 1
0.35073335965474445 1.42949 2019-12-14 02 12 2019-12-14 02 1
0.11388687789440155 1.24261 2019-12-15 17 16 2019-12-15 17 1
0.37757340158735003 6.02301 2019-12-15 19 35 2019-12-15 19 3
0.30702126026153564 4.89602 2019-12-16 22 30 2019-12-16 22 2
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = pd.read_csv('data2.csv')
fig = make_subplots(rows=3, cols=1)
fig.add_trace(
go.Scatter(
x=df['date1'],
y=df['took'],
name='Čas v sekundách průměrně'
),
row=1,
col=1
)
fig.add_trace(
go.Scatter(
x=df['date1'],
y=df['tookmax'],
name='Čas v sekundách maximálně'
),
row=1,
col=1
)
fig.add_trace(
go.Scatter(
x=df['date1'],
y=df['requests'],
name='Počet požadavků'
),
row=2,
col=1
)
fig.add_trace(
go.Scatter(
x=df['date1'],
y=df['oversecond'],
name='Requestů přes sekundu'
),
row=3,
col=1
)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment