如何对 PHP 脚本的效率进行基准测试 [英] How to benchmark efficiency of PHP script

查看:33
本文介绍了如何对 PHP 脚本的效率进行基准测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道对我的 PHP 脚本进行基准测试的最佳方法是什么.无论是 cron 作业、网页还是网络服务,都无关紧要.

I want to know what is the best way to benchmark my PHP scripts. Does not matter if a cron job, or webpage or web service.

我知道我可以使用微时间,但它真的给了我 PHP 脚本的实时时间吗?

I know i can use microtime but is it really giving me the real time of a PHP script?

我想测试和基准测试 PHP 中执行相同操作的不同函数.例如,preg_match vs strposdomdocument vs preg_match 或 preg_replace vs str_replace`

I want to test and benchmark different functions in PHP that do the same thing. For example, preg_match vs strpos or domdocument vs preg_match or preg_replace vs str_replace`

网页示例:

<?php
// login.php

$start_time = microtime(TRUE);

session_start(); 
// do all my logic etc...

$end_time = microtime(TRUE);

echo $end_time - $start_time;

这将输出:0.0146126717(一直变化 - 但这是我得到的最后一个).这意味着执行 PHP 脚本需要 0.015 左右.

This will output: 0.0146126717 (varies all the time - but that is the last one I got). This means it took 0.015 or so to execute the PHP script.

有更好的方法吗?

推荐答案

如果您真的想对现实世界的代码进行基准测试,请使用诸如 XdebugXHProf.

If you actually want to benchmark real world code, use tools like Xdebug and XHProf.

Xdebug 非常适合您在开发/暂存环境中工作,而 XHProf 是一款出色的生产工具,在那里运行它是安全的(只要您阅读说明).任何单个页面加载的结果都不会像查看您的代码执行情况那样相关,而服务器也被锤打着做一百万个其他事情并且资源变得稀缺.这就提出了另一个问题:你在 CPU 上遇到瓶颈了吗?内存?输入输出?

Xdebug is great for when you're working in dev/staging, and XHProf is a great tool for production and it's safe to run it there (as long as you read the instructions). The results of any one single page load aren't going to be as relevant as seeing how your code performs while the server is getting hammered to do a million other things as well and resources become scarce. This raises another question: are you bottlenecking on CPU? RAM? I/O?

您还需要不仅仅关注您在脚本中运行的代码,还需要关注脚本/页面的服务方式.你使用的是什么网络服务器?举个例子,我可以让 nginx + PHP-FPM 认真地执行 mod_php + Apache,而这反过来又因为使用一个好的 CDN 来提供静态内容而被击败.

You also need to look beyond just the code you are running in your scripts to how your scripts/pages are being served. What web server are you using? As an example, I can make nginx + PHP-FPM seriously out perform mod_php + Apache, which in turn gets trounced for serving static content by using a good CDN.

接下来要考虑的是您要优化什么?

The next thing to consider is what you are trying to optimise for?

  • 页面在用户浏览器中呈现的速度第一优先?
  • 正在以最快的速度将每个对服务器的请求抛出可能以最小的 CPU 消耗为目标吗?

前者可以通过对发送到浏览器的所有资源执行 gzip 等操作来帮助,但这样做可能(在某些情况下)使您远离实现后者.

The former can be helped by doing things like gzipping all resources sent to the browser, yet doing so could (in some circumstances) push you further away from the achieving the latter.

希望以上所有内容都可以帮助表明,仔细隔离的实验室"测试不会反映您在生产中会遇到的变量和问题,您必须确定您的高级目标是什么,然后您可以做什么到达那里,然后前往微/过早优化通往地狱的路线.

Hopefully all of the above can help show that carefully isolated 'lab' testing will not reflect the variables and problems that you will encounter in production, and that you must identify what your high level goal is and then what you can do to get there, before heading off down the micro/premature-optimisation route to hell.

这篇关于如何对 PHP 脚本的效率进行基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆