1. 准备工作
准备CentOS、Ubuntu测试机器,CentOS选用7.9版本,Ubuntu采用22.04版本。
1.1. CentOS配置镜像源及安装基础软件
配置国内的公共镜像源,安装基础软件。
1.1.1. 配置镜像源
# 备份
$ sudo mkdir /etc/yum.repos.d/bak
$ sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
# 配置镜像源文件
$ sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
$ sudo curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
# 生成缓存
$ sudo yum makecache
1.1.2. 安装基础依赖包
# 安装开发工具包
$ sudo yum groupinstall -y "Development Tools"
$ sudo yum install -y wget
1.2. Ubuntu配置镜像源及安装基础软件
1.2.1. 配置镜像源
# 替换为阿里云镜像源
$ sudo sed -i 's/https:\/\/archive.ubuntu.com/http:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list
1.2.2. 安装基础依赖包
# 安装构建工具包
$ sudo apt-get install -y build-essential
可用镜像源链接:
2. CPU性能测试
CPU性能测试工具常用的有SuperPI,Sysbench等。
2.1. SuperPI测试
SuperPI是著名的圆周率计算软件,通常用于CPU的单线程性能对比测试。
2.1.1. 安装及测试
# 创建下载安装目录
$ git clone https://github.com/Fibonacci43/SuperPI.git
# 或者使用国内的代码仓库
$ git clone https://gitee.com/lg19891024/SuperPI.git
$ cd SuperPI/
$ ls
CMakeLists.txt fftsg_h.c install.txt Makefile pi_fftcs.c README.md readme.txt
$ gcc -O -funroll-loops -fomit-frame-pointer pi_fftcs.c fftsg_h.c -lm -o pi_css5
# 编译后文件如下,生成了pi_css5
$ ls
CMakeLists.txt fftsg_h.c install.txt Makefile pi_css5 pi_fftcs.c README.md readme.txt
# 执行测试。将计算圆周率小数点后2的26次方位的数值,即64M(6700万)位。
$ ./pi_css5 $((1<<26)) # 等同于 ./pi_css5 67108864
2.1.2. 结果分析
$ ./pi_css5 $((1<<26))
Calculation of PI using FFT and AGM, ver. LG1.1.2-MP1.5.2a.memsave
initializing...
nfft= 16777216
radix= 10000
error_margin= 0.365078
calculating 67108864 digits of PI...
AGM iteration
precision= 48: 12.31 sec
precision= 80: 11.61 sec
precision= 176: 11.63 sec
precision= 352: 12.28 sec
precision= 688: 11.99 sec
precision= 1392: 11.68 sec
precision= 2784: 12.12 sec
precision= 5584: 14.04 sec
precision= 11168: 13.61 sec
precision= 22336: 13.58 sec
precision= 44688: 13.30 sec
precision= 89408: 13.26 sec
precision= 178816: 12.34 sec
precision= 357648: 12.45 sec
precision= 715312: 12.10 sec
precision= 1430640: 12.62 sec
precision= 2861280: 12.06 sec
precision= 5722592: 11.74 sec
precision= 11445200: 11.57 sec
precision= 22890416: 11.84 sec
precision= 45780848: 12.16 sec
precision= 91561728: 11.88 sec
writing pi67108864.txt...
306.04 sec. (real time)
计算的圆周率写入到文件pi67108864.txt。计算67108864位圆周率共耗时306.04s,通过对比时间长短来比较不同机器CPU的单线程性能。
2.2. sysbench
sysbench是一个开源的、模块化的、跨平台的多线程性能测试工具,可以用来进行CPU、内存、磁盘I/O、线程、数据库的性能测试。
2.2.1. 安装
2.2.1.1. 镜像源安装
centos安装sysbench需要配置epel源。
# CentOS安装
sudo yum -y install sysbench
# Ubuntu安装
sudo apt -y install sysbench
2.2.1.2. 源码编译安装
下载编译安装
$ sudo wget https://github.com/akopytov/sysbench/archive/master.zip
$ sudo unzip master.zip
$ sudo cd master/
# 或使用国内代码仓库
$ git clone https://gitee.com/mirrors/sysbench.git
$ cd sysbench/
$ sudo ./autogen.sh
# 如果仅测试cpu性能不涉及mysql,添加下面参数。否则编译配置不通过。
$ sudo ./configure --without-mysql
# 安装
$ sudo make && sudo make install
参数解读:
$ sysbench --help
Usage:
sysbench [options]... [testname] [command]
Commands implemented by most tests: prepare run cleanup help
General options:
--threads=N number of threads to use [1]
--events=N limit for total number of events [0]
--time=N limit for total execution time in seconds [10]
--warmup-time=N execute events for this many seconds with statistics disabled before the actual benchmark run with statistics enabled [0]
--forced-shutdown=STRING number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
--thread-stack-size=SIZE size of stack per thread [64K]
--thread-init-timeout=N wait time in seconds for worker threads to initialize [30]
--rate=N average transactions rate. 0 for unlimited rate [0]
--report-interval=N periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
--report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
--debug[=on|off] print more debugging info [off]
--validate[=on|off] perform validation checks where possible [off]
--help[=on|off] print help and exit [off]
--version[=on|off] print version and exit [off]
--config-file=FILENAME File containing command line options
--luajit-cmd=STRING perform LuaJIT control command. This option is equivalent to 'luajit -j'. See LuaJIT documentation for more information
Pseudo-Random Numbers Generator options:
--rand-type=STRING random numbers distribution {uniform, gaussian, pareto, zipfian} to use by default [uniform]
--rand-seed=N seed for random number generator. When 0, the current time is used as an RNG seed. [0]
--rand-pareto-h=N shape parameter for the Pareto distribution [0.2]
--rand-zipfian-exp=N shape parameter (exponent, theta) for the Zipfian distribution [0.8]
Log options:
--verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]
--percentile=N percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
--histogram[=on|off] print latency histogram in report [off]
General database options:
--db-driver=STRING specifies database driver to use ('help' to get list of available drivers)
--db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
--db-debug[=on|off] print database-specific debug information [off]
Compiled-in database drivers:
Compiled-in tests:
fileio - File I/O test
cpu - CPU performance test
memory - Memory functions speed test
threads - Threads subsystem performance test
mutex - Mutex performance test
See 'sysbench <testname> help' for a list of options for each test.
2.2.2. 测试
# 查看cpu测试帮助信息
$ sysbench cpu help
常用参数:
--cpu-max-prime: 素数生成数量的上限,默认为10000
--threads: 线程数,默认为1
--time: 运行时间,单位为妙,默认为10。每完成一轮素数计算为1个event,相同时间情况下比较谁的events多。
--events: 上限次数,完成events后,即使时间剩余也停止运行。相同events情况下比较谁的时间小。
# 执行测试
$ sysbench cpu --cpu-max-prime=20000 --threads=2 run
2.2.3. 结果分析
$ sysbench cpu --cpu-max-prime=2000 --threads=2 run
sysbench 1.1.0-2ca9e3f (using bundled LuaJIT 2.1.0-beta3)
Running the test with following options:
Number of threads: 2
Initializing random number generator from current time
Prime numbers limit: 2000
Initializing worker threads...
Threads started!
CPU speed:
events per second: 14323.35
Throughput:
events/s (eps): 14323.3494
time elapsed: 10.0002s
total number of events: 143236
Latency (ms):
min: 0.12
avg: 0.14
max: 12.21
95th percentile: 0.16
sum: 19917.39
Threads fairness:
events (avg/stddev): 71618.0000/908.00
execution time (avg/stddev): 9.9587/0.00
两台服务器进行CPU性能比较,当素数上限和线程数一致时:
- 相同时间,比较events;
- 相同events,比较时间;
- 时间和events都相同,比较标准差。
参考资料:
本文由mdnice多平台发布
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。