Go Notes

Test

go test -v -run=[pattern]

Benchmark

go test -v -run=bench -bench=[pattern] -cpu=[n] -benchtime=[t]

Analysis

  • race:

    go test -v -run=[pattern] -race

  • cpu:

    go test -v -run=bench -bench=[pattern] -cpu=[n] -benchtime=[t] -cpuprofile=[output]

  • mem:

    go test -v -run=bench -bench=[pattern] -cpu=[n] -benchtime=[t] -memprofile=[output]

  • block:

    go test -v -run=bench -bench=[pattern] -cpu=[n] -benchtime=[t] -blockprofile=[output]

    go tool pprof [program].test [output]

GODEBUG

runtime提供了环境变量来控制程序。GODEBUG是其中一,它可以设置很多值,格式name=value,name=val,如开启gctrace:$ GODEBUG='gctrace=1' your_program,会在标准错误输出gc信息。

JSON

推荐使用ffjson
用它替换标准库后,编解码效率提高2倍。对于一些需要编码的大对象,可以采用sync.Pool缓存一定大小的bytes.Buffer,然后直接写buffer来完成json编码,性能有一定提高,在我笔记本上测10*1024的字符串数组,大概比标准json快20%,结果受到分配buffer大小的影响。在对json对象编码的时候,如果有字符串字段,并且字符串可能出现json格式用到的字符,最好预先对字符串编码,否则json编码的时候处理字符串转义会导致性能下降,如果字符串很长,可能会成为瓶颈。

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2015-2024 RivenZoo
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信