Profiling user requests

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.


1 0
Viewed: 9 115 times
Version: 1.1
Category: Tutorials
Written by: M0ka
Last updated by: M0ka
Created on: May 19, 2014
Last updated: 9 years ago
Update Article


View all history

Related Articles