14.2 Go Performance Optimization

14.2 Go Performance Optimization

Optimization methods

1.减少HTTP请求数,合并CSS、JS、图片
2.使用CDN,就近访问
3.启用nginx gzip压缩,降低传输内容大小
4.优化后端api性能

api service performance optimization goals

1.线上程序是黑盒状态
2.通过性能分析,可知程序占用多少资源
3.找到系统瓶颈

go performance optimization direction

1.Cpu维度优化
2.Mem维度优化
3.锁竞争维度的优化

1.1 Performance optimization principle

1.知道程序占用了多少资源,如cpu,内存量
2.知道程序的函数占用资源比例
3.如有A,B两个数据就可以快速定位到系统瓶颈
4.通过pprof,每隔一段时间10ms采集当前堆栈信息,获取各个函数占用的cpu以及内存资源
pprof完毕后,通过对数据进行分析,形成分析报告。

CPU performance optimization

import ("runtime/pprof")//采集性能分析的数据
开始cpu性能分析,pprof.StartCPUProfile(w io.Writer)
停止CPU性能分析  pprof.StopCPUProfile()

1.1.1. PProf

Want performance optimization, first of all attention in the Go tool chain itself provided as the basis for the analysis, this article will take you to learn, use Go back garden, it involves the following:

  • runtime / pprof: acquisition program (non-Server) operating data for analysis
  • net / http / pprof: HTTP Server runtime data collection for analysis

pprof visualization and analysis tools for analyzing data performance

pprof to  profile.proto  read analyzing a sample set, and generate reports to help visualize and analyze the data (text and graphics support Report)

profile.proto Protocol Buffer v3 is a description file that describes a set of callstack and symbolization information, call stack effect is a statistical analysis of a set of samples is very common configuration file format stacktrace

1.1.2. What supports usage patterns

  • Report generation: report generation
  • Interactive terminal use: using interactive terminal
  • Web interface: Web interface

1.1.3. What you can do

  • CPU Profiling: CPU analysis, according to a certain frequency acquisition listens application CPU (including registers) to be used, the application can determine the position of the time spent in the active CPU cycles consumed
  • Memory Profiling: memory analysis, recorded at the time of application heap allocation stack traces for monitoring current and historical memory usage, as well as check for memory leaks
  • Block Profiling: Analysis obstruction, the recording position of the sync block waiting goroutine (including a timer channel)
  • Mutex Profiling: mutex analysis, competitive situation report mutex

Guess you like

Origin www.cnblogs.com/open-yang/p/11256986.html