一、命令简介
sysbench 是一个跨平台的基准测试工具,用于评估系统性能,包括 CPU、内存、文件 I/O、数据库等性能。
比较同类测试工具
bench.sh
在上文 bench.sh:Linux 服务器基准测试中介绍了 bench.sh 一键测试脚本,它对新手十分友好。一键完成测试,主打一个省心。缺点是不能设置测试参数。
sysbench
本文的 sysbench 命令填补了bench.sh的缺陷,sysbench命令则可以设置测试参数,更为灵活。
安装 sysbench
apt update
apt install sysbench
第二章“命令参数”会比较枯燥,可以直接跳到第三章,查看命令用法,以及实测结果。
二、命令参数
基本命令格式:
sysbench [测试模块] [全局选项] [命令] [测试参数]
常用测试模块:
-
cpu
:CPU 性能测试。 -
memory
:内存测试。 -
fileio
:文件 I/O 性能测试。 -
threads
:线程性能测试。 -
mutex
:互斥锁测试。 -
oltp
:OLTP(数据库)测试。
常用的全局选项:
-
--threads=N
:设置并发线程数,N
是线程数,默认是 1。 -
--time=N
:设置测试运行的时间(秒),默认是无限制。 -
--events=N
:设置最大事件数,测试在事件数达到 N 后结束。 -
--report-interval=N
:设置每 N 秒打印一次报告。 -
--rate=N
:每秒产生的事件数。
Sysbench 命令:
-
prepare
:准备测试环境(如创建测试文件)。 -
run
:运行测试。 -
cleanup
:清理测试环境(如删除测试文件)。
三、命令示例
准备了三台主机,电视盒子是arm架构,其余是x86架构,使用sysbench命令分别测试不同单元的跑了,并记录。
虚拟机:
- 2C2G
- CPU:i7 6700hq
- ubuntu 22.04
电视盒子:
- 4C2G
- CPU:晶晨s905l3a。
- 系统:armbian(ubuntu24.04)
云服务器:
- 4C4G
- CPU:未知
- 系统:ubuntu 22.04
1. CPU 性能测试
实际是测试 CPU 计算质数的性能,所以不能完全代表CPU的综合性能。
单核性能
sysbench cpu --cpu-max-prime=20000 run
-
--cpu-max-prime=20000
表示计算 20,000 以内的质数。 -
run
表示运行测试。
测试结果
- 虚拟机
CPU speed:
events per second: 458.04
General statistics:
total time: 10.0020s
total number of events: 4582
Latency (ms):
min: 2.09
avg: 2.18
max: 3.46
95th percentile: 2.30
sum: 9999.88
Threads fairness:
events (avg/stddev): 4582.0000/0.00
execution time (avg/stddev): 9.9999/0.00
- 电视盒子
CPU speed:
events per second: 375.26
General statistics:
total time: 10.0016s
total number of events: 3755
Latency (ms):
min: 2.66
avg: 2.66
max: 4.24
95th percentile: 2.66
sum: 9999.37
Threads fairness:
events (avg/stddev): 3755.0000/0.00
execution time (avg/stddev): 9.9994/0.00
- 云服务器
CPU speed:
events per second: 337.99
General statistics:
total time: 10.0010s
total number of events: 3381
Latency (ms):
min: 2.89
avg: 2.96
max: 4.12
95th percentile: 3.02
sum: 9995.00
Threads fairness:
events (avg/stddev): 3381.0000/0.00
execution time (avg/stddev): 9.9950/0.00
多核性能
sysbench cpu --cpu-max-prime=20000 --theads=核心数 run
把“核心数”换成你的服务器核心数,比如2,4,8。
测试结果:虚拟机>电视盒子>云服务器
- 虚拟机
CPU speed:
events per second: 858.40
General statistics:
total time: 10.0020s
total number of events: 8587
Latency (ms):
min: 2.22
avg: 2.33
max: 12.45
95th percentile: 2.43
sum: 19998.65
Threads fairness:
events (avg/stddev): 4293.5000/0.50
execution time (avg/stddev): 9.9993/0.00
- 电视盒子
CPU speed:
events per second: 1493.22
General statistics:
total time: 10.0026s
total number of events: 14943
Latency (ms):
min: 2.66
avg: 2.68
max: 18.54
95th percentile: 2.71
sum: 39998.98
Threads fairness:
events (avg/stddev): 3735.7500/8.20
execution time (avg/stddev): 9.9997/0.0
- 云服务器
CPU speed:
events per second: 1339.86
General statistics:
total time: 10.0027s
total number of events: 13405
Latency (ms):
min: 2.89
avg: 2.98
max: 4.71
95th percentile: 3.13
sum: 39989.84
Threads fairness:
events (avg/stddev): 3351.2500/13.35
execution time (avg/stddev): 9.9975/0.00
2. 内存性能测试
测试内存的读写速度:
sysbench memory --memory-block-size=1M --memory-total-size=10G run
-
--memory-block-size=1M
:每次内存读写的块大小为 1 MB。 -
--memory-total-size=10G
:总共测试 10 GB 的数据量。
测试结果:虚拟机>云服务器>电视盒子
- 虚拟机
10240.00 MiB transferred (18088.04 MiB/sec)
General statistics:
total time: 0.5645s
total number of events: 10240
Latency (ms):
min: 0.05
avg: 0.05
max: 0.19
95th percentile: 0.08
sum: 561.64
Threads fairness:
events (avg/stddev): 10240.0000/0.00
execution time (avg/stddev): 0.5616/0.00
- 电视盒子
10240.00 MiB transferred (4792.52 MiB/sec)
General statistics:
total time: 2.1318s
total number of events: 10240
Latency (ms):
min: 0.21
avg: 0.21
max: 0.37
95th percentile: 0.21
sum: 2126.68
Threads fairness:
events (avg/stddev): 10240.0000/0.00
execution time (avg/stddev): 2.1267/0.00
- 云服务器
10240.00 MiB transferred (14686.15 MiB/sec)
General statistics:
total time: 0.6950s
total number of events: 10240
Latency (ms):
min: 0.06
avg: 0.07
max: 0.17
95th percentile: 0.08
sum: 690.30
Threads fairness:
events (avg/stddev): 10240.0000/0.00
execution time (avg/stddev): 0.6903/0.00
3. 文件 I/O 性能测试
首先需要生成测试文件,然后运行 I/O 性能测试:
创建测试文件:
sysbench fileio --file-total-size=5G prepare
-
--file-total-size=5G
:生成一个 5 GB 的文件来测试。
写入速度:
- 虚拟机
5368709120 bytes written in 4.63 seconds (1106.91 MiB/sec).
- 电视盒子
内置mmc硬盘太小,无法完成测试。
- 云服务器
5368709120 bytes written in 155.73 seconds (32.88 MiB/sec).
运行 I/O 测试:
sysbench fileio --file-total-size=5G --file-test-mode=rndrw --max-time=60 --max-requests=0 run
-
--file-test-mode=rndrw
:以随机读写模式测试。 -
--max-time=60
:测试持续时间为 60 秒。 -
--max-requests=0
:请求次数不限。
测试结果:虚拟机>云服务器>电视盒子
- 虚拟机
File operations:
reads/s: 5385.18
writes/s: 3590.12
fsyncs/s: 11488.40
Throughput:
read, MiB/s: 84.14
written, MiB/s: 56.10
General statistics:
total time: 60.0076s
total number of events: 1227881
Latency (ms):
min: 0.00
avg: 0.05
max: 28.11
95th percentile: 0.08
sum: 59288.93
Threads fairness:
events (avg/stddev): 1227881.0000/0.00
execution time (avg/stddev): 59.2889/0.00
- 电视盒子
内置mmc硬盘太小,无法完成测试。
- 云服务器
File operations:
reads/s: 35.77
writes/s: 23.85
fsyncs/s: 76.33
Throughput:
read, MiB/s: 0.56
written, MiB/s: 0.37
General statistics:
total time: 60.3794s
total number of events: 8081
Latency (ms):
min: 0.00
avg: 7.47
max: 11084.39
95th percentile: 0.17
sum: 60354.33
Threads fairness:
events (avg/stddev): 8081.0000/0.00
execution time (avg/stddev): 60.3543/0.00
清理测试文件:
sysbench fileio --file-total-size=5G cleanup
4. 数据库测试 (MySQL)
需要先配置 MySQL 相关选项,然后运行基准测试。
安装MySQL
sudo apt update
sudo apt install mysql-client-core-8.0 -y
sudo apt install mysql-server-8.0 -y
登录MySQL
sudo mysql -u root -p
创建测试数据库
CREATE DATABASE testdb;
退出MySQL
exit
创建测试表:
sysbench oltp_read_write --mysql-host=localhost --mysql-user=root --mysql-password=yourpassword --mysql-db=testdb --tables=10 --table-size=10000 prepare
创建速度:虚拟机>电视盒子>云服务器
运行读写测试:
sysbench oltp_read_write --mysql-host=localhost --mysql-user=root --mysql-password=yourpassword --mysql-db=testdb --tables=10 --table-size=10000 --threads=8 --time=60 run
-
--tables=10
:测试表的数量。 -
--table-size=10000
:每个表的行数。 -
--threads=8
:使用 8 个线程。 -
--time=60
:测试持续时间为 60 秒。
清理测试表:
sysbench oltp_read_write --mysql-host=localhost --mysql-user=root --mysql-password=yourpassword --mysql-db=testdb cleanup
测试结果:虚拟机>电视盒子>云服务器
- 虚拟机
SQL statistics:
queries performed:
read: 360430
write: 102980
other: 51490
total: 514900
transactions: 25745 (428.94 per sec.)
queries: 514900 (8578.78 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 60.0187s
total number of events: 25745
Latency (ms):
min: 5.67
avg: 18.64
max: 104.16
95th percentile: 27.66
sum: 479997.75
Threads fairness:
events (avg/stddev): 3218.1250/14.90
execution time (avg/stddev): 59.9997/0.01
- 电视盒子
SQL statistics:
queries performed:
read: 117348
write: 33528
other: 16764
total: 167640
transactions: 8382 (139.60 per sec.)
queries: 167640 (2791.97 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 60.0391s
total number of events: 8382
Latency (ms):
min: 23.51
avg: 57.28
max: 219.09
95th percentile: 92.42
sum: 480099.51
Threads fairness:
events (avg/stddev): 1047.7500/5.21
execution time (avg/stddev): 60.0124/0.01
- 云服务器
SQL statistics:
queries performed:
read: 6818
write: 1948
other: 974
total: 9740
transactions: 487 (7.98 per sec.)
queries: 9740 (159.60 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 61.0263s
total number of events: 487
Latency (ms):
min: 89.65
avg: 999.24
max: 5688.30
95th percentile: 3841.98
sum: 486629.54
Threads fairness:
events (avg/stddev): 60.8750/1.69
execution time (avg/stddev): 60.8287/0.07
这台云服务器实在太垃圾了,虽然一年的价格只要20块。CPU和数据库性能被电视盒子暴打。此外它是纯ipv6,使用起来颇多不便。只能说不测不知道,一测吓一跳。
电视盒子(armbian Linux)
本文共 877 个字数,平均阅读时长 ≈ 3分钟
评论 (0)