phpperformancecakephpcakephp-2.1execution-time

Calculate execution time of pagination in Cakephp


I am trying to optimize my query for a page with pagination results in Cakephp and therefore I am looking to calculate the exact time with the PHP function microtime(). That way, I will know better how much time it's taking to execute the certain request.

Moreover, I am trying to cache the results of the pagination through Cache::read and Cache::write, which both are internal Cakephp functions as regards caching methods in Cakephp 2.x.

Thus, what I have at this point is this: I am thinking that in order to calculate exactly the whole timelapse, should I put the rest of the code inside the while loop ?

Any help would be quite appreciating. Thanks

        $start = microtime(true);
        while ($start) {
            $this->paginate = $options;
            $resultado = $this->paginate('Doctor');  
        }
        $time_elapsed_secs = microtime(true) - $start;
        pr($time_elapsed_secs);
        foreach ($resultado AS $k => $r) {
            $hasProduct = Cache::read(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS . '_' . $r['Doctor']['id'], '1day');
            if (empty($hasProduct)) {
                $hasProduct = $this->DoctorsProduct->find('all', [
                    'recursive' => -1,
                    'fields' => ['Product.*', 'DoctorsProduct.*'],
                    'joins' => [
                        ['table' => 'td_products',
                            'alias' => 'Product',
                            'type' => 'INNER',
                            'conditions' => [
                                'Product.id = DoctorsProduct.id_product'
                            ]
                        ]
                    ],
                    'conditions' => [
                        'id_doctor' => $r['Doctor']['id'],
                        'DoctorsProduct.status' => 1
                    ],
                    'order' => [
                        'Product.id ASC',
                    ]
                ]);
                Cache::write(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS, $hasProduct, '1day');
            }
            $resultado[$k]['Products'] = $hasProduct;
            $resultado[$k]['Article'] = 0;
        }

Solution

  • Well, the approach which I posted it is indeed wrong, that would possibly just loop infinitely. Finally, what I found as a solution to the question would be to just put the microtime function at the very beginning. I declare a new variable as $time.

    After this, I am just substracting the actual microtime with the time which was declared beforewards. Works like a charm.

            $time = microtime( TRUE );
            foreach ($resultado AS $k => $r) {
                $hasProduct = Cache::read(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS . '_' . $r['Doctor']['id'], '1day');
                if (empty($hasProduct)) {
                    $hasProduct = $this->DoctorsProduct->find('all', [
                        'fields' => ['Product.*', 'DoctorsProduct.*'],
                        'joins' => [
                            ['table' => 'td_products',
                                'alias' => 'Product',
                                'type' => 'INNER',
                                'conditions' => [
                                    'Product.id = DoctorsProduct.id_product'
                                ]
                        ],
                        ],
                        'conditions' => [
                            'id_doctor' => $r['Doctor']['id'],
                            'DoctorsProduct.status' => 1
                        ],
                        'order' => [
                            'Product.id ASC',
                        ]
                    ]);
                    Cache::write(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS, 
                $hasProduct, '1day');
                }
                $resultado[$k]['Products'] = $hasProduct;
                $resultado[$k]['Article'] = 0;
            }
            $time = microtime( TRUE ) - $time;
            echo $time;
            die("123");
    

    enter image description here