JMeter 在 CentOS 的日志分析与定位指南
一 日志类型与存放位置
二 常用命令与日志输出控制
三 关键日志关键字与定位思路
四 快速指标计算与 Shell 脚本示例
#!/usr/bin/env bash
set -euo pipefail
LOG="${1:-jmeter.log}"
thread_num=$(grep 'Thread started' "$LOG" | tail -n1 | awk -F'-' '{print $6}')
start_time=$(grep 'All thread groups have been started' "$LOG" | head -n1 | awk '{print $1,$2}' | sed 's/,//g')
end_time=$(grep 'Shutdown hook ended' "$LOG" | tail -n1 | awk '{print $1,$2}' | sed 's/,//g')
final_success_time=$(grep 'place order success' "$LOG" | tail -n1 | awk '{print $1,$2}' | sed 's/,//g')
to_sec() { date -d "$1" +%s 2>/dev/null || echo 0; }
start_sec=$(to_sec "$start_time")
end_sec=$(to_sec "$end_time")
final_sec=$(to_sec "$final_success_time")
success_times=$(grep -c 'place order success' "$LOG" || true)
failure_times=$(grep -c 'FailureMessage' "$LOG" || true)
request_times=$((success_times + failure_times))
error_rate=$(awk -v s="$failure_times" -v r="$request_times" 'BEGIN{printf "%.2f", r>0?s/r*100:0}')
running_time=$((end_sec - start_sec))
success_running_time=$((final_sec - start_sec))
qps=$(awk -v r="$request_times" -v t="$running_time" 'BEGIN{printf "%.2f", t>0?r/t:0}')
throughput=$(awk -v s="$success_times" -v t="$success_running_time" 'BEGIN{printf "%.2f", t>0?s/t:0}')
echo "线程数: $thread_num"
echo "请求次数: $request_times"
echo "成功次数: $success_times"
echo "失败次数: $failure_times"
echo "错误率: ${error_rate}%"
echo "开始时间: $start_time"
echo "结束时间: $end_time"
echo "最后成功时间: $final_success_time"
echo "测试时长(秒): $running_time"
echo "成功运行时长(秒): $success_running_time"
echo "吞吐量(成功/秒): ${throughput}/s"
echo "QPS(请求/秒): ${qps}/s"
五 结果日志 JTL/CSV 的分析与可视化
import pandas as pd
import matplotlib.pyplot as plt
cols = ['timeStamp','elapsed','label','responseCode','responseMessage','bytes','latency','success']
df = pd.read_csv('result.jtl', names=cols, skiprows=1)
total = len(df)
success = int(df['success'].sum())
rate = success / total * 100
avg_rt = df['elapsed'].mean()
print(f"总请求数: {total}")
print(f"成功率: {rate:.2f}%")
print(f"平均响应时间: {avg_rt:.2f} ms")
plt.hist(df['elapsed'], bins=50, color='skyblue', edgecolor='black')
plt.title('响应时间分布')
plt.xlabel('响应时间(ms)')
plt.ylabel('请求数')
plt.grid(True)
plt.tight_layout()
plt.show()