Archive for the ‘php’ tag
PHP Benchmarking
I came across a few sites which have some valuable information on the “cost” of certain functions or practices in PHP. It comes in handy to keep in mind and avoid doing, and also to remember that there are better ways of doing things. If your application is coming down to relying on micro-optimization, your application itself needs to be looked at. But there’s no harm in knowing what methods take more time than others. Or of course, you can use ‘em to impress a prospective employer
- The PHP Benchmark – http://www.phpbench.com/
- lixlpixel PHP Benchmark – http://lixlpixel.org/php-benchmarks/
- Comparing all different functions and uses – http://net-beta.net/ubench/
- PHP Benchmarking Suite – http://phplens.com/benchmark_suite/
There are also some lectures and talks on optimizing:
- Speeding up PHP applications by Derick Rethans
- Simple is Hard by Rasmus Lerdorf (the creator of PHP)
- Performance by Rasmus Lerdorf
- PHP Tips & Tricks by Rasmus Lerdorf
- PHP best practices – The dos and don’ts by Tobias Schlitt & Kore Nordmann
Some good stuff there to look over and to keep in mind, especially obvious things to avoid, such as including the count() inside every for() loop. Actually any of the stuff on http://talks.php.net are a good read, and good to see where PHP has evolved from and where it’s evolving to, and to understand how to leverage the language.
To benchmark your scripts, you can use the PEAR Benchmark package; I have PEAR in my path, so for me it was as simple as including the same code that was in the examples.
Nginx + PHP Query Strings
Nginx (Engine-X) is the sweetest web server I’ve used. It’s running my VPS now, CPU usage has barely budged, even with a decent number of visitors. I ran into a problem, which I was searching for a solution before I cracked at it myself. The solution ended up being incredibly simple, but still may help someone else who’s searchin’ for it.
With nginx, PHP is passed of to FastCGI PHP process to parse and execute. It relies on a location rule to find out what are PHP files. But it would fall apart using URLs like:
|
1 |
index.php/some/query/string/parameters |
Apache doesn’t have any trouble, with it, but nginx, if you looked at the error log, it’ll be about the directory not existing. (Hmm, parsing that in PHP sounds like a good idea for another post… noted).
So if we check out the current rewrite rules:
|
1 2 3 4 5 6 |
location ~ \.php$ {
include /etc/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/public/$fastcgi_script_name;
} |
The fix is pretty simple:
|
1 2 3 4 5 6 |
location ~ \.php(.*)$ {
include /etc/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/public/$fastcgi_script_name;
} |
Adding in the (.*) after the .php allows the PHP files to be picked up, and those additional query string parameters will get passed along into FastCGI.
Simple fix!
Airplane Movie Quotes
I just did a quick thing for the phpVMS admin panel, displaying random quotes from the movie Airplane! in the footer. Just call the function randquote(), and it’ll return the string, so you can do whatever you want with it.
And don’t call me Shirly…
Click to download it (zip file)