File contents FastAPI 性能指标
本规则集定义了在 FastAPI 应用程序中监控和优化性能的关键指标和方法,旨在确保应用程序的高效运行和良好的用户体验。
1. 关键性能指标 (KPIs)
响应时间 (Latency) : API 端点处理请求并返回响应所需的时间。通常关注平均响应时间、P95 和 P99 响应时间。
吞吐量 (Throughput) : 单位时间内 API 端点能够处理的请求数量(例如,每秒请求数 RPS)。
错误率 (Error Rate) : 返回错误响应(例如,HTTP 5xx 状态码)的请求所占的百分比。
资源利用率 : CPU 使用率、内存使用率、磁盘 I/O 和网络 I/O。
并发连接数 : 同时连接到服务器的客户端数量。
2. 监控工具与实践
日志 : 使用结构化日志记录请求和响应信息,包括响应时间、状态码等。
Prometheus/Grafana : 集成 Prometheus 用于收集指标,Grafana 用于可视化和构建仪表盘。
FastAPI 性能中间件 : 可以使用 starlette-exporter 或自定义中间件来暴露 Prometheus 指标。
APM (Application Performance Monitoring) : 使用 Sentry、New Relic、Datadog 等 APM 工具进行更全面的应用性能监控和追踪。
压力测试 : 使用 Locust、JMeter、k6 等工具进行压力测试,模拟高并发场景,发现性能瓶颈。
3. 性能优化策略
异步操作 : 充分利用 FastAPI 的异步特性(async/await),尤其是在进行 I/O 密集型操作(如数据库查询、外部 API 调用)时。
数据库优化 :
索引 : 确保数据库表有适当的索引。
查询优化 : 避免 N+1 查询问题,使用连接查询或批量查询。
连接池 : 使用数据库连接池来管理数据库连接。
缓存 :
内存缓存 : 使用 Redis 或 Memcached 缓存频繁访问的数据。
HTTP 缓存 : 利用 HTTP 缓存头(Cache-Control, ETag, Last-Modified)减少重复请求。
数据序列化优化 : 优化 Pydantic 模型的序列化和反序列化性能。
Gunicorn/Uvicorn 配置 :
工作进程数 : 根据 CPU 核心数配置 Gunicorn 的工作进程数(通常为 2 * CPU_CORES + 1)。
Worker 类型 : 对于 I/O 密集型应用,可以考虑使用 uvloop 和 httptools 优化 Uvicorn。
代码优化 :
避免不必要的计算 : 减少每个请求中的计算量。
延迟加载 : 延迟加载不立即需要的数据或资源。
限流 : 实施速率限制以保护 API 免受滥用和过载。
CDN : 对于静态文件,使用 CDN 加速内容分发。
1 --- 2 name: fastapi-api-example-fastapi-performance-metrics 3 description: FastAPI 性能指标 4 --- 5 6 # FastAPI 性能指标 7 8 本规则集定义了在 FastAPI 应用程序中监控和优化性能的关键指标和方法,旨在确保应用程序的高效运行和良好的用户体验。 9 10 ## 1. 关键性能指标 (KPIs) 11 12 - **响应时间 (Latency)**: API 端点处理请求并返回响应所需的时间。通常关注平均响应时间、P95 和 P99 响应时间。 13 - **吞吐量 (Throughput)**: 单位时间内 API 端点能够处理的请求数量(例如,每秒请求数 RPS)。 14 - **错误率 (Error Rate)**: 返回错误响应(例如,HTTP 5xx 状态码)的请求所占的百分比。 15 - **资源利用率**: CPU 使用率、内存使用率、磁盘 I/O 和网络 I/O。 16 - **并发连接数**: 同时连接到服务器的客户端数量。 17 18 ## 2. 监控工具与实践 19 20 - **日志**: 使用结构化日志记录请求和响应信息,包括响应时间、状态码等。 21 - **Prometheus/Grafana**: 集成 Prometheus 用于收集指标,Grafana 用于可视化和构建仪表盘。 22 - **FastAPI 性能中间件**: 可以使用 `starlette-exporter` 或自定义中间件来暴露 Prometheus 指标。 23 - **APM (Application Performance Monitoring)**: 使用 Sentry、New Relic、Datadog 等 APM 工具进行更全面的应用性能监控和追踪。 24 - **压力测试**: 使用 Locust、JMeter、k6 等工具进行压力测试,模拟高并发场景,发现性能瓶颈。 25 26 ## 3. 性能优化策略 27 28 - **异步操作**: 充分利用 FastAPI 的异步特性(`async/await`),尤其是在进行 I/O 密集型操作(如数据库查询、外部 API 调用)时。 29 - **数据库优化**: 30 - **索引**: 确保数据库表有适当的索引。 31 - **查询优化**: 避免 N+1 查询问题,使用连接查询或批量查询。 32 - **连接池**: 使用数据库连接池来管理数据库连接。 33 - **缓存**: 34 - **内存缓存**: 使用 Redis 或 Memcached 缓存频繁访问的数据。 35 - **HTTP 缓存**: 利用 HTTP 缓存头(`Cache-Control`, `ETag`, `Last-Modified`)减少重复请求。 36 - **数据序列化优化**: 优化 Pydantic 模型的序列化和反序列化性能。 37 - **Gunicorn/Uvicorn 配置**: 38 - **工作进程数**: 根据 CPU 核心数配置 Gunicorn 的工作进程数(通常为 `2 * CPU_CORES + 1`)。 39 - **Worker 类型**: 对于 I/O 密集型应用,可以考虑使用 `uvloop` 和 `httptools` 优化 Uvicorn。 40 - **代码优化**: 41 - **避免不必要的计算**: 减少每个请求中的计算量。 42 - **延迟加载**: 延迟加载不立即需要的数据或资源。 43 - **限流**: 实施速率限制以保护 API 免受滥用和过载。 44 - **CDN**: 对于静态文件,使用 CDN 加速内容分发。
liaosw97/awesome-rules-skills/tree/main/plugins/fastapi/skills/fastapi-api-example-fastapi-performance-metrics commit d248ba8bb1
Frequently asked questions How do I install the Fastapi API Example Fastapi Performance Metrics skill? Run npx skillmds@latest add liaosw97/fastapi-api-example-fastapi-performance-metrics in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Fastapi API Example Fastapi Performance Metrics skill do? FastAPI 性能指标 It is listed under Integrations & APIs on SkillMD.
Is Fastapi API Example Fastapi Performance Metrics safe to use? This skill has not completed SkillMD's automated safety review yet. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Fastapi API Example Fastapi Performance Metrics? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Fastapi API Example Fastapi Performance Metrics free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Fastapi API Example Fastapi Performance Metrics? liaosw97 (@liaosw97) published this skill. Their other Agent Skills are listed on their SkillMD profile.