Skip to content

Performance

Jean-Marc Strauven edited this page Aug 1, 2025 · 1 revision

πŸš€ Performance Guide

Laravel Safeguard is designed to be fast and efficient, but here are some tips to optimize performance for large applications and continuous monitoring.

⚑ Performance Optimization

Rule Selection

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',
        ],
    ],
];

Selective Rule Execution

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=database

Parallel Execution

Enable parallel processing for independent rules:

// config/safeguard.php
return [
    'performance' => [
        'parallel_execution' => true,
        'max_processes' => 4,
    ],
];

Caching

Enable rule result caching for repeated executions:

// config/safeguard.php
return [
    'cache' => [
        'enabled' => true,
        'ttl' => 300, // 5 minutes
        'store' => 'file', // or 'redis', 'database'
    ],
];

πŸ“Š Benchmarks

Rule Execution Times

Rule Category Average Time Memory Usage
Environment 5ms 1MB
Security 15ms 2MB
Database 25ms 3MB
Custom Variable Variable

Large Application Performance

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

πŸ”§ Configuration Optimization

Minimal Production Config

// 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,
    ],
];

Development Optimization

// config/safeguard.php (development optimized)
return [
    'rules' => [
        'basic-environment',
        'development-helpers',
    ],
    'performance' => [
        'cache_enabled' => false, // Always fresh in dev
        'detailed_output' => true,
    ],
];

πŸ“ˆ Monitoring Performance

Built-in Profiling

# Enable profiling
php artisan safeguard:check --profile

# Output includes timing information
php artisan safeguard:check --profile --format=json

Example 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 Performance Monitoring

// 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;
    }
}

πŸ”„ CI/CD Performance

Optimized Pipeline

# .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

Caching in CI

# 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

🚨 Performance Alerts

Slow Rule Detection

// config/safeguard.php
return [
    'performance' => [
        'slow_rule_threshold' => 0.1, // 100ms
        'alert_on_slow_rules' => true,
        'log_performance' => env('APP_ENV') === 'production',
    ],
];

Memory Usage Monitoring

# Monitor memory usage
php -d memory_limit=256M artisan safeguard:check --memory-limit=200M

πŸ“š Best Practices

1. Environment-Specific Rules

Only run rules relevant to your environment to minimize execution time.

2. Rule Prioritization

Order rules by importance and execution speed.

3. Caching Strategy

Use caching in production, disable in development for fresh results.

4. Parallel Execution

Enable for applications with many independent rules.

5. Regular Monitoring

Monitor performance in CI/CD to catch performance regressions.

πŸ“š Related Documentation


🏠 Home | βš™οΈ Configuration | πŸ”§ Commands | πŸš€ CI/CD Integration

Clone this wiki locally