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

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

问题描述

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

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.

我知道我可以使用microtime,但这真的给了我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.

有更好的方法吗?

推荐答案

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

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

Xdebug非常适合在开发/暂存中工作,并且XHProf是用于生产的出色工具,并且可以在此处安全运行(只要您阅读了说明).任何单个页面加载的结果都不会像查看服务器在执行一百万其他事务以及资源变得稀缺时要如何执行代码一样重要.这就提出了另一个问题:您是否在CPU上遇到瓶颈?内存? I/O?

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?

除了在脚本中运行的代码外,您还需要查看如何为脚本/页面提供服务.您正在使用什么Web服务器?举例来说,我可以使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消耗达到目标?
  • Is the speed with which the page renders in the users browser the number one priority?
  • Is getting each request to the server thrown back out as quickly as possible with smallest CPU consumption the goal?

前者可以通过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天全站免登陆