CakePHP3で処理の実行時間を計測する簡単な方法
どこに時間がかかっているのか、各処理の実行時間を計測したい時、ありますよね?
そういうとき、いろいろな計測方法があると思いますが、CakePHP3で、手っ取り早く計測したいときは \DebugKit\DebugTimer
を使うと楽です。
計測コードの設置
\DebugKit\DebugTimer::start()
と \DebugKit\DebugTimer::stop()
を差し込みます。第1引数がキーになっているので合わせてください。
<?php
-snip-
public function edit($id = null)
{
\DebugKit\DebugTimer::start('post_get', 'データ取得'); // 計測スタート
$post = $this->Posts->get($id, [
'contain' => ['Tags']
]);
\DebugKit\DebugTimer::stop('post_get'); // 計測終了
if ($this->request->is(['patch', 'post', 'put'])) {
$post = $this->Posts->patchEntity($post, $this->request->data);
if ($this->Posts->save($post)) {
$this->Flash->success(__('The post has been saved.'));
- snip -
実行時間の確認
DebugKitのTimerパネルを確認してみてください。

「データ取得」という行が増えていますね。
入れ子での計測も可能です。
楽ー。