Last active
August 21, 2025 09:26
-
-
Save am009/17373b3786cf43881fceb8fd88a2cb5e to your computer and use it in GitHub Desktop.
Prepare a Linux desktop PC for CPU sensitive benchmarking.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| # https://llvm.org/docs/Benchmarking.html | |
| # https://smalldatum.blogspot.com/2024/10/managing-cpu-frequency-for-amd-on.html | |
| # sudo apt install linux-tools-common linux-tools-generic linux-cloud-tools-generic cpuset | |
| # disable hyper threading | |
| for i in $(seq 16 31); do | |
| echo 0 | sudo tee /sys/devices/system/cpu/cpu$i/online | |
| done | |
| sudo cpupower idle-set -D1 | |
| sudo cpupower frequency-set --governor performance | |
| cpupower frequency-info -o proc | |
| echo 0 | sudo tee /sys/devices/system/cpu/cpufreq/boost | |
| echo 0 | sudo tee /proc/sys/kernel/randomize_va_space | |
| # echo 1 | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo | |
| #taskset -c 0 ./mybenchmark |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| # sudo apt install linux-tools-common linux-tools-generic linux-cloud-tools-generic cpuset | |
| # disable hyper threading | |
| for i in $(seq 16 31); do | |
| echo 1 | sudo tee /sys/devices/system/cpu/cpu$i/online | |
| done | |
| sudo cpupower idle-set -E | |
| sudo cpupower frequency-set --governor powersave | |
| cpupower frequency-info -o proc | |
| echo 1 | sudo tee /sys/devices/system/cpu/cpufreq/boost | |
| # echo 0 | sudo tee /proc/sys/kernel/randomize_va_space | |
| # echo 0 | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo | |
| #taskset -c 0 ./mybenchmark |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" | |
| # CPU性能稳定性测试脚本 | |
| # 用途:多次运行CPU基准测试并记录性能随时间的变化 | |
| # 配置参数 | |
| TEST_COUNT=20 # 测试次数 | |
| TEST_DURATION=30 # 每次测试的持续时间(秒) | |
| PRIME_NUMBERS=10000 # 计算的质数范围 | |
| RESULTS_FILE="cpu_bench_results.csv" # 结果保存文件 | |
| SLEEP_BETWEEN_TESTS=0 # 测试间隔(秒) | |
| $SCRIPTPATH/benchmark-prepare.sh | |
| # 检查sysbench是否安装 | |
| if ! command -v sysbench &> /dev/null; then | |
| echo "错误: sysbench未安装,请先安装sysbench" | |
| echo "在Ubuntu/Debian上可以使用: sudo apt install sysbench" | |
| echo "在CentOS/RHEL上可以使用: sudo yum install sysbench" | |
| exit 1 | |
| fi | |
| # 创建结果文件头 | |
| echo "timestamp,test_number,events_per_second,cpu_latency" > $RESULTS_FILE | |
| echo "开始CPU性能稳定性测试..." | |
| echo "将进行 $TEST_COUNT 次测试,每次测试 $TEST_DURATION 秒" | |
| echo "结果将保存到: $RESULTS_FILE" | |
| # echo "[*] Shielding: move system slices off CPU0..." | |
| # systemctl set-property --runtime init.scope AllowedCPUs=1-31 | |
| # systemctl set-property --runtime system.slice AllowedCPUs=1-31 | |
| # systemctl set-property --runtime user.slice AllowedCPUs=1-31 | |
| # 运行多次测试 | |
| for ((i=1; i<=$TEST_COUNT; i++)) | |
| do | |
| # 获取当前CPU频率设置(可选) | |
| echo "当前CPU频率信息:" | |
| if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then | |
| echo "CPU调速器: $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)" | |
| fi | |
| if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq ]; then | |
| echo "当前频率: $(($(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)/1000)) MHz" | |
| fi | |
| echo "==============================================" | |
| echo "运行测试 #$i - $(date)" | |
| # 运行sysbench CPU测试 | |
| echo "运行CPU基准测试..." | |
| # result=$(systemd-run --collect --scope -p AllowedCPUs=0 --same-dir -- sysbench cpu --cpu-max-prime=$PRIME_NUMBERS --time=$TEST_DURATION run) | |
| result=$(sysbench cpu --cpu-max-prime=$PRIME_NUMBERS --time=$TEST_DURATION run) | |
| # 提取性能数据 | |
| events_per_second=$(echo "$result" | grep "events per second:" | awk '{print $4}') | |
| cpu_latency=$(echo "$result" | grep "avg:" | awk '{print $2}') | |
| timestamp=$(date +"%Y-%m-%d %H:%M:%S") | |
| # 记录结果 | |
| echo "$timestamp,$i,$events_per_second,$cpu_latency" >> $RESULTS_FILE | |
| echo "测试完成: $events_per_second 事件/秒, CPU Latency: $cpu_latency%" | |
| # 如果不是最后一次测试,则等待 | |
| if [ $i -lt $TEST_COUNT ]; then | |
| echo "等待 $SLEEP_BETWEEN_TESTS 秒后进行下一次测试..." | |
| sleep $SLEEP_BETWEEN_TESTS | |
| fi | |
| done | |
| echo "==============================================" | |
| echo "所有测试完成! 结果已保存到 $RESULTS_FILE" | |
| # 简单结果分析 | |
| echo "性能结果摘要:" | |
| awk -F',' 'NR>1 {print $3}' $RESULTS_FILE | awk ' | |
| BEGIN { | |
| min = 9999999 | |
| max = 0 | |
| sum = 0 | |
| count = 0 | |
| } | |
| { | |
| if ($1 < min) min = $1 | |
| if ($1 > max) max = $1 | |
| sum += $1 | |
| count++ | |
| } | |
| END { | |
| printf "测试次数: %d\n", count | |
| printf "平均性能: %.2f 事件/秒\n", sum/count | |
| printf "最佳性能: %.2f 事件/秒\n", max | |
| printf "最差性能: %.2f 事件/秒\n", min | |
| printf "性能波动: %.2f%%\n", (max-min)/max*100 | |
| }' $RESULTS_FILE | |
| # echo "[*] Restoring AllowedCPUs..." | |
| # systemctl revert init.scope | |
| # systemctl revert system.slice | |
| # systemctl revert user.slice | |
| # systemctl set-property --runtime init.scope AllowedCPUs=0-32 | |
| # systemctl set-property --runtime system.slice AllowedCPUs=0-32 | |
| # systemctl set-property --runtime user.slice AllowedCPUs=0-32 | |
| $SCRIPTPATH/benchmark-restore.sh | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment