-
-
Notifications
You must be signed in to change notification settings - Fork 0
Performance
Laravel Safeguard is designed to be fast and efficient, but here are some tips to optimize performance for large applications and continuous monitoring.
Only run necessary rules for your environment:
// config/safeguard.php
return [
'rules' => [
'production' => [
'app-debug-false',
'app-key-set',
'database-security',
'session-security',
],
'development' => [
'app-debug-true',
'database-connection',
],
],
];Run only specific rule categories:
# Only environment rules
php artisan safeguard:check --only=environment
# Only security rules
php artisan safeguard:check --only=security
# Exclude specific categories
php artisan safeguard:check --except=databaseEnable parallel processing for independent rules:
// config/safeguard.php
return [
'performance' => [
'parallel_execution' => true,
'max_processes' => 4,
],
];Enable rule result caching for repeated executions:
// config/safeguard.php
return [
'cache' => [
'enabled' => true,
'ttl' => 300, // 5 minutes
'store' => 'file', // or 'redis', 'database'
],
];| Rule Category | Average Time | Memory Usage |
|---|---|---|
| Environment | 5ms | 1MB |
| Security | 15ms | 2MB |
| Database | 25ms | 3MB |
| Custom | Variable | Variable |
For applications with 100+ rules:
# Without optimization
php artisan safeguard:check
# β±οΈ ~2.5 seconds
# With optimization
php artisan safeguard:check --cache --parallel
# β±οΈ ~0.8 seconds// config/safeguard.php (production optimized)
return [
'rules' => [
'app-debug-false',
'app-key-set',
'session-secure',
'database-encrypted',
],
'performance' => [
'parallel_execution' => true,
'cache_enabled' => true,
],
'output' => [
'format' => 'json',
'verbose' => false,
],
];// config/safeguard.php (development optimized)
return [
'rules' => [
'basic-environment',
'development-helpers',
],
'performance' => [
'cache_enabled' => false, // Always fresh in dev
'detailed_output' => true,
],
];# Enable profiling
php artisan safeguard:check --profile
# Output includes timing information
php artisan safeguard:check --profile --format=jsonExample Profiled Output:
{
"performance": {
"total_time": "0.856s",
"memory_peak": "4.2MB",
"rules_executed": 15,
"cache_hits": 8,
"parallel_processes": 4
},
"timing": {
"environment_rules": "0.125s",
"security_rules": "0.445s",
"database_rules": "0.286s"
}
}// Custom rule with performance tracking
class OptimizedCustomRule extends BaseRule
{
public function check(): RuleResult
{
$start = microtime(true);
// Your rule logic here
$result = $this->performCheck();
$duration = microtime(true) - $start;
// Log slow rules
if ($duration > 0.1) {
Log::warning("Slow rule execution: {$this->name()} took {$duration}s");
}
return $result;
}
}# .github/workflows/security.yml
name: Security Check
on: [push, pull_request]
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PHP with cache
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, xml, ctype, iconv, intl
coverage: none
- name: Cache Composer dependencies
uses: actions/cache@v3
with:
path: vendor
key: composer-${{ hashFiles('**/composer.lock') }}
- name: Install dependencies
run: composer install --no-dev --optimize-autoloader
- name: Run optimized security check
run: |
php artisan safeguard:check \
--format=ci \
--cache \
--parallel \
--only=production \
--fail-on-error# Use CI-specific cache directory
export SAFEGUARD_CACHE_DIR=/tmp/safeguard-cache
mkdir -p $SAFEGUARD_CACHE_DIR
php artisan safeguard:check --cache-dir=$SAFEGUARD_CACHE_DIR// config/safeguard.php
return [
'performance' => [
'slow_rule_threshold' => 0.1, // 100ms
'alert_on_slow_rules' => true,
'log_performance' => env('APP_ENV') === 'production',
],
];# Monitor memory usage
php -d memory_limit=256M artisan safeguard:check --memory-limit=200MOnly run rules relevant to your environment to minimize execution time.
Order rules by importance and execution speed.
Use caching in production, disable in development for fresh results.
Enable for applications with many independent rules.
Monitor performance in CI/CD to catch performance regressions.
- π§ Commands - Command-line options for performance
- βοΈ Configuration - Performance configuration options
- π CI/CD Integration - Optimizing for pipelines
π Home | βοΈ Configuration | π§ Commands | π CI/CD Integration
Laravel Safeguard - Configurable Security Checks for Laravel Applications
π Home | π¦ Installation | β‘ Quick Start | π‘ Examples | π Full Docs
Made with β€οΈ for the Laravel community
Β© 2025 - Laravel Safeguard by Grazulex