Press "Enter" to skip to content

Richy's Random Ramblings

PHP Profilers – A Quick summary

More an aid to memory then anything, here’s the PHP profilers I’ve recently heard about:

XDebug by Derick Rethans
The oldest and most well known profiler. Needs modification of the server’s PHP file to run. Compatible with KCachegrind/WinCacheGrind, MacCallGrind and Webgrind and remote debugging tools. Produces quite large debug files (typically 900Kb to 10Mb compare to XHProf’s 110Kb).
XHProf by Facebook Inc (available via PECL)
Large amount of information provided and allows you to “drill down” by various sections – allows comparison of “diffs” between runs to check performance enhancements. Needs modification of the server’s PHP file to run. Also compatible with the *Grind systems. Used by Facebook and Manga High’s online maths games website, but only currently runs on Linux/FreeBSD. Seems to be preferred by iBuildings (see Profiling with XHProf) which is how I originally heard about it.
PHP Quick Profiler (PQP) by Particle tree
Needs modifications to your PHP code, but does not need the server’s PHP modifying (so it can be used on shared hosting easily). Nice and bright. Used by ParticleTree on their Wufoo product.

Snippet: Predicted Caffeine Intake

Today I’m probably going to be consuming:

  • 2x Mars Mochachino Hazlenut coffees (caffeine value unknown – one already drunk)
  • 1x Mana Health Potion (160milligrams of caffeine)
  • 1x Bawls G33k B33r (64milligrams of caffeine
  • 1x Cherry Lucozade (46milligrams of caffeine)

  • At least 1 cup of tea
  • Total: At least 270milligrams

Why? Well, just to try and keep me awake and warm. According to my other half 10grams of caffeine is lethal: so I’ll have less than a twentieth of a lethal dose (just 0.27grams).

Giggity-giggity watch me turn to goo 😉

Web: Bad eset emails

Just tried to signup for a trial of the eset antivirus from eset.co.uk and received the following useful emails…. After trying a second time, I just went to eset.com and downloaded the 64bit antivirus software without having to register…

Here’s the email:

Subject:ESET NOD32 Antivirus Home Edition – Trial Licence
Layout[‘Header’] Content[‘Generic-TrialIntroduction’]

Content[‘Generic-TrialLicenceInformation’]

Content[‘ENAHE-Instructions’]

Content[‘ENAHE-SystemRequirements’] Info[‘CustomerSupport’]
Info[‘AdditionalInformation’]
Info[‘awards’] Layout[‘Footer’]

Yep, useful!

Linux: Handy Varnish commands

If you are running the Varnish reverse proxy cache system on your website (yep, this is a techy post!), you might find the following command line tools useful. Varnish is a very powerful and useful cache tool which sits in front of your website and helps reduce the load on Apache/PHP – but there’s very little information about how to use it available. Hopefully this is the first of many posts about perfecting a varnish configuration. But first, let’s get an idea of some of the information that can be reported from varnish:

varnishlog
See what Varnish is currently processing.

varnishtop -i RxHeader -I \^Referer
Show the referer (sic) header for requests.

varnishtop -b -i TxURL
Shows requests made to the backend (-b) where the line matches (-i) the transmit URL (TxURL). Basically, shows you what is being passed to the backend and isn’t being cached. It will list all requests going to a backend, grouped by URL and sorted by a decaying average of frequency. Basically the number on the left should be single-digit and preferably all 1s or less (a higher number means the backend request is taking place frequently). [Technically, you don’t even need the “-b” as the TxURL is only set when making requests to the backend anyway]

varnishhist
Shows a histogram chart of the last 1,000 requests (by default) to the Varnish proxy showing “|” as a “hit” on the cache and “#” as a miss. The more “|” to the left of the chart, the better. The scale on the bottom is in seconds with 1e0 being “1” second and 1e-6 being 0.000001seconds (1e-1 being 0.1seconds). The vertical scale is shown in the top left hand corner.

varnishstat
Shows various information about varnish – I’m sure I’ll figure out what they mean in time…

Use varnishreload
From http://kristian.blog.linpro.no/2009/02/18/easy-reloading-of-varnish-vcl/ , this is a very handy script you can use to reload varnish’s configuration without having to restart the proxy server:
#!/bin/bash
# Reload a varnish config
# Author: Kristian Lyngstol
FILE="/etc/varnish/default.vcl"
# Hostname and management port
# (defined in /etc/default/varnish or on startup)
HOSTPORT="localhost:6082"
NOW=`date +%s`
error()
{
echo 1>&2 "Failed to reload $FILE."
exit 1
}
varnishadm -T $HOSTPORT vcl.load reload$NOW $FILE || error
varnishadm -T $HOSTPORT vcl.use reload$NOW || error
echo Current configs:
varnishadm -T $HOSTPORT vcl.list

Show what caused recent 503 errors
varnishlog -d -c -o TxStatus 503

Clear stale cache by host name
purge req.http.host ~ example.com

See what status codes are being commonly hit by your users (ideally lots of 200 and few 5xxx)
varnishtop -i TxStatus
See also: http://www.slideshare.net/schoefmax/caching-with-varnish-1642989,
http://blogs.osuosl.org/gchaix/2009/10/12/pressflow-varnish-and-caching/ and http://letsgetdugg.com/2009/12/04/random-varnish-tips-n-tricks/

PHP: self and clone

I recently had to make a PHP class (let’s call it “class_A”) which returned an array of itself (to return multiple search results). All well and good and I used “$object=new self();” to do so like this:


class class_A {
public $setting;

public function doSomething() {
$toReturn=Array();
for ($i=1;$i<100;$i++) { $object=new self(); $object->setting=$i;
Array_push($toReturn,$object);
return $object;
}
}

Calling $class_A->doSomething() then returns 100 “class_A” objects.

However, I then needed to extend the PHP class (let’s call this class_B – declared with “class class_B extends class_A”) to take into account additional settings and I fully expected $object to be a new copy of class_B (as class B is technically the class being run).

Nope, it doesn’t work like that. I ended up with $object being class_A . I had to modify the code to change:
$object=new self();
to:
$object=clone $this;
which then allowed $class_B->doSomething(); to return 100 class_B objects.

Annoying, but something to remember!