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 renderi开发者_StackOverflow中文版ng, 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.
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.
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
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.
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.
精彩评论