Codeigniter benchmarking, where are these ms comin

2019-07-18 05:42发布

问题:

I'm in the process of benchmarking my website.

class Home extends Controller {

    function Home() 
    {
        parent::Controller();
        $this->benchmark->mark('Constructor_start');

        $this->output->enable_profiler(TRUE);
        $this->load->library ('MasterPage');

        $this->benchmark->mark('Constructor_end');
    }

    function index() 
    {
        $this->benchmark->mark('Index_start');

        $this->masterpage->setMasterPage('master/home');
        $this->masterpage->addContent('home/index', 'page');
        $this->masterpage->show();

        $this->benchmark->mark('Index_end');
    }
}

These are the results:

Loading Time Base Classes: 0.0076
Constructor: 0.0007
Index: 0.0440
Controller Execution Time ( Home/ Index ): 0.4467
Total Execution Time: 0.4545`

I understand the following:

  • Loading Time Base Classes (0.0076)
  • Constructor (0.0007)
  • Index (0.0440)

But where is the rest of the time coming from?

回答1:

I haven't done a lot of benchmarking of CI-powered sites, but 0.4545 doesn't seem very fast.

One thing that occurs under the umbrella of Controller Execution Time (but outside your custom defined benchmarks) is the autoloading of everything defined in the config/autoload.php file. If you're loading numerous libraries or models there, that would add to your Controller Execution Time without any immediately obvious reason.



回答2:

I think the last line of your index function should be

$this->benchmark->mark('Index_end');

This typo might have caused the funny looking results.



回答3:

Codeigniter automatically benchmarks the total time from the request to the final output is sent to the browser (this is the Total Execution Time)

Also, when it is about to call the controller/method you specify (usually through the url) it marks the start of that and then when that method returns something or sends data to the output it ends that benchmark ( this is the Controller Execution Time ( Home/ Index ) you are seeing)