Scott Keck-Warren
Scott Keck-Warren

Reputation: 1952

Track page load time and total query count in Zend Framework

Is there a way to get a count of the total number of queries that are run on a single page and how long it took to generate that page in the Zend Framework?

We want to be able to log this information so we can see where we have bottlenecks and pages that require a large amount of resources to generate.

Thanks,

Upvotes: 0

Views: 2383

Answers (4)

erickthered
erickthered

Reputation: 901

Sure there is a Db Profiling Tool available, it's call Zend_Db_Profiler and you can find out how to use it by reading the official documentation here: http://framework.zend.com/manual/en/zend.db.profiler.html#zend.db.profiler.using

Basically, you have to do something like this:

$db = Zend_Db_Table::getDefaultAdapter();
$profiler   = $db->getProfiler();
$totalTime  = $profiler->getTotalElapsedSecs();
$queryCount = $profiler->getTotalNumQueries();

As for the total load time of your page, if your are not using layout(s), then it's better to create a base controller class that gets the current (micro)time during predispatch and postDispatch methods, then you make it the base class for all your controllers. Perhaps something like this may do the job:

class Myapp_Controller_Action extends Zend_Controller_Action
{
  protected $_startTime;
  protected $_endTime;
  protected $_totalTime;

  public function preDispatch()
  {
    parent::preDispatch();
    $this->_startTime = microtime(true);
  }

  public function postDispatch()
  {
    parent::postDispatch();
    $this->_endTime = microtime(true);
    $this->_totalTime = $this->_endTime - $this->_startTime;
    // do something with $this->_totalTime;
  }
}

And every controller in your application should extend Myapp_Controller_Action instead of Zend_Controller_Action:

class IndexController extends Myapp_Controller_Action
{
  ...
}

Upvotes: 3

theAndroid
theAndroid

Reputation: 854

Additionally, here is an advice for debugging. Of course, it won't apply for logging. You can use Zend_Db_Profiler + FireBug + FirePhp with Firefox which are really really nice together : The configuration should be (I'm using .ini format but can be adapted by code or XML) :

database.params.profiler.enabled = true
database.params.profiler.class = Zend_Db_Profiler_Firebug

You'll get this kind of result : total time spent in your database(s) + query details and time spent (and optional parameters). Great every day use tools !

enter image description here

Upvotes: 2

Adam Shiemke
Adam Shiemke

Reputation: 3742

You can hook into the pre and post dispatch hooks for a plugin, and log the timestamps in a DB table. On post, you can look for the corresponding pre-dispatch stamp and calculate the total.

For queries, the easiest way would probably be to add a wrapper to whatever you are using to make the queries and increment a static counter on the wrapper class every time the query method is called. In the post dispatch hook, you could log this to the DB as well.

For writing plugins to sue pre/post dispatch hooks, look at http://devzone.zend.com/1224/front-controller-plugins-in-zend-framework/

Good luck.

Upvotes: 0

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22162

To see the queries, you can enable the profiler of your database adapter. To do this, you have to write the following line that should be at the beginning of your software:

$Db->getProfiler()->setEnabled(TRUE);

Of course $Db should be an instance of your database adapter. To retrieve all the queries you can do:

$Db->getProfiler()->getQueryProfiles()

Of course you can read more about profiler from the official doc.

Upvotes: 0

Related Questions