PHP code example of drewlabs / laravel-mem-usage

1. Go to this page and download the library: Download drewlabs/laravel-mem-usage 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/ */

    

drewlabs / laravel-mem-usage example snippets



// MyMemoryLogListener.php

class MyMemoryLogListener
{
    /**
     * Handle the event.
     */
    public function handle(\Drewlabs\Laravel\Memory\Usage\Log $log): void
    {
        // Note: returned value by each of these functions are of type AllocatedMemory{usedMemory, requestedMemory}
        $alloc1 = $log->getInitialMemoryAllocation(); // memory allocation before handling the request [memory consumed by laravel initialization classed and function ]
        $alloc2 = $log->getCurrentMemoryAllocation(); // $alloc2 = $alloc1 + memory consume by the actual request handler

        $diff = $log->getHandlersConsumedMemory(); // Compute the difference $diff = $alloc1 - $alloc 2

        // Do something with the logged information
    }
}

// Then we bind the event listener to the log event instance in our application service provider

// EventServiceProvider.php

use Illuminate\Foundation\Support\Providers\EventServiceProvider as BaseServiceProvider;

class EventServiceProvider extends BaseServiceProvider
{
	/**
	 * The event listener mappings for the application.
	 *
	 * @var array<class-string, array<int, class-string>>
	 */
	protected $listen = [
		\Drewlabs\Laravel\Memory\Usage\Log::class => [
			\App\Listeners\MyMemoryLogListener::class
		],
	];

	/**
	 * Register application services.
	 * 
	 *
	 * @return void
	 */
	public function register()
	{
        // Call the event service provider instance
		parent::register();
    }
}