vitorsreis/extend-analysis

Analysis and Profile of Requests/Server for PHP

1.0.0 2024-09-13 20:18 UTC

This package is auto-updated.

Last update: 2024-10-13 20:35:57 UTC


README

Latest Stable Version PHP Version Require License Total Downloads Repo Stars

Simple and powerful monitor of requests/server with interactive and realtime dashboard for a simple analysis and manual profile. Unit tests have passed on versions: 5.6, 7.4, 8.1, 8.2 and 8.3

Install

composer require vitorsreis/extend-analysis

Request Monitor

• Simple start usage

use VSR\Extend\Analysis;

# Create driver
$driver = new Analysis\Driver\Standard(__DIR__);

# Set driver
Analysis::setDriver($driver);

# @param bool $autoSave [optional] Save automatically on shutdown event
#                       default: true, if false, you need call $requestProfile->save() manually
global $profile;
$profile = new Analysis\Request();

• Adding action to profile tree

Is recommended to use try/catch/finally for capture errors, however Error\Exception is captured automatically. Use at strategic points in the code to better build your tree, e.g. caller middleware, model proxy, ...

global $profile;
try {
    $profile->start(/* profile_name */); # up level, start action monitor
    // your code
} catch (Throwable $e) {
    $profile->error($e); # register error in current level
    // your code
} finally {
    $extra = ...; // [optional] extra info about action
    $profile->stop($extra || null); # down level, end action monitor
}

• Adding extra info about request

$requestProfile->extra(...);

• Capture before save

You can capture the request before save and cancel it if necessary or remove some data. Use "return false" to cancel save.

global $requestProfile;
$requestProfile->onBeforeSave(function (array $request) {
    # Toleration of 1000 actions, 300ms of duration and not error
    if ($request['profile_count'] < 1000 && $request['duration'] < .300 && !$request['error']) {
        # Remove debug fields to save space in database
        $fields = [
            'referer',
            'useragent',
            'get',
            'post',
            'raw_post',
            'files',
            'cookies',
            'server',
            'headers', 
            'inc_files',
            'error',
            'extra',
            'profile'
        ];
        foreach ($fields as $field) {
            $request[$field] = null;
        }
    }
    return $request;
});

Server Monitor

Exemple of usage in examples/serverTop.php