翻译13.10

This commit is contained in:
dake
2015-11-14 23:46:57 +08:00
parent 145cb40146
commit 213fa81c41

View File

@@ -1,2 +1,24 @@
# 13.10 性能调试:分析并优化 Go 程序 # 13.10 性能调试:分析并优化 Go 程序
## 13.10.1 时间和内存消耗
可以用这个便捷脚本 *xtime* 来测量:
```sh
#!/bin/sh
/usr/bin/time -f %Uu %Ss %er %MkB %C$@
```
在 Unix 命令行中像这样使用 ```xtime goprogexec```,这里的 progexec 是一个 Go 可执行程序这句命令行输出类似56.63u 0.26s 56.92r 1642640kB progexec分别对应用户时间系统时间实际时间和最大内存占用。
## 13.10.2 用 go test 调试
如果代码使用了 Go 中 testing 包的基准测试功能,我们可以用 gotest 标准的 `-cpuprofile` 和 `-memprofile` 标志向指定文件写入 CPU 或 内存使用情况报告。
使用方式:```go test -x -v -cpuprofile=prof.out -file x_test.go```
编译执行 x_test.go 中的测试,并向 prof.out 文件中写入 cpuprofile 信息。
## 13.10.3 用 pprof 调试