Skip to content

feat(benchmark):优化基准测试报告生成流程 #26

feat(benchmark):优化基准测试报告生成流程

feat(benchmark):优化基准测试报告生成流程 #26

Workflow file for this run

name: 性能测试
on:
workflow_dispatch:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
benchmark:
runs-on: ubuntu-latest
services:
redis:
image: redis:latest
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
strategy:
matrix:
client_count: [1, 8, 128, 1024]
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Java 环境
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
cache: 'maven'
- name: 编译项目
run: mvn compile
- name: 运行性能测试 (客户端数量 ${{ matrix.client_count }})
run: |
echo "Running benchmark tests with ${{ matrix.client_count }} clients..."
# 定义测试参数
clients=("Redisun" "Redisson" "Jedis" "Lettuce")
methods=("asyncSet" "asyncGet" "concurrentSet" "concurrentGet")
# 循环执行所有测试
for client in "${clients[@]}"; do
for method in "${methods[@]}"; do
echo "Running ${client} ${method} test..."
mvn test -Dtest=${client}Benchmark#${method} -Dclient.count=${{ matrix.client_count }} > ${client,,}_${method}_${{ matrix.client_count }}.log 2>&1 || true
done
done
# 上传单个测试结果作为工件
- name: 上传单个测试结果
uses: actions/upload-artifact@v4
with:
name: benchmark-results-${{ matrix.client_count }}
path: "*.log"
# 新增一个job来整合所有结果
merge-reports:
needs: benchmark
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
# 下载所有测试结果
- name: 下载所有测试结果
uses: actions/download-artifact@v4
with:
path: ./artifacts
# 整合报告
- name: 整合测试报告
run: |
echo "# Redis客户端性能测试报告" > benchmark-report.md
echo "测试时间: $(date)" >> benchmark-report.md
echo "" >> benchmark-report.md
echo "## 测试环境" >> benchmark-report.md
echo "- 操作系统: Ubuntu 20.04" >> benchmark-report.md
echo "- Java 版本: JDK 8" >> benchmark-report.md
echo "- Redis 版本: latest" >> benchmark-report.md
echo "- 测试键数量: 50000" >> benchmark-report.md
echo "" >> benchmark-report.md
# 创建通用函数提取数据
extract_data() {
local file_prefix=$1
local client_count=$2
local log_file="./artifacts/benchmark-results-${client_count}/${file_prefix}_${client_count}.log"
if [ -f "$log_file" ]; then
# 提取时间 (cost)
local time_line=$(grep "\] ${file_prefix#*_} cost:" "$log_file" | head -1)
local time="-"
if [ -n "$time_line" ]; then
# 处理不同的格式:xxxms 或 xxx
time=$(echo "$time_line" | awk '{print $NF}' | sed 's/ms$//' | sed 's/[^0-9\-]*//g')
fi
# 提取OPS
local ops_line=$(grep "\] ${file_prefix#*_} ops/s:" "$log_file" | head -1)
local ops="-"
if [ -n "$ops_line" ]; then
ops=$(echo "$ops_line" | awk '{print $NF}' | sed 's/[^0-9\-]*//g')
fi
echo "$time/$ops"
else
echo "-/-"
fi
}
# 定义测试类型和表头
test_types=("concurrent_set" "async_set" "concurrent_get" "async_get")
type_names=("同步SET性能对比" "异步SET性能对比" "同步GET性能对比" "异步GET性能对比")
# 为每种测试类型生成表格
for i in "${!test_types[@]}"; do
test_type=${test_types[$i]}
type_name=${type_names[$i]}
echo "## $type_name" >> benchmark-report.md
echo "| 客户端数量 | 耗时(Redisun/Redisson/Jedis/Lettuce) | OPS(Redisun/Redisson/Jedis/Lettuce) |" >> benchmark-report.md
echo "|------------|------------------------|-----------------------|" >> benchmark-report.md
for client_count in 1 8 128 1024; do
# 修正文件前缀以匹配实际的日志文件名
case "${test_type}" in
"concurrent_set")
redisun_data=$(extract_data "redisun_concurrentSet" "$client_count")
redisson_data=$(extract_data "redisson_concurrentSet" "$client_count")
jedis_data=$(extract_data "jedis_concurrentSet" "$client_count")
lettuce_data=$(extract_data "lettuce_concurrentSet" "$client_count")
;;
"async_set")
redisun_data=$(extract_data "redisun_asyncSet" "$client_count")
redisson_data=$(extract_data "redisson_asyncSet" "$client_count")
jedis_data=$(extract_data "jedis_asyncSet" "$client_count")
lettuce_data=$(extract_data "lettuce_asyncSet" "$client_count")
;;
"concurrent_get")
redisun_data=$(extract_data "redisun_concurrentGet" "$client_count")
redisson_data=$(extract_data "redisson_concurrentGet" "$client_count")
jedis_data=$(extract_data "jedis_concurrentGet" "$client_count")
lettuce_data=$(extract_data "lettuce_concurrentGet" "$client_count")
;;
"async_get")
redisun_data=$(extract_data "redisun_asyncGet" "$client_count")
redisson_data=$(extract_data "redisson_asyncGet" "$client_count")
jedis_data=$(extract_data "jedis_asyncGet" "$client_count")
lettuce_data=$(extract_data "lettuce_asyncGet" "$client_count")
;;
esac
redisun_time=$(echo $redisun_data | cut -d'/' -f1)
redisun_ops=$(echo $redisun_data | cut -d'/' -f2)
redisson_time=$(echo $redisson_data | cut -d'/' -f1)
redisson_ops=$(echo $redisson_data | cut -d'/' -f2)
jedis_time=$(echo $jedis_data | cut -d'/' -f1)
jedis_ops=$(echo $jedis_data | cut -d'/' -f2)
lettuce_time=$(echo $lettuce_data | cut -d'/' -f1)
lettuce_ops=$(echo $lettuce_data | cut -d'/' -f2)
echo "| ${client_count} | ${redisun_time}/${redisson_time}/${jedis_time}/${lettuce_time} | ${redisun_ops}/${redisson_ops}/${jedis_ops}/${lettuce_ops} |" >> benchmark-report.md
done
echo "" >> benchmark-report.md
done
echo "> 注意:由于 GitHub Actions 环境限制,测试结果可能不如本地环境准确。" >> benchmark-report.md
echo "" >> benchmark-report.md
# 显示报告内容
cat benchmark-report.md
# 上传整合后的测试报告
- name: 上传整合后的测试报告
uses: actions/upload-artifact@v4
with:
name: benchmark-report
path: benchmark-report.md