Yii 1.1: 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.


Total 3 comments

#17506 report it
M0ka at 2014/06/24 06:55am

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 :)

#17499 report it
le_top at 2014/06/23 05:44pm

For fine-grained profiling, I use XDEBUG: http://www.yiiframework.com/wiki/487/profiling-using-xdebug

#17284 report it
selorm at 2014/05/19 11:53am
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;

Leave a comment

Please to leave your comment.

Write new article
  • Written by: M0ka
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +1 / -1
  • Viewed: 3,814 times
  • Created on: May 19, 2014
  • Last updated: Jun 24, 2014
  • Tags: profiling, optimization