前言

Linux性能测试、监控、优化是一个持续的过程,上图为LinuxCon上Brendan D. Gregg分享的Linux benchmarking tools示意图,涵盖面十分广泛。我们可以通过成熟的监控方案如Prometheus,Zabbix来捕获大部分信息,在实际工作中我们会经常关注CPU、Memory、I/O、Network等性能问题,本文增加了倪朋飞在极客时间出品的《Linux性能优化实战》专栏课程学习笔记。

Donald Knuth说”过早优化是万恶之源”(premature optimization is the root of all evil)


更新记录

2022年10月18日 - 增加Linux性能优化实战
2022年08月31日 - 增加bench.sh和yabs.sh
2015年03月06日 - 初稿

阅读原文 - https://wsgzao.github.io/post/linux-performance/

扩展阅读

Linux Performance - http://www.brendangregg.com/linuxperf.html


CPU

确认CPU型号

1
2
3
4
cat /proc/cpuinfo |grep "model name"|uniq|cut -f2 -d:

Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz

Super PI

计算时间越短越好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

time echo "scale=500;4*a(1)"|bc -l -q

3.141592653589793238462643383279502884197169399375105820974944592307\
81640628620899862803482534211706798214808651328230664709384460955058\
22317253594081284811174502841027019385211055596446229489549303819644\
28810975665933446128475648233786783165271201909145648566923460348610\
45432664821339360726024914127372458700660631558817488152092096282925\
40917153643678925903600113305305488204665213841469519415116094330572\
70365759591953092186117381932611793105118548074462379962749567351885\
75272489122793818301194912

real 0m0.081s
user 0m0.076s
sys 0m0.000s

Disk

清空缓存

每次做读写测试前建议先清空缓存

1
sync; echo 3 > /proc/sys/vm/drop_caches

测试读性能

选择测试磁盘,建议做2-3组取平均值

1
2
3
4
hdparm -t /dev/sda

/dev/sda:
Timing buffered disk reads: 1074 MB in 3.00 seconds = 357.92 MB/sec

测试写入性能

根据业务选择不同的BlockSize大小按需多次测试取平均值

1
2
time dd if=/dev/zero of=/tmp/speed bs=1M count=2K conv=fsync;rm /tmp/speed

参考数据

以10,000 rpm 300 GB SAS硬盘为例,机型为IBM x3650 M4,Raid参数如下

1.Read Policy:Ahead (控制器缓存读策略:预读)
2.Write Policy:Write Back with BBU(控制器缓存写策略:有电池备份时回写)
3.IO Policy: Direct(IO策略:直接)
4.Drive Cache:disable (硬盘缓存:禁用)

Raid Read(MB) Write(MB)
Raid 1 170 130
Raid 5 350 250
Raid 10 300 215

开源的一键测试工具

bench.sh

一键测试脚本bench.sh

1、显示各种系统信息;
2、取自 Speedtest 世界多处的数据中心的测试点,网络测试比较全面;
3、支持 IPv6 下载测速;
4、IO 测试(顺序写入 1GB 数据)三次,并显示其平均值。

再配合 unixbench.sh 脚本测试,即可全面测试 VPS 的性能。
https://teddysun.com/245.html

Description: Auto test I/O & upload & download speed script
Intro: https://teddysun.com/444.html

1
2
3
4
5
wget -qO- bench.sh | bash

curl -Lso- bench.sh | bash


yabs.sh

Here’s an attempt to create yet another damn Linux server benchmarking script.

https://github.com/masonr/yet-another-bench-script

1
2
3
4
curl -sL yabs.sh | bash

wget -qO- yabs.sh | bash

This script has been tested on the following Linux distributions: CentOS 6+, Debian 8+, Fedora 30, and Ubuntu 16.04+. It is designed to not require any external dependencies to be installed nor elevated privileges to run.

Local fio/iperf3 Packages: If the tested system has fio and/or iperf3 already installed, the local package will take precedence over the precompiled binary.

Experimental ARM Compatibility: Initial ARM compatibility has been introduced, however, is not considered entirely stable due to limited testing on distinct ARM devices. Report any errors or issues.

High Bandwidth Usage Notice: By default, this script will perform many iperf network tests, which will try to max out the network port for ~20s per location (10s in each direction). Low-bandwidth servers (such as a NAT VPS) should consider running this script with the -r flag (for reduced iperf locations) or the -i flag (to disable network tests entirely).

VPS常用测试工具

VPS常用测试脚本
https://zhuanlan.zhihu.com/p/117547388

流媒体检测脚本
https://github.com/sjlleo/netflix-verify
https://github.com/lmc999/RegionRestrictionCheck

Linux性能优化实战

Linux 性能优化实战
倪朋飞 资深 Linux 专家,Kubernetes 项目维护者
https://time.geekbang.org/column/intro/100020901

Linux性能优化实战笔记

Linux performance optimization
https://feiyang233.club/post/linux/

文章目录
  1. 1. 前言
  2. 2. 更新记录
  3. 3. CPU
    1. 3.1. 确认CPU型号
    2. 3.2. Super PI
  4. 4. Disk
    1. 4.1. 清空缓存
    2. 4.2. 测试读性能
    3. 4.3. 测试写入性能
    4. 4.4. 参考数据
  5. 5. 开源的一键测试工具
    1. 5.1. bench.sh
    2. 5.2. yabs.sh
    3. 5.3. VPS常用测试工具
  6. 6. Linux性能优化实战