使 PHP 性能分析变得可预测

发布于 2024-09-19 23:49:15 字数 183 浏览 13 评论 0原文

我正在使用 xdebug 和 PHP 来进行一些性能分析。但是当我多次运行同一个脚本时,我经常会得到非常不同的时间。因此很难知道对结果有多少信心。

显然,机器上发生的很多事情都会影响 PHP 的性能。但是我能做些什么来减少变量的数量,以便多次测试更加一致吗?

我在 Mac OS X 上的 Apache 下运行 PHP。

I'm using xdebug with PHP to do some performance profiling. But when I run the same script more than once, I often get very different times. So it's hard to know how much faith to put in the results.

Obviously there's a lot happening on a machine that can affect PHP's performance. But is there anything I can do to reduce the number of variables, so multiple tests are more consistent?

I'm running PHP under Apache, on Mac OS X.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日 2024-09-26 23:49:15
  1. 尽可能减少盒子上无关的服务数量。
  2. 减少 Apache 进程的数量。
  3. 通过加载脚本几次来准备各种缓存。可以使用 Apache 的 absiege,以确保所有阿帕奇孩子都被击中。
  4. 使用 curlwget 从命令行分析脚本,以便 Apache 只提供一种资源:脚本本身。

可能有人认为通过省略其中一些步骤来获得更多“现实世界”的数字。我期待这个问题可能收到的其他答案。

  1. Reduce the number of unrelated services on the box as much as possible.
  2. Cut down on the number of Apache processes.
  3. Prime the various caches by loading your script a few times. Possibly use a benchmarking tool like Apache's ab or siege, to make sure all Apache children are hit.
  4. Profile your script from the command line using curl or wget so that Apache only serves one resource: the script itself.

There may be an argument for getting more "real world" numbers by omitting some of these steps. I look forward to other answers this question may receive.

谁与争疯 2024-09-26 23:49:15

有两个不同的任务:衡量性能和发现问题。

为了测量所花费的时间,您应该预期可变性,因为它取决于机器中发生的其他情况。这很正常。

为了发现问题,您需要知道各种活动所用时间的百分比。作为其他因素的函数,百分比不会发生太大变化,而且百分比的确切值无论如何也没有多大关系。

重要的是你找到了能带来健康百分比的活动,你可以修复它们,然后修复它们。当您这样做时,您可以期望节省高达该百分比的时间,但发现的结果正是您需要做的。测量是次要的。

补充:您可能想问“难道您不需要测量才能找到吗?”
考虑一个例子。假设您在打开调试的情况下运行程序,然后随机暂停它,并且您在关闭日志文件的过程中看到它。你继续,然后再次暂停,看到同样的事情。粗略的“测量”表明它花费了 100% 的时间来做这件事。当然,花在这件事上的时间并不是真正的 100%,但无论它是什么,它都很大,而且你已经找到了。那么也许您不必如此频繁地打开/关闭文件,或者其他什么。通常,需要更多样本,但不要太多。

There are two different tasks, measuring performance and finding problems.

For measuring the time it takes, you should expect variability, because it depends on what else is going on in the machine. That's normal.

For finding problems, what you need to know is the percent of time used by various activities. Percent doesn't change too much as a function of other things, and the exact value of the percent doesn't matter much anyway.

What matters is that you find activities responsible for a healthy percent, that you can fix, and then that you fix them. When you do, you can expect to save time up to that percent, but the finding is what you need to do. The measuring is secondary.

Added: You might want to ask "Don't you have to measure in order to find?"
Consider an example. Suppose you run your program with debugging turned on, and you randomly pause it, and you see it in the process of closing a log file. You continue it, and then pause it again, and see the same thing. Well that rough "measurement" says it's spending 100% of its time doing that. Naturally, the time spent doing it isn't really 100%, but whatever it is, it's big, and you've found it. So then maybe you don't have to open/close the file so often, or something. Typically, more samples are needed, but not too many.

泛滥成性 2024-09-26 23:49:15
  1. 正如其他人所说,将正在运行的服务和程序减少到最低限度
  2. 连续多次运行测试并取平均值以考虑异常值
  3. 确保禁用任何类型的缓存(除非您特别想使用缓存进行测试)
  4. 如果结果仍然存在很大差异,问题很可能出在您的分析代码中。它可能有一些竞争条件或取决于网络连接。如果您提供代码,您将获得更多详细信息
  5. 。您还可能在某些运行中遇到一些瓶颈。如果您仔细分析脚本的不同部分,您也许能够捕捉到它。
  1. As others have said, reduce the running services and programs to a minimum
  2. Run your test multiple times in succession and average to account for outliers
  3. Make sure caching of any sort is disabled (unless you specifically want to test it with a cache)
  4. If the results still vary widely, the problem is most likely in your profiling code. It might have some racing conditions or depends on network connections. You will get more details if you provide the code
  5. You also might be hitting some bottlenecks on some of the runs. If you profile carefully different parts of the scripts you might be able to catch it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文