srinivt
srinivt

Reputation: 153

PHP - measuring database time for a HTTP request

I want to find how much time a PHP http request spends in Database. Rails logger prints out such information (how much time is spent on rendering, database and app). Example:

Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]

Is there something similar for PHP too?

Thanks in advance.

Upvotes: 1

Views: 2315

Answers (4)

Xeoncross
Xeoncross

Reputation: 57184

You can use microtime() and memory_get_usage() to test the speed as well as the memory used (which is often more important with ORM's).

$time = microtime(TRUE);
$memory = memory_get_usage();

...code here...

print (microtime(TRUE)-$time). ' seconds and '. (memory_get_usage()-$memory). ' bytes';

However, database queries should be benchmarked at the database level. Use MySQL Query Profiler for real benchmarking.

Upvotes: 3

Dan Grossman
Dan Grossman

Reputation: 52372

Yes, PHP has frameworks (Rails is a framework written in Ruby, PHP is a language) that have built in logging of how much time is spent on things like that. Look at Symfony, Kohana, Zend Framework, CodeIgniter.

Upvotes: 0

john mossel
john mossel

Reputation: 2156

Check out the PHP function microtime() http://php.net/manual/en/function.microtime.php I think this is what you're looking for, though I'm not sure.

Upvotes: 1

Xiao
Xiao

Reputation: 108

You can use microtime to get the time before the query then subtract it from the time when the query finishes. http://php.net/manual/en/function.microtime.php

Upvotes: 1

Related Questions