定制 2a/symfony-performance-analyzer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

2a/symfony-performance-analyzer

Composer 安装命令:

composer require 2a/symfony-performance-analyzer

包简介

Bundle to monitor and analyze performance of Symfony applications

README 文档

README

License PHP Version Symfony Version Build Status Coverage

A powerful toolkit for monitoring and optimizing Symfony applications, offering real-time performance metrics, N+1 query detection, cognitive complexity analysis, automated reporting, a secure web dashboard, and seamless CI/CD integration.

Features

  • 🚀 Real-time Performance Metrics: Monitor request response time, database queries, memory usage, and CLI command performance.
  • 🔍 N+1 Query Detection: Identify and resolve N+1 query issues with detailed analysis.
  • 🧠 Cognitive Complexity Analysis: Detect complex code structures (threshold > 10) to improve maintainability.
  • 📊 Automated Reporting: Generate HTML, JSON, or SVG badge reports for CI/CD pipelines.
  • 📈 Secure Web Dashboard: Visualize performance trends and metrics at /_performance.
  • 🔔 Performance Alerts: Configurable thresholds for response time, memory usage, query count, and complexity.
  • 💾 Flexible Storage: Store data using Doctrine ORM (DatabaseStorage) or JSON files (FileStorage) with retention policies.
  • 🌐 Internationalization: Supports English and French translations.
  • 🛠 Extensibility: Add custom analyzers, collectors, and event listeners.
  • Standards Compliance: Enforced with PHPStan (max level), PHPUnit (100% coverage), ECS, and Rector.
  • Performance Optimizations: Request sampling, degraded mode, and circuit breaker for external services.

Installation

Install the bundle via Composer:

composer require 2a/symfony-performance-analyzer

Enable the bundle in config/bundles.php:

return [
    // ...
    AA\PerformanceAnalyzer\PerformanceAnalyzerBundle::class => ['all' => true],
];

Generate and apply database migrations (for database storage):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

Install assets for the web dashboard:

php bin/console assets:install --symlink

Configuration

Create a configuration file at config/packages/symfony_performance_analyzer.yaml:

symfony_performance_analyzer:
    enabled: true
    storage:
        type: database  # or 'file'
        file_path: '%kernel.project_dir%/var/performance'
        retention:
            days: 30  # Retain data for 30 days
            max_records: 10000  # Maximum number of records
    analyzers:
        memory: true
        n_plus_one: true
        cognitive_complexity: true
    thresholds:
        max_memory_mb: 256  # Maximum memory in MB
        max_response_time_ms: 500  # Maximum response time in ms
        max_db_queries: 50  # Maximum number of database queries
        max_cognitive_complexity: 10  # Maximum cognitive complexity score
    sampling:
        rate: 1.0  # Analyze 100% of requests (0.0 to 1.0)
        degraded_mode: false  # Enable degraded mode under high load
        degraded_threshold_ms: 1000  # Trigger degraded mode above this response time (ms)
    circuit_breaker:
        enabled: false  # Enable circuit breaker for external services
        failure_threshold: 5  # Number of failures before opening circuit
        retry_timeout: 60  # Seconds to wait before retrying
    dashboard:
        enabled: true
        route_prefix: /_performance
        security:
            enable_firewall: false
            allowed_ips: []  # Restrict dashboard access to specific IPs
    profiler:
        enabled: true
        toolbar: true  # Enable Symfony debug toolbar integration
    ci_cd:
        enabled: false
        level: warning  # Report level: error, warning, notice
        output_format: json  # Report format: json, html

For file-based storage, ensure the storage directory is writable:

mkdir -p var/performance
chmod -R 775 var/performance

Usage

CLI Commands

Analyze performance and generate reports:

# Generate an HTML performance report
php bin/console performance:generate-report --format=html --output=report.html

# Generate a JSON report for CI/CD
php bin/console performance:generate-report --format=json --dry-run > ci-report.json

# Generate a CI/CD badge
php bin/console performance:generate-ci-badge --output=badge.svg

# Profile a CLI command
php bin/console performance:profile app:my-command

Example JSON report output:

{
    "logs": [
        {
            "route": "app_home",
            "response_time": 100,
            "memory_usage": 32,
            "query_count": 5
        }
    ],
    "stats": {
        "avg_response_time": 100,
        "avg_memory_usage": 32,
        "max_query_count": 10
    },
    "issues": [
        {
            "type": "high_complexity",
            "message": "Complexity 12 > 10",
            "severity": "high"
        }
    ]
}

Web Dashboard

Access the performance dashboard at /_performance. Secure it by enabling the firewall and restricting IP access:

symfony_performance_analyzer:
    dashboard:
        security:
            enable_firewall: true
            allowed_ips: ['127.0.0.1', '192.168.1.0/24']

Customize the dashboard route in config/routes.yaml:

performance_dashboard:
    path: /_performance
    controller: AA\PerformanceAnalyzer\Controller\DashboardController::index

Debug Toolbar

The bundle integrates with Symfony’s debug toolbar, displaying request time, query count, memory usage, and performance issues. Enable it in the configuration:

symfony_performance_analyzer:
    profiler:
        toolbar: true

Performance Metrics

The bundle collects and analyzes the following metrics:

Metric Description Expected Range Source
Request Time HTTP request execution time 0–500 ms PerformanceTracker, Timer
Database Queries Query count, duration, and N+1 issues 0–50 queries DatabaseQueryCollector, QueryAnalyzer
Memory Usage Peak memory consumption 0–256 MB MemoryAnalyzer
Cognitive Complexity Code complexity score 0–10 CognitiveComplexityAnalyzer
CLI Performance Command execution time 0–5000 ms PerformanceTracker
Issues Performance violations detected 0–10 issues AnalysisResult

Custom Analyzers

Create a custom analyzer by implementing AnalyzerInterface:

namespace App\Analyzer;

use AA\PerformanceAnalyzer\Model\AnalysisResult;
use AA\PerformanceAnalyzer\Model\PerformanceResult;
use AA\PerformanceAnalyzer\Service\Analyzer\AnalyzerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class CustomAnalyzer implements AnalyzerInterface
{
    public function analyze(Request $request, Response $response, PerformanceResult $result): AnalysisResult
    {
        $analysisResult = new AnalysisResult();
        if ($response->getStatusCode() >= 500) {
            $analysisResult->addIssue('server_error', [
                'message' => 'Server error detected',
                'status_code' => $response->getStatusCode(),
                'severity' => 'high'
            ]);
        }
        return $analysisResult;
    }
}

Register the analyzer in config/services.yaml:

App\Analyzer\CustomAnalyzer:
    tags: ['performance.analyzer']

Custom Collectors

Create a custom collector by implementing CollectorInterface:

namespace App\Collector;

use AA\PerformanceAnalyzer\Service\Collector\CollectorInterface;

class CustomCollector implements CollectorInterface
{
    private array $data = [];

    public function start(string $identifier): void
    {
        $this->data[$identifier] = ['start_time' => microtime(true)];
    }

    public function collect(string $identifier): array
    {
        return [
            'custom_duration' => microtime(true) - $this->data[$identifier]['start_time']
        ];
    }
}

Register the collector:

App\Collector\CustomCollector:
    tags: ['performance.collector']

Performance Considerations

  • Request Sampling: Set sampling.rate to a value less than 1.0 (e.g., 0.1 for 10% of requests) in production to reduce overhead.
  • Degraded Mode: Enable sampling.degraded_mode to skip non-essential collectors when response time exceeds degraded_threshold_ms.
  • Circuit Breaker: Activate circuit_breaker.enabled to protect against external service failures, with configurable failure thresholds and retry timeouts.
  • Data Retention: Configure storage.retention to limit database or file storage growth, specifying retention days and maximum records.
  • Overhead: Performance tests ensure tracking overhead is below 5ms per request.

Testing

Run the test suite:

vendor/bin/phpunit

Perform static analysis and linting:

vendor/bin/phpstan analyze
vendor/bin/ecs check
vendor/bin/rector process --dry-run

The bundle includes:

  • Unit Tests: Cover all services and components (100% coverage).
  • Integration Tests: Validate end-to-end workflows with database storage.
  • Performance Tests: Measure tracking overhead.
  • Edge Case Tests: Handle database failures and high-load scenarios.

Uninstallation

Remove the bundle:

composer remove 2a/symfony-performance-analyzer

Drop database tables (if using database storage):

php bin/console doctrine:query:sql "DROP TABLE performance_log, performance_stat"

Remove configuration files and assets:

rm config/packages/symfony_performance_analyzer.yaml
rm -rf var/performance

Contributing

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Commit changes: git commit -am 'Add my feature'
  4. Push to the branch: git push origin feature/my-feature
  5. Open a Pull Request.

Ensure all tests pass and adhere to coding standards (ECS, Rector, PHPStan).

License

This bundle is licensed under the MIT License. See the LICENSE file for details.

2a/symfony-performance-analyzer 适用场景与选型建议

2a/symfony-performance-analyzer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「performance」 「analyzer」 「dashboard」 「n-plus-one」 「ai-recommendations」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 2a/symfony-performance-analyzer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 2a/symfony-performance-analyzer 我们能提供哪些服务?
定制开发 / 二次开发

基于 2a/symfony-performance-analyzer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-30