ismailelbery/api-performance-analyzer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ismailelbery/api-performance-analyzer

Composer 安装命令:

composer require ismailelbery/api-performance-analyzer

包简介

Production-safe API performance profiler for Laravel — slow endpoints, query/N+1 analysis, health scores, alerts and PDF digests.

README 文档

README

Latest Version on Packagist Tests Total Downloads PHP Version Laravel License

A production-safe API performance profiler for Laravel — in the spirit of Telescope/Pulse but focused exclusively on API performance. Low-overhead capture, an admin-gated dashboard, and a REST API for every metric.

  • Package: ismailelbery/api-performance-analyzer
  • Namespace: ApiPerformanceAnalyzer\
  • Requires: PHP 8.2+, Laravel 11/12, MySQL or PostgreSQL
  • Vendor prefix: apa

Install

composer require ismailelbery/api-performance-analyzer

# Publish config (optional) and run migrations
php artisan vendor:publish --tag=apa-config
php artisan migrate

The middleware auto-attaches to your api route group — no code changes needed. The dashboard lives at /apa and the REST API at /apa/api.

How capture works

Request → ApaMiddleware → [Collectors] → ProfileContext (in-memory)
                                              │ terminate()
                                              ▼
                                  ProfileStore (sync|queue|batch)
                                              ▼
                              apa_request_profiles (+ child rows)
  • Lightweight always-on capture (timing/status) runs for every request.
  • Retention is decided at the end of the request: a profile is kept when it is sampled or slow or an error. Child rows (queries, HTTP calls) are only stored for retained profiles.
  • All writes happen in terminate() — never in the response path.

Configuration highlights (config/apa.php)

Key Purpose
sample_rate Probabilistic capture (0.0–1.0). Errors/slow always retained.
kill_switch_cache_key Set this cache key truthy to disable capture instantly — no deploy.
storage.driver sync (dev), queue (default), batch (high-traffic bulk insert).
storage.connection Use a separate DB connection in production so the profiler never pollutes or contends with the app it measures.
thresholds.* slow_request_ms, slow_query_ms, n_plus_one.
privacy.* Bindings off by default (PII), IP hashing, query-string scrubbing.
dashboard.middleware + dashboard.gate Admin gate — never left open. Define Gate::define('viewApa', …).

Storage drivers

  • sync — inline write. Dev / low traffic only.
  • queue — one StoreProfileJob per retained request. Default.
  • batch — buffer in Redis and bulk-insert on size or via apa:flush (schedule it). Recommended for high traffic.

REST API

GET /apa/api/requests              list + filters (method,status,slow,n_plus_one,uri,from,to,per_page)
GET /apa/api/requests/{uuid}       single profile + queries + http calls
GET /apa/api/endpoints             per-endpoint stats (count, avg, p95, error_rate, n+1)  ?sort=p95|avg|count|error_rate|queries
GET /apa/api/stats/overview        window totals
GET /apa/api/stats/slow-queries    cross-request slow query analysis (by sql_hash)
GET /apa/api/stats/n-plus-one      aggregate N+1 suspects per endpoint

Commands

Command What it does
apa:prune [--days=] Delete profiles past the retention window. Schedule daily.
apa:rollup [--date=] [--recommend] Aggregate into apa_daily_endpoint_stats (+ health scores). Schedule daily.
apa:flush Flush the batch buffer. Schedule every minute when using batch.
apa:alerts Evaluate alert rules and notify (Slack/email). Schedule.
apa:explain {sql_hash} EXPLAIN a captured slow query — dev/staging only, opt-in, never auto-applied.
apa:report [--from=] [--to=] [--email=] [--path=] One-document PDF digest (problems + adjacent recommendations). Requires barryvdh/laravel-dompdf.

Suggested scheduler

$schedule->command('apa:prune')->daily();
$schedule->command('apa:rollup --recommend')->dailyAt('00:30');
$schedule->command('apa:flush')->everyMinute();      // only with the batch driver
$schedule->command('apa:alerts')->everyFiveMinutes(); // only if alerts enabled

Phases

  • Phase 1 — timing, query count/time, memory, status; slow-endpoint ranking; request list/detail; REST API.
  • Phase 2 — N+1 detection (per-request and aggregate), external HTTP timing (HttpCollector), daily rollups, slow-query analysis.
  • Phase 3 — per-endpoint health scores (with insufficient-data guardrails), advisory index recommendations, threshold alert engine with cooldown, Slack/email notifications, PDF digest.

Production notes

  • Run profiler storage on a separate connection; use the batch driver under heavy load.
  • Sampling decides what to capture; the batch driver decides how cheaply to write it.
  • The per-request cache kill switch disables capture during an incident without a deploy.
  • Octane-safe: ProfileContext is a scoped binding reset on every request; collectors hold no cross-request state.

Tests

composer install
vendor/bin/phpunit

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固