Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

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

楽ー。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away