Andrew
Andrew

Reputation: 238937

How to go about speed testing my PHP script?

I have a script that is running slowly and I was wondering how I could time it and see which fixes improve the speed. Is there some sort of a PHP timer?

Update: Thought I should clarify... The script that is running slowly is my mailing list script that sends an email to everyone on the list. So I am looping through an array of subscribers and sending them an email one by one. It's so slow that I thought it would be interesting to see how long it took to iterate though the array. So I'm looking for a quick and easy way to test that.

Upvotes: 1

Views: 1118

Answers (4)

Andrew
Andrew

Reputation: 238937

I ended up using a timer class I found similar to this one: http://forum.codecall.net/php-tutorials/4254-php-timer-class.html

I just started a timer at the beginning of my loop and logged the duration at the end of each iteration. Found out, on average, it was taking between 3 and 4 seconds per email! Five minutes per email blast is a little too much. Time to refactor...

Upvotes: 0

bertbalcaen
bertbalcaen

Reputation: 156

How about using a queue? Seems like a good idea to me, because as the list grows one day you'll hit the limits of the mail server. There's a popular implementation in PEAR of this queue thing, but basically all you need is a cron job and a database table.

I'm building something similar in ZF so I'm interested to see what happens!

Upvotes: 0

mhd
mhd

Reputation: 94

You can measure the loading time of your page and adjust where needed.

I like websites like http://www.phpbench.com/ which tell you more about the basic needs in php.

Offcourse writing php using Zend Framework doesn't really say anything about your code actually. Do you follow the zend coding standards ?

http://www.php.net/getrusage might be usefull for your timing issue.

Good luck !

edit : extra link for the getrusage function ( reffered to on the php.net page as well ).

edit 2 : For your mailing list you could check phpbench.com for example with the count before the loop which saves some time.

How are you sending your mails ? That could be a bottleneck.

Good luck !

Upvotes: 1

AlexanderJohannesen
AlexanderJohannesen

Reputation: 2028

The best thing I know about, and use all the time, is to use a profiler. My profiler of choice is Xdebug which has a built-in profiler, and getting it installed in your PHP setup is fairly easy. Once up and running, it can give you all sorts of debugging and profiling information, and the output format of Xdebug can be read by the amazing kCacheGrind tool (visualizing and reporting on the stack and graphing your application flow) if you're a happy Linux camper. What would I do without these tools!?

Also, there's a few pitfalls in terms of speed when it comes to the Zend Framework, mostly associated with not using the Zend_Cache class enough, or convoluted class hiearchies where you possibly should use the Zend_Registry class.

Happy profiling.

Upvotes: 1

Related Questions