How to profile user requests so you can determine which actions need an enhancements and optimizations? This wiki going to show you a step by step how to do it.
STEP 1 /config/main.php ¶
We are going to attach a static functions with the event onBeginRequest and onEndRequest
...
'onBeginRequest' => array(
'Auditor', 'starter'
),
'onEndRequest' => array(
'Auditor', 'ender'
),
...
STEP 2 /application/components/Auditor.php ¶
Create a class called Auditor and place it in the components folder of the application, this class will have only static functions to be called by the application.
class Auditor {
private static $start;
public static function starter(CEvent $event) {
self::$start = microtime(true);
}
public static function bytesToKb($bytes) {
return $bytes * 1024;
}
public static function bytesToMb($bytes) {
return self::bytesToKb($bytes) * 1024;
}
public static function ender(CEvent $event) {
$memory = floatval(memory_get_usage(true));
$peak = floatval(memory_get_peak_usage(true));
$error = Yii::app()->getErrorHandler()->error;
$param = array(
'request_memory_kb' => self::bytesToKb($memory),
'request_memory_mb' => self::bytesToMb($memory),
'request_memory_peak_kb' => self::bytesToKb($peak),
'request_memory_peak_mb' => self::bytesToMb($peak),
'request_url' => Yii::app()->request->requestUri,
'request_type' => Yii::app()->request->requestType,
'request_errors' => $error,
'user_status' => Yii::app()->user->isGuest ? 'Guest' : Yii::app()->user->id,
'user_ip' => Yii::app()->request->userHostAddress,
'date' => time(),
'time_taken' => microtime(true) - self::$start
//or you can minus the current microtime from the constant YII_BEGIN_TIME
);
}
}
What I've done here is, the static parameter $start will contain the initial microtime when the function starter fire at onBeginRequest event. At the end of the request (onEndRequest), the function ender will start, it will calculate most of the needed information about the request.
$memory => the amount of memory in bytes, that's currently being allocated.
$peak => the peak of memory in bytes that's been allocated.
$error => the errors "if there is any"
I've also stored the URL the user was requesting to know exactly which action needs the optimization.
The request type, its important to know.
The user status, if the user is logged in or not, I've added this since there is a different behaviors for the logged in user in my project.
The user_ip and date are extra needed information.
The time_taken, describes how many micro seconds the Application took to execute the request.
Now we are done, you can store these values to a database or a log file to analyze.
Cheers!
nice tip. suggestion
heyy nice tip but looks like you forgot to add the Helper class to do the wiki, thus, was referenced on this line Helper::bytesToKb($memory). I created two methods and changed all Helper:: to self::
public static function bytesToKb($memory) { return 1024*$memory; } public static function bytesToMb($memory) { return 1024 * 1024 * $memory; }
XDEBUG
For fine-grained profiling, I use XDEBUG:
http://www.yiiframework.com/wiki/487/profiling-using-xdebug
XDEBUG
Thanks selorm for the note :) I was sharing it from a project am working on, I will update the Wiki now.
@le_top
Thank you for sharing the extension. Sure that XDEBUG is more advanced tool, but I have shared this wiki just for the simple usage and the ease of setup :)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.