PHP code example of duckstery / process-analyzer

1. Go to this page and download the library: Download duckstery/process-analyzer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

duckstery / process-analyzer example snippets


// Start of code
$uid = Analyzer::start("Do a simple math");

// Calculating
$output = 1 + 1;
// Print output to screen
echo $output;

// End of code
Analyzer::stop($uid);
// Flush to get report
Analyzer::flush();

Duckstery\Laravel\Analyzer\ProcessAnalyzerServiceProvider::class

/*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */

    'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */
        Duckstery\Laravel\Analyzer\ProcessAnalyzerServiceProvider::class, // ** //
        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
    ])->toArray(),



use Duckstery\Analyzer\AnalyzerConfig;

class MyAnalyzerConfig extends AnalyzerConfig
{
    // Todo
}



use Duckstery\Analyzer\AnalyzerConfig;

class MyAnalyzerConfig extends AnalyzerConfig
{
    // Property override
    protected bool $prettyPrint;
    
    // Function override
    public function prettyPrint(): bool
    {
        return true;
    }
}



use Duckstery\Analyzer\Analyzer;

Analyzer::tryToInit(new MyAnalyzerConfig());

[
    "Default" => [
        "peak" => [
            // Metrics method name's or callback array
            "handler" => "memory_get_peak_usage",
            // Formatter
            "formatter" => [Utils::class, "appendB"],
            // Get at the start
            "start" => true,
            // Get at the end
            "stop" => true,
            // Calculate difference
            "diff" => true
        ]
    ]
]

Duckstery\Analyzer\Structures\AnalysisProfile::class

Duckstery\Analyzer\Structures\AnalysisRecord::class

Duckstery\Analyzer\AnalysisPrinter::class

$prettyPrint = true;
//    Default --------------------
//    ╭───────────────┬──────────────────┬─────────────┬────────┬────────────┬────────────┬───────────╮
//    │ Uid           │ Name             │ Time        │ Memory │ Start peak │ Stop peak  │ Diff peak │
//    ├───────────────┼──────────────────┼─────────────┼────────┼────────────┼────────────┼───────────┤
//    │ 654af62889e08 │ Do a simple math │ 2006.472 ms │ 2.5 KB │ 16502872 B │ 16502872 B │       0 B │
//    ╰───────────────┴──────────────────┴─────────────┴────────┴────────────┴────────────┴───────────╯
//    ----------------------------

$prettyPrint = false;
//    Default --------------------
//    [654af6159585c] Do a simple math:
//        Time ⇒ [2000.605 ms];
//        Memory ⇒ [2.5 KB];
//        Start peak ⇒ [16501872 B];
//        Stop peak ⇒ [16501872 B];
//        Diff peak ⇒ [0 B];
//    ----------------------------

$oneLine = true;
//    Default --------------------
//    [654b2b522090f] Do a simple math: Time ⇒ [2009.288 ms]; Memory ⇒ [2.5 KB]; Start peak ⇒ [16501904 B]; Stop peak ⇒ [16501904 B]; Diff peak ⇒ [0 B];
//    ----------------------------

$oneLine = false;
//    Default --------------------
//    [654af6159585c] Do a simple math:
//        Time ⇒ [2000.605 ms];
//        Memory ⇒ [2.5 KB];
//        Start peak ⇒ [16501872 B];
//        Stop peak ⇒ [16501872 B];
//        Diff peak ⇒ [0 B];
//    ----------------------------

Duckstery\Analyzer\AnalysisPrinter::class



use Duckstery\Analyzer\AnalysisPrinter;
use Duckstery\Analyzer\Interfaces\IAProfile;

class MyPrinter extends AnalysisPrinter
{
    public function onPreprocessProfile(IAProfile $profile): void
    {
        // Todo
    }
}

$example = [
    "uid" => "654af6159585c",
    "name" => "handle",
    "time" => "2000.605 ms",
    "memory" => "2.5 KB",
]

$example = [
    "uid" => "654af6159585c",
    "name" => "handle",
    "time" => "2000.605 ms",
    "memory" => "2.5 KB",
    "start peak" => ..., // If start = true
    "stop peak" => ..., // If stop = true
    "diff peak" => ..., // If start = stop = diff = true
]

Duckstery\Analyzer\Analyzer::class



use Duckstery\Analyzer\Analyzer;

public class SomeController
{
    public function handle(): void
    {
        // Use Profile: SomeController and start recording
        Analyzer::profile("SomeController")->start("handle");
        
        // Execute process A
        $this->processA();
        // Execute process B
        $this->processB();
        // Execute process C
        $this->processC();
        
        // Stop the latest recording of Profile
        Analyzer::profile("SomeController")->stop();

        // Flush
        Analyzer::flush("SomeController");
        // Or Analyzer::flush(); to flush all Profile
    }
    
    public function processA(): void
    {
        // Use Profile: SomeController and start recording
        Analyzer::profile("SomeController")->start("processA");
    
        // Use 5kb
        
        // Stop the latest recording of Profile
        Analyzer::profile("SomeController")->stop();
    }
    
    public function processB(): void
    {
        // Use Profile: SomeController and start recording
        Analyzer::profile("SomeController")->start("processA");
    
        // Use 0kb
        
        // Stop the latest recording of Profile
        Analyzer::profile("SomeController")->stop();
    }
    
    public function processC(): void
    {
        // Use Profile: SomeController and start recording
        Analyzer::profile("SomeController")->start("processA");
    
        // Executed for 5s
        
        // Stop the latest recording of Profile
        Analyzer::profile("SomeController")->stop();
    }
}

$profileExtras = [
    "Default" => [
        "peak" => [
            // Metrics method name's or callback array
            "handler" => "memory_get_peak_usage",
            // Formatter
            "formatter" => [Utils::class, "appendB"],
            // Get at the start
            "start" => true,
            // Get at the end
            "stop" => true,
            // Calculate difference
            "diff" => true
        ]
    ]
]



use Duckstery\Analyzer\Analyzer;

public class SomeController
{
    public function handle(): void
    {
        $uid = Analyzer::start("SomeController::handle");
        // Or Analyzer::start("SomeController::handle");
        // Todo
        Analyzer::stop($uid);
        // Or Analyzer::stop();

        // Flush
        Analyzer::flush();
    }
}



use Duckstery\Analyzer\Analyzer;

public class SomeController
{
    public function handle(): void
    {
        $uid = Analyzer::start();
        // Or Analyzer::start();
        // Todo
        Analyzer::stop($uid);
        // Or Analyzer::stop();

        // Flush
        Analyzer::flush();
    }
}



use Duckstery\Analyzer\Analyzer;

public class SomeController
{
    public function handle(): void
    {
        $uid = Analyzer::startProfile("Profile 1");
        // Or Analyzer::startProfile("Profile 1");
        $this->todo();
        Analyzer::stopProfile("Profile 1", $uid);
        // Or Analyzer::stopProfile("Profile 1");

        // Flush
        Analyzer::flush();
    }
    
    public function todo(): void
    {
        $uid = Analyzer::startProfile("Profile 2");
        // Or Analyzer::startProfile("Profile 2");
        $this->todo();
        Analyzer::stopProfile("Profile 2", $uid);
        // Or Analyzer::stopProfile("Profile 2");
    }
}
config/app.php
shell
php artisan vendor:publish --provider="Duckstery\Laravel\Analyzer\ProcessAnalyzerServiceProvider"
array
array
array
array
array