01、为什么要学习性能统计
性能测试
性能分析
02、性能统计知识
系统级性能数据分析
进程级别性能数据分析
03、常用性能指标
cpu 代表算法的高效性
mem 代表数据结构的使用合理性
net io 等更多指标
net io 的不合理使用同样会在 cpu 和 mem 上体现出影响,所以我们今天重点介绍 3 个指标,cpu mem 与 net
04、统计方法
linux命令,临时性分析:命令交互,优点是方便实时查看,做临时性分析,缺点是不利于长期保存数据
系统性分析:promethus(数据库)+grafana(可视化展示平台)
05、知识点深度声明
因为这是 linux 与 bash 的入门章节,所以我们先不涉及更深入的性能指标,
先从入门开始学习。更多知识课可参考霍格沃兹测试开发学社独立的性能测试课程。
06、CPU 使用统计
cpu 信息 /proc/cpuinfo
查看CPU信息命令:cat /proc/cpuinfo,结果信息如下:
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 79
model name : Intel(R) Xeon(R) CPU E5-2682 v4 @ 2.50GHz
microcode : 0x1
cpu MHz : 2499.994
cache size : 40960 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
clflush size : 64
cache_alignment : 64
address sizes : 46 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
07、系统负载与进程 cpu 占用 top
查看系统负载与进程命令:top
top - 17:27:57 up 76 days, 18:17, 1 user, load average: 1.03, 1.11, 1.15
Tasks: 215 total, 2 running, 131 sleeping, 0 stopped, 0 zombie
%Cpu(s): 28.7 us, 0.4 sy, 0.0 ni, 70.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16122020 total, 1343832 free, 4715324 used, 10062864 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 11060040 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23508 yangche+ 20 0 668504 114388 30628 R 100.0 0.7 2872:02 /usr/local+
11120 root 10 -10 152468 34444 9120 S 10.0 0.2 2648:51 /usr/local+
11634 yangtie+ 20 0 1101696 123748 12648 S 2.0 0.8 53:58.88 /home/yang+
30939 yangtie+ 20 0 1525284 95928 17472 S 1.3 0.6 27:44.85 /home/yang+
658 root 20 0 306560 170916 170332 S 0.7 1.1 116:29.63 /usr/lib/s+
5541 yangtie+ 20 0 447548 42472 12632 S 0.7 0.3 8:15.91 /home/yang+
5713 quanjin+ 20 0 7850436 2.2g 8648 S 0.7 14.0 583:19.28 java -Duse+
7427 yangche+ 20 0 7796228 225700 9420 S 0.7 1.4 537:38.47 java -jar +
08、cpu 的关键指标
cpu 利用率 进程的 cpu 利用情况
load average 系统负载情况
ps 命令的 cpu 是平均 cpu 利用率,不适合做性能分析
09、内存(MEM)占用统计
常用命令
free
ps
top
10、free 输出
total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
used Used memory (calculated as total - free - buffers - cache)
free Unused memory (MemFree and SwapFree in /proc/meminfo)
shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available, on kernels 2.6.32,
displayed as zero if not available)
buffers Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and Slab in /proc/meminfo)
buff/cache Sum of buffers and cache
available Estimation of how much memory is available for starting new applications,
without swapping. not all reclaimable memory slabs will be reclaimed due to items being in use
11、meminfo
/proc/meminfo 保存了更多的内存使用数据
cat /proc/meminfo
MemTotal: 3881920 kB
MemFree: 132204 kB
MemAvailable: 1073044 kB
Buffers: 561564 kB
Cached: 2307572 kB
SwapCached: 0 kB
Active: 795200 kB
Inactive: 2358704 kB
Active(anon): 376576 kB
Inactive(anon): 2035324 kB
Active(file): 418624 kB
Inactive(file): 323380 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
...
12、进程级别的内存分析
ps -e -o uid,pid,ppid,pcpu,pmem,rss,vsz,comm --sort -%mem | head -10
UID PID PPID %CPU %MEM RSS VSZ COMMAND
6597 12059 1 0.0 1.9 77036 3584588 java
5729 32380 1 0.0 1.9 74608 3518808 java
0 29821 1 1.5 0.9 38768 161384 AliYunDun
0 457 1 0.2 0.9 37816 1053844 CmsGoAgent.linu
0 340 1 0.0 0.7 27628 62708 systemd-journal
0 12047 1 0.0 0.4 19264 396524 rsyslogd
0 28845 1 0.5 0.4 16092 719100 node_exporter
0 10454 1 0.0 0.2 11408 564716 tuned
998 10439 1 0.0 0.2 8788 710032 polkitd
13、top 命令
top - 17:17:49 up 325 days, 5:59, 5 users, load average: 0.00, 0.01, 0.05
Tasks: 92 total, 1 running, 91 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3881920 total, 134192 free, 372640 used, 3375088 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 1071332 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12059 dx42310+ 20 0 3584588 77036 0 S 0.3 2.0 35:01.64 java
32380 dx53889 20 0 3518808 74608 1056 S 0.0 1.9 19:20.34 java
29821 root 10 -10 161384 38768 5788 S 0.3 1.0 758:25.24 AliYunDun
457 root 20 0 1053844 37680 5208 S 0.0 1.0 993:28.66 CmsGoAgent+
340 root 20 0 62708 27876 27244 S 0.0 0.7 193:01.35 systemd-jo+
12047 root 20 0 396524 19408 15780 S 0.0 0.5 54:24.50 rsyslogd
28845 root 20 0 719100 16160 4376 S 0.0 0.4 1263:32 node_expor+
10454 root 20 0 564716 11408 712 S 0.0 0.3 18:45.31 tuned
10439 polkitd 20 0 710032 8788 1172 S 0.0 0.2 8:42.55 polkitd
17217 root 20 0 155268 5924 4476 S 0.0 0.2 0:00.03 sshd
14、网络连接(Net)统计
查看网络连接
netstat -tnp | head -10
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.17.56.105:46828 39.102.48.202:63791 ESTABLISHED 11634/python3
tcp 0 0 172.17.56.105:22 221.216.138.35:59149 ESTABLISHED 12510/sshd: huangya
tcp 0 0 172.17.56.105:47020 39.102.48.202:63791 ESTABLISHED 11634/python3
tcp 0 0 172.17.56.105:42122 100.100.18.120:443 TIME_WAIT -
tcp 0 0 172.17.56.105:51976 39.102.48.202:63791 ESTABLISHED 11634/python3
tcp 0 0 172.17.56.105:46270 39.102.48.202:63791 ESTABLISHED 6602/python3
tcp 0 0 172.17.56.105:45318 100.100.105.70:80 TIME_WAIT -
tcp 0 0 172.17.56.105:41300 39.102.48.202:63791 ESTABLISHED 17105/python3
15、查看网络连接
命令:netstat -tlnp
命令:netstat -tnp | head -10
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.17.56.105:46828 39.102.48.202:63791 ESTABLISHED 11634/python3
tcp 0 0 172.17.56.105:22 221.216.138.35:59149 ESTABLISHED 12510/sshd: huangya
tcp 0 0 172.17.56.105:47020 39.102.48.202:63791 ESTABLISHED 11634/python3
tcp 0 0 172.17.56.105:42122 100.100.18.120:443 TIME_WAIT -
tcp 0 0 172.17.56.105:51976 39.102.48.202:63791 ESTABLISHED 11634/python3
tcp 0 0 172.17.56.105:46270 39.102.48.202:63791 ESTABLISHED 6602/python3
tcp 0 0 172.17.56.105:45318 100.100.105.70:80 TIME_WAIT -
tcp 0 0 172.17.56.105:41300 39.102.48.202:63791 ESTABLISHED 17105/python3
16、数据统计
netstat -tn | awk 'NR>2{print $NF}'| sort | uniq -c | sort -nr
31 ESTABLISHED
7 TIME_WAIT
2 CLOSE_WAIT
17、性能命令
top: 持续监视系统性能(实时监控)
ps: 查看进程信息
-aux显示所有进程,包括用户,分组情况
18、cd ~ 表示切换到当前用户的家目录
cd - 表示切换到上一次所在的目录
19、如何查看某个进程里面的数据?
首先用top命令查看PID
然后使用命令:ls /proc/21391
20、buffer 缓冲存储器,缓存
cache 高速缓存
cookie 缓存文件
21、如何查看系统内存?
1、free命令
2、ls /proc/meminfo
22、进程级别的内存分析
首先用top命令查看PID
然后使用命令:ls /proc/21391