PHP项目性能压测工具

wen PHP项目 2

本文目录导读:

PHP项目性能压测工具

  1. HTTP层压测工具(最常用)
  2. PHP性能分析工具
  3. 监控与日志分析
  4. 数据库性能测试
  5. PHP项目专用压测方案
  6. CI/CD集成压测
  7. 压测最佳实践

针对PHP项目的性能压测,我推荐以下工具和方案:

HTTP层压测工具(最常用)

Apache Bench (ab)

最简单的压测工具,Apache自带:

# 1000个请求,100并发
ab -n 1000 -c 100 http://your-site.com/api/test
# 带POST请求
ab -n 100 -c 10 -p post_data.json -T application/json http://your-site.com/api/post

wrk

高性能压测工具,用C语言编写:

# 12线程,400连接,30秒
wrk -t12 -c400 -d30s http://your-site.com/api/test
# 使用Lua脚本模拟复杂场景
wrk -t8 -c200 -d60s -s post.lua http://your-site.com

JMeter

Apache出品的图形化压测工具,功能最全面:

  • 支持多种协议(HTTP/HTTPS/WebSocket等)
  • 支持分布式压测
  • 可视化报表

示例测试计划配置:

<TestPlan>
  <ThreadGroup>
    <ThreadGroupProperties>
      <num_threads>100</num_threads>
      <ramp_time>10</ramp_time>
      <loops>100</loops>
    </ThreadGroupProperties>
  </ThreadGroup>
</TestPlan>

k6

现代压测工具,脚本化配置:

// loadtest.js
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
  http.get('http://your-site.com/api/test');
  sleep(1);
}
export const options = {
  vus: 50,           // 50个虚拟用户
  duration: '60s',   // 60秒
  thresholds: {
    http_req_duration: ['p(95)<500'] // 95%请求在500ms内
  }
};

运行:

k6 run loadtest.js

PHP性能分析工具

Xdebug + Webgrind

分析PHP执行性能:

; php.ini配置
xdebug.profiler_enable=1
xdebug.profiler_output_dir=/tmp/profiler
xdebug.profiler_output_name=cachegrind.out.%p

生成的文件用Webgrind可视化分析。

Blackfire.io

专业PHP性能分析工具:

# 安装agent
curl -sL https://blackfire.io/install | sh
# 分析请求
blackfire run php script.php

Tideways

轻量级PHP性能监控:

// 代码嵌入
$tideways = new Tideways\Profiler();
$tideways->start();
// ... 业务代码
$tideways->stop();

监控与日志分析

New Relic

实时性能监控,提供:

  • 事务追踪
  • 数据库查询分析
  • 外部API调用耗时

Pinpoint

开源APM,支持PHP:

# pinpoint-agent配置
agent_collector_ip: 127.0.0.1
agent_collector_port: 9994

ELK Stack

日志分析与性能监控:

{
  "request_time": 0.123,
  "response_code": 200,
  "request_uri": "/api/getUser",
  "php_execution_time": 0.098
}

数据库性能测试

MySQL Benchmark

# 测试MySQL性能
mysqlslap --user=root --password=123 --concurrency=50 \
  --iterations=10 --number-of-queries=1000 \
  --create-schema=test_db --query="SELECT * FROM users"

Sysbench

全面测试数据库:

# 安装
apt-get install sysbench
# CPU测试
sysbench cpu run --threads=4
# MySQL基准测试
sysbench oltp_read_write --db-driver=mysql \
  --mysql-user=root --mysql-password=123 \
  --mysql-db=test_db run

PHP项目专用压测方案

Laravel Telescope

Laravel项目性能调试:

composer require laravel/telescope
php artisan telescope:install
php artisan migrate

访问 /telescope 查看请求性能分析。

Symfony Profiler

Symfony项目自带Profiler:

# config/packages/dev/web_profiler.yaml
web_profiler:
    toolbar: true
    intercept_redirects: false

访问 /_profiler 查看性能数据。

CI/CD集成压测

GitHub Actions示例:

name: Load Test
on: [push]
jobs:
  load-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install k6
        run: |
          sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
          echo "deb https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
          sudo apt-get update
          sudo apt-get install k6
      - name: Run load test
        run: k6 run loadtest.js

压测最佳实践

压测前准备

  • 关闭Xdebug等调试工具
  • 开启OPcache
  • 调整PHP配置:
    memory_limit = 256M
    max_execution_time = 30
    opcache.enable = 1
    opcache.memory_consumption = 128

压测指标关注

  • QPS(每秒请求数)
  • 响应时间(P95, P99)
  • 错误率
  • CPU/内存/IO使用率

常见问题处理

# 查看PHP-FPM状态
curl http://localhost/status?json
# 查看慢查询
tail -f /var/log/php-fpm/slow.log
# 查看Nginx访问日志
tail -f /var/log/nginx/access.log

快速开始指南

对于简单的PHP项目,建议:

  1. 先用ab做快速压测
  2. 发现问题后用Xdebug Profiler分析性能瓶颈
  3. 使用wrk做持续压测收集详细数据
  4. 压力测试环境开启监控

选择工具建议:

  • 快速测试:abwrk
  • 复杂场景:JMeterk6
  • 性能分析:BlackfireXdebug
  • 生产监控:New RelicPinpoint

根据项目需求和复杂度选择合适的工具组合,建议在测试环境进行压测,避免影响生产环境。

抱歉,评论功能暂时关闭!