(1) shell内置time指令
这个方法不算新颖,但是确很实用。 time
是Unix/Linux内置多命令,使用时一般不用传过多参数,直接跟上需要调试多程序即可。
$ time go run test2.go
&{{0 0} 张三 0}
real 0m0.843s
user 0m0.216s
sys 0m0.389s
上面是使用time对 go run test2.go
对执行程序坐了性能分析,得到3个指标。
real
:从程序开始到结束,实际度过的时间;user
:程序在用户态度过的时间;sys
:程序在内核态度过的时间。
一般情况下 real
>=user
+ sys
,因为系统还有其它进程(切换其他进程中间对于本进程会有空白期)。
(2) /usr/bin/time指令
这个指令比内置的time更加详细一些,使用的时候需要用绝对路径,而且要加上参数-v
$ /usr/bin/time -v go run test2.go
Command being timed: "go run test2.go"
User time (seconds): 0.12
System time (seconds): 0.06
Percent of CPU this job got: 115%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.16
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 41172
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 1
Minor (reclaiming a frame) page faults: 15880
Voluntary context switches: 897
Involuntary context switches: 183
Swaps: 0
File system inputs: 256
File system outputs: 2664
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
可以看到这里的功能要强大多了,除了之前的信息外,还包括了:
- CPU占用率;
- 内存使用情况;
- Page Fault 情况;
- 进程切换情况;
- 文件系统IO;
- Socket 使用情况;
- ……