英文:
Profiling Golang server
问题
我想对我用Go语言编写的简单Web服务器进行性能分析。它接收请求,将请求映射到Avro对象,并在一个Go协程中将其发送到Kafka。要求是它立即回答请求,并稍后将对象发送到Kafka。在本地,平均响应时间不到1毫秒。我一直在尝试使用davecheney/profile包启动脚本,并使用jmeter发送测试请求进行分析。我可以看到输出中生成了分析文件,但是无论jmeter发送请求多长时间,文件始终为空。我在Mac El Capitan上运行它。我了解到在Mac上进行性能分析可能会遇到一些问题,但在El Capitan上应该可以正常工作。你有什么建议吗?
英文:
I want to profile a simple webserver that I wrote in Go. It accepts requests, maps the request to avro object and sends it to Kafka in a go routine. The requirement is that it answers immediately and sends an object to the Kafka later. Locally it answers in under 1 ms on average. I have been trying to profile it by starting the script with davecheney/profile package and sending test requests with jmeter. I can see in the output that the profile file is generated but it remains empty, no matter how long jemeter is sending the requests. I'm running it on Mac El Capitan. I read that there were some issues with profiling on Mac but it would be working on El Capitan. Do you have any tips?
答案1
得分: 1
首先,我不确定你是否正在进行延迟分析。如果是的话,请注意Go的CPU分析器只报告函数在CPU上执行的时间,不包括休眠等待的时间。如果你真的在寻找CPU分析器,可以继续阅读。
如果你正在运行一个Web服务器,在你的导入语句中(在main()所在的文件中)添加以下内容,并重新构建:
import _ "net/http/pprof"
然后,在通过jmeter施加负载的同时,运行以下命令:
go tool pprof /path/to/webserver/binary http://<server>/debug/pprof/profile
net/http/pprof
包提供了分析钩子,允许你随时对Web服务器进行分析,即使在生产环境中也可以。不过,如果你的Web服务器需要公开访问,你可能需要使用一个不同的、受防火墙保护的端口。
英文:
Firstly, I'm not sure whether you're trying to do latency profiling. If so, be aware that Go's CPU profiler only reports time spent by a function executing on the CPU and doesn't include time spent sleeping, etc. If CPU Profiling really is what you're looking for, read on.
If you're running a webserver, just include the following in your imports (in the file where main() is) and rebuild:
import _ "net/http/pprof"
Then, while applying load through jmeter, run:
go tool pprof /path/to/webserver/binary http://<server>/debug/pprof/profile
The net/http/pprof
package provides profiling hooks that allow you to profile your webserver on demand any time, even while it's running in production. You may want to use a different, firewalled port for it, though, if your webserver is meant to be exposed publicly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论