Profiling user requests

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

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 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' => Helper::bytesToKb($memory),
            'request_memory_mb' => Helper::bytesToMb($memory),
            'request_memory_peak_kb' => Helper::bytesToKb($peak),
            'request_memory_peak_mb' => Helper::bytesToMb($peak),
            'request_url' => Yii::app()->request->requestUri,
            'request_type' => Yii::app()->request->requestType,
            'request_errors' => $error,
            'user_status' => !Yii::app()->user->isGuest,
            'user_ip' => !Yii::app()->request->userHostAddress,
            'date' => time(),
            'time_taken' => microtime(true) - self::$start
        );
    }

}

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!

1 0
3 followers
Viewed: 9 577 times
Version: Unknown (update)
Category: Tutorials
Written by: M0ka
Last updated by: M0ka
Created on: May 19, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles