LoginSignup
12
10

More than 5 years have passed since last update.

CakePHP3で処理の実行時間を計測する簡単な方法

Last updated at Posted at 2016-08-23

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パネルを確認してみてください。

ScrenCaptured 2016-08-23 10.36.31.png

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

楽ー。

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10