树莓派性能分析脚本

脚本内容

以下是一个包含依赖项说明和安装步骤的脚本:

#!/bin/bash

# 创建日志文件
LOG_FILE="performance_test.log"
echo "System Performance Test Log" > $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
echo "Date: $(date)" >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE

# 系统信息
echo "System Information:" >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
uname -a >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE

# 检查并安装必要的工具
echo "Checking and installing necessary tools..." >> $LOG_FILE
sudo apt update >> $LOG_FILE 2>&1
sudo apt install -y sysbench >> $LOG_FILE 2>&1

# CPU 性能测试
echo "CPU Performance Test:" >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
echo "Running sysbench CPU test..." >> $LOG_FILE
sysbench cpu --cpu-max-prime=20000 run >> $LOG_FILE 2>&1
echo "-----------------------------------" >> $LOG_FILE

# 内存使用情况
echo "Memory Usage:" >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
free -h >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE

# 磁盘 I/O 测试
echo "Disk I/O Test:" >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
echo "Running dd disk write test..." >> $LOG_FILE
dd if=/dev/zero of=/tmp/output bs=8k count=10k oflag=dsync 2>> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE

# 系统负载
echo "System Load:" >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
uptime >> $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE

echo "Performance test completed. Results saved to $LOG_FILE"

依赖项说明

此脚本需要以下依赖项:

  1. sysbench:用于 CPU 性能测试。
  2. coreutils:包含 freedd 命令,用于内存和磁盘 I/O 测试。
  3. procps:包含 uptime 命令,用于查看系统负载。

安装依赖项

在运行脚本之前,请确保安装了所有必要的依赖项。你可以通过以下命令安装这些依赖项:

sudo apt update
sudo apt install -y sysbench coreutils procps

使用方法

  1. 保存脚本
    将上述脚本内容保存为一个文件,例如 performance_test.sh

  2. 赋予脚本执行权限

    chmod +x performance_test.sh
    
  3. 安装依赖项

    sudo apt update
    sudo apt install -y sysbench coreutils procps
    
  4. 运行脚本

    ./performance_test.sh
    
  5. 查看日志文件
    脚本运行完成后,查看日志文件的内容:

    cat performance_test.log
    

示例输出

运行脚本后,日志文件的内容可能如下所示:

System Performance Test Log
-----------------------------------
Date: 2025年 11月 10日 星期一 14:40:56 CST
-----------------------------------
System Information:
-----------------------------------
Linux raspberrypi 6.12.47+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.47-1+rpt1 (2025-09-16) aarch64 GNU/Linux
-----------------------------------
Checking and installing necessary tools...
-----------------------------------
...
-----------------------------------
CPU Performance Test:
-----------------------------------
Running sysbench CPU test...
sysbench 1.0.20 (using system LuaJIT 2.1.1723681758)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Prime numbers limit: 20000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:   119.70

General statistics:
    total time:                          10.0070s
    total number of events:              1199

Latency (ms):
         min:                                    8.31
         avg:                                    8.34
         max:                                   10.96
         95th percentile:                        8.43
         sum:                                10002.75

Threads fairness:
    events (avg/stddev):           1199.0000/0.00
    execution time (avg/stddev):   10.0028/0.00

-----------------------------------
Memory Usage:
-----------------------------------
               total        used        free      shared  buff/cache   available
内存:         906Mi       270Mi       279Mi        84Mi       498Mi       635Mi
交换:         905Mi          0B       905Mi
-----------------------------------
Disk I/O Test:
-----------------------------------
Running dd disk write test...
输入了 10240+0 块记录
输出了 10240+0 块记录
83886080 字节 (84 MB, 80 MiB) 已复制,0.300482 s,279 MB/s
-----------------------------------
System Load:
-----------------------------------
 14:41:06 up 9 min,  3 users,  load average: 0.79, 0.35, 0.19
-----------------------------------
posted @ 2025-11-10 14:45  aiplus  阅读(3)  评论(0)    收藏  举报
悬浮按钮示例