以下是Linux环境下ThinkPHP性能监控的实用技巧:
top、htop查看进程资源占用,vmstat、iostat监控内存、磁盘I/O负载,netstat分析网络连接状态。runtime/log)和Nginx/Apache访问日志,定位异常请求和慢响应。// 示例:记录请求耗时中间件
public function handle($request, \Closure $next)
{
$start = microtime(true);
$response = $next($request);
$cost = microtime(true) - $start;
\think\facade\Log::info("请求耗时:{$cost}秒");
return $response;
}
EXPLAIN分析低效SQL,添加索引优化。with方法)优化关联查询。worker_processes和worker_connections,启用Gzip压缩和HTTP缓存头。