simple-as-fuck / php-performance-log
Services for logging slow parts of application.
Installs: 48
Dependents: 1
Suggesters: 1
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/simple-as-fuck/php-performance-log
Requires
- php: ^8.2
- illuminate/console: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/contracts: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/database: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/events: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/http: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/log: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/queue: ^8.30|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.30|^9.0|^10.0|^11.0|^12.0
- psr/http-message: ^1.0|^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.0|^2.0|^3.0
- simple-as-fuck/php-validator: ^0.1.0|^0.2.0|^0.3.0|^0.4.0|^0.5.0|^0.6.0|^0.7.0
- symfony/http-foundation: ^5.4|^6.0|^7.0
Requires (Dev)
Suggests
- sentry/sentry: Allows receive logs with performance warning and make statistic on them, send notification, ...
README
Services for logging slow parts of application.
Installation
composer require simple-as-fuck/php-performance-log
Support
If any PHP platform requirements in composer.json ends with security support, consider package version as unsupported except last version.
Measurements support
Application | Http requests | DB transactions | SQL queries | Console commands | Queue Jobs |
---|---|---|---|---|---|
Laravel | With middleware use default 1 second | Default 300 milliseconds | Default 50 milliseconds | Default off | Recommended 40 seconds |
Laravel application
Global thresholds configuration are in performance_log.php
config, publishable from package.
php artisan vendor:publish --tag performance-log-config
For http request time logging you must register LaravelMiddleware as global on first position.
Other applications
All package services or listeners do not have any external dependencies except PSR interfaces, is possible use package for measurement of different types of applications.
You must register some PerformanceLogConfig extends to your application
as unique global instance (singleton), because in the service state are hold temporary thresholds overwrites,
and you should overwrite methods getConfig...
, where you can configure your global thresholds
or in methods load thresholds from application configuration files.
Thresholds values has behaviour: not zero value will log longer runs then threshold value as warning,
null
or zero value turn off measurement, zero value with debug true
will log running time as debug.
You can look at LaravelPerformanceLogConfig as example.
You must register listeners to your application as singletons, because most listeners has state and hold measurements data inside. PSR-14 listeners are callables, you can register package listeners by anonymous function adapters.
You can look at LaravelProvider as example.
For http request time logging you must register PsrMiddleware. Recommended usage is register middleware as global on first position and all of your request will be measured.
Thresholds overwrite
Sql
If you know than some sql is slow, and you are fine with that you can overwrite global thresholds configuration by setting a temporary threshold in PerformanceLogConfig.
/** @var \SimpleAsFuck\PerformanceLog\Service\PerformanceLogConfig $performanceLogConfig */ $sqlThreshold = $performanceLogConfig->setSlowSqlQueryThreshold(null); $transactionThreshold = $performanceLogConfig->setSlowDbTransactionThreshold(null); // run some slow queries without annoying performance log $sqlThreshold->restore(); $transactionThreshold->restore();
Http
If you know that some specific controller action is slow or should be extra fast, you can overwrite global threshold configuration by setting a temporary threshold. The temporary threshold can be set anywhere in request run and live until a request ends.
/** @var \SimpleAsFuck\PerformanceLog\Service\PerformanceLogConfig $performanceLogConfig */ $performanceLogConfig->setSlowRequestThreshold(null); // run some extra slow logic without annoying performance log // no need for threshold restoring, performance listener will handle it
Console
If you want to overwrite global threshold configuration, you can do it by setting a temporary threshold. The temporary threshold can be set anywhere in command run and live until a command ends.
/** @var \SimpleAsFuck\PerformanceLog\Service\PerformanceLogConfig $performanceLogConfig */ $performanceLogConfig->setSlowCommandThreshold(60); // one minute // run some measured logic // no need for threshold restoring, performance listener will handle it
Job
If you want to overwrite global threshold configuration, you can set a temporary threshold. The temporary threshold can be set anywhere in job run and live until a job ends.
/** @var \SimpleAsFuck\PerformanceLog\Service\PerformanceLogConfig $performanceLogConfig */ $performanceLogConfig->setSlowJobThreshold(10000); // 10 seconds // run some measured logic // no need for threshold restoring, performance listener will handle it
Usage with monitoring
Is recommended send performance warning logs into your monitoring system, so you know what is slow.
For simple monitoring is sentry integration. Sentry integration can collect information about request or command with stacktrace, this can make finding slow query much easier.