在 Python 中测量性能
我正在用 Python 编写一个 Web 应用程序,我还没有决定是否要使用 Flask、web.py 还是其他东西,并且我希望能够对实时应用程序进行配置文件。
关于如何实施仪器来进行性能测量的信息似乎很少,除了到处进行大量打印 datetime.now() 之外。
检测 Python 应用程序以进行良好测量的最佳方法是什么?我想我正在寻找类似于 Stackoverflow 团队 mvc-mini-profiler 的东西。
I'm writing a web-application in Python, I haven't decided if I want to use Flask, web.py or something else yet, and I want to be able to do profile on the live application.
There seems to be very little information on how you go about implementing the instrumentation to do performance measurement, short of doing a lot of print datetime.now() everywhere.
What is the best way of going about instrumenting your Python application to allow good measurements to be made. I guess I'm looking for something similar to the Stackoverflow teams mvc-mini-profiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以简单地运行 Python 附带的 cProfile 工具:
当然,您必须创建 script.py 文件来执行您想要测试的代码部分。如果您有一些单元测试,您也可以使用它。
或者您可以使用:
从 foo 入口点对其进行分析。
You could simply run cProfile tool that comes with Python:
Of course, you would have to create the script.py file that would execute the parts of the code that you want to test. If you had some unit tests, you could also use that.
Or you couse use:
to profile it from
foo
entry point.Amir Salihefendic 写了一篇简短的文章 (150 LOC)
RequestProfiler
,本博文对此进行了描述:我还没有尝试过,但由于它是一个 WSGI 中间件,所以它应该是可插拔的。
Amir Salihefendic wrote a short (150 LOC)
RequestProfiler
, which is described in this blog post:I haven't tried it, but since it is a WSGI middleware, it should be somewhat pluggable.
您可以只使用通用 Web 应用程序性能工具,例如 httpperf。这可以使用外部客户端并与任何框架一起使用,因为它针对标准接口(HTTP)工作。因此它测试了全栈性能。
You can just use a general purpose web application performance tool, such as httpperf. This works using an external client and works with any framework since it works against a standard interface (HTTP). Therefore it tests the full stack performance.
使用New Relic的免费监控系统。您只需在服务器上安装一个代理并指向您的 Flask init.py 文件。一旦您使用正确的代理设置运行应用程序,您将开始在 New Relic 的名为 APM 的在线仪表板中看到应用程序指标。
默认情况下,它将向您显示应用程序吞吐量(QPS/RPM)、应用程序响应时间、最高事务、错误率、错误堆栈跟踪(如果有)(例如 500 错误)、对外部服务的调用等的图表。此外,您还可以监控您的系统统计数据也是如此。
Use New Relic's Free monitoring system. You simply install an agent on the server and point to your flask init.py file. Once you run the application with proper agent setup, you will start seeing application metrics in see New Relic's online dashboard called APM.
By default it will show you graphs of your application's throughput (QPS/RPM), app response time, top transactions, error rate, error stack trace if any(eg for 500 error), calls to external services etc. In addition you can monitor your System stats too.