drewlabs / laravel-mem-usage
跟踪Laravel应用中每个请求的内存使用情况
v0.1.5
2024-08-15 15:17 UTC
Requires
- illuminate/support: ^9.0|^10.0|^11.0
This package is auto-updated.
Last update: 2024-09-15 15:30:13 UTC
README
此库提供了一个中间件对象,允许开发人员查询Laravel请求的内存使用情况。该中间件可以全局注册,使其在每个请求上运行,或者通过中间件别名附加到特定的路由。
默认情况下,它不对记录的内存信息进行任何操作。为了将其写入磁盘或应用数据库,开发人员必须提供附加到Drewlabs\Laravel\Memory\Usage\Log
类的监听器,如下所示
// 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(); } }