性能与质量 | 青训营笔记

75 阅读2分钟

青训营 后端入门-go语言原理与实践 5-6-7-8节课 高质量编程简介及编码 --go test go benchmark

性能优化指南 -pprof

性能优化分析工具

part1 具体对一些结构的测试和设计提示,大小预设定,比如slice(公共底层,有时请拷贝走,让它好早早释放),字符串处理(+不好,用string.Builder(最好)和bytes.Buffer、空结构体节省空间,比如用空结构体map做set(key就是set)、(p54)atomic包来对只是维护一个变量

geektutu.com/post/hpg-slice.html 代码有些在这里。注意测试在项目中的组织结构,就是bench和test都放到_test文件,然后运行:go test -run. -v go test -bench. -benchmem -count=3,win需要比ppt演示的少等号。而且这个代码里面举了打印内存的例子,可以看看,应该是结合testing和runtime包

part2 pprof工具 runtime和http。用github.com/wolfogre/go… ,看怎么用pprof怎么找到定位,说的课程笔记不知道在哪,可以看看www.jianshu.com/p/f46906229…

http://localhost:6060/debug/pprof

命令行:go tool pprof http://localhost:6060/debug/pprof/profile?seconds=10 ,进入pprof命令行工具,可以输入一些命令比如list Eat

跳到上面简书笔记的安装,dot -version,然后web

go tool pprof http://localhost:6060/debug/pprof/block

加-http选项可以在网页看到结果(win下需比演示多了=localhost),go tool pprof -http=localhost:8080 http://localhost:6060/debug/pprof/heap view选source ,sample选inuse/alloc,一个是累计一个是申请

go tool pprof -http=localhost:8080 http://localhost:6060/debug/pprof/heap

go tool pprof -http=localhost:8080 http://localhost:6060/debug/pprof/goroutine view选火焰图flame好看

总之就是换后缀

2505讲了个view那边看不到的,可以从:6060看

顺便记下第二节课(3-4)

go语言进阶与依赖管理 进阶主要讲了goroutine和channel和sync.wg,依赖管理是go mod三个部分gomod文件,proxy仓库,命令行工具go mod init tidy

工程实践之测试 回归测试集成测试单元测试 _test.go testxxx函数 testMain 运行 go test name coverage可以看覆盖的代码行数百分比