Script to test / benchmark SQL queries

I’m not particularly knowledgable on the subject of optimising SQL queries, so the easiest way to optimise a query for me is to write a few variations then test them against one another. To this end I’ve developed a PHP class to do the testing and benchmarking. I think that even if I was highly knowledgable about optimising queries, then I would still want to test my queries to ensure that my theory held true in practice.

For a useful benchmark you need to execute the queries using a range of data that simulates the real data the queries would be executed with. They also need to be executed in a random order and multiple times, to ensure results can be averaged and reasonably reliable. That’s what this class does, along with providing a summary of the results in CSV format.

It should be noted that this class does not set up or modify any tables for testing with – it just allows you to supply a range of data to be included within the queries themselves, such as testing with a range of different values in a WHERE clause.

Continue reading

Posted on by xoogu, last updated

Issues compiling PHP dependencies

I decided to update PHP, and had a few problems compiling its dependencies. So I thought I’d share the problems I had and the solutions here for future reference, and maybe they might help someone else as well.

Continue reading

Posted on by xoogu, last updated

Does HTTP2 really simplify things?

I recently watched a webinar from NGINX on ‘What’s new in HTTP/2?’. In the webinar they go over how HTTP/2 differs from version 1, and what benefits it has. One of the benefits is that it allows you to use SSL with no performance hit compared to plain HTTP/1.1. The other benefit they go into is that it simplifies your work process. However, I’m not sure this simplification benefit has much truth in it.

Continue reading

Posted on by xoogu, last updated

Accessing a local site on a VM with NAT (shared IP) from another computer

I develop my websites using Linux in a VMWare Virtual Machine, on a Windows host PC. I have the VM configured to use NAT for the Network connection, which means that the VM does not have its own specific IP address on the network, but rather shares the host machine’s IP address.

VMWare Virtual Machine Network adapter settings set to NAT

This is no problem for testing sites locally from the host machine (on a linux VM use ifconfig to get the IP address, then add this with your test domains to the host machine’s hosts file). However, it does cause issues if you want to test your sites from other machines on the network, as they have no way of connecting to your VM. In my case I had a Windows tablet that I wanted to test my site on.

Continue reading

Posted on by xoogu, last updated

Switching gallery / attachment image srcs to a static domain / CDN in WordPress

For the majority of sites that I manage, I use a www. subdomain as the main URL, which dynamic pages are served from. For static assets, such as images, javascript, CSS, etc. I use a static subdomain. This makes it easy to send long expires headers for everything on the static subdomain, and keep cookies restricted to the www subdomain only, resulting in a faster website.

Normally I write my posts in HTML, meaning I explicitly set the src of any images included in the post to point to the static subdomain. However, I realised today that on one of my sites I’m using the [gallery] shortcode. With this WordPress automatically creates an image gallery based on the images added as attachments to the post, and of course, will just use the standard www. subdomain for the image srcs.

Thankfully, you can filter the src that WordPress uses for attachment images very easily. In my theme’s functions.php, I added the following:

add_filter('wp_get_attachment_image_src', 'staticize_attachment_src', null, 4);
/*
* @param array|false  $image         Either array with src, width & height, icon src, or false.
* @param int          $attachment_id Image attachment ID.
* @param string|array $size          Registered image size to retrieve the source for or a flat
*                                    array of height and width dimensions. Default 'thumbnail'.
* @param bool         $icon          Whether the image should be treated as an icon. Default false.
*/
function staticize_attachment_src($image, $attachment_id, $size, $icon)
{
	if (is_array($image) && !empty($image[0])) {
		$image[0] = str_replace('http://www.', 'http://static1.', $image[0]);
	}
	return $image;
}

You need to hook into the wp_get_attachment_image_src action. The callback has 4 paramaters – the first is the important one in our case, $image. This is a numerically indexed array, with index 0 being the src of the image. So you can just do a find and replace on the src in the callback to replace the default subdomain with your static subdomain.

Bear in mind that if your site uses / allows HTTPS, then you’ll need to modify the code above appropriately.

Posted on by xoogu, last updated

Moving the WP Super Cache Plugins folder

WP Super Cache is a plugin for WordPress that allows you to speed up your site by making use of server side caching. The plugin itself can be extended through the use of Super Cache plugins. Ideally you should move the Super Cache plugins folder from its default location (wp-content/plugins/wp-super-cache/plugins/) as otherwise whenever you upgrade the super cache plugin it will overwrite its plugins directory.

I suggest moving the folder to wp-content/wpsc-plugins/. You’ll then need to edit the wp-content/wp-cache-config.php file to change the location where WP Super Cache looks for the plugins. Find the line that says:

$wp_cache_plugins_dir = WPCACHEHOME . 'plugins';

and change it to:

$wp_cache_plugins_dir = WP_CONTENT_DIR . '/wpsc-plugins';
Posted on by xoogu, last updated

Differences between caching methods with Geo targeted content

I recently released a version of the Geo Text WordPress plugin with support for the WP Super Cache and W3 Total Cache page caching plugins. As part of this I added a page to describe why caching plugins are problematic and the different methods the plugin uses to enable compatibility with caching plugins: Geo Text WordPress Plugin: W3TC and WPSC compatibility.

After writing that text, I thought some simple diagrams might help illustrate the reason why standard caching is not useful when you have geo targeted content, and the way that page key modification and fragment caching / dynamic caching can solve this issue.

Continue reading

Posted on by xoogu, last updated

Losslessly Optimising existing JPEGs on your website(s) using jpegtran

Google recently announced that you can now use Webmaster Tools to track your site’s usability in mobile browsers. I checked a couple of my sites and the information given was wrong for both, so it seems to be pretty useless at the moment.

However, what they don’t mention is that their PageSpeed Insights has been updated to cover mobile (and desktop) usability. And testing some pages from the same sites I looked at in Webmaster Tools, the PageSpeed tool actually gives accurate information!

It’s well worth running a page from your site through the tool to see how it fares. Of course, you don’t have to follow all the suggestions it makes, but it may well point out an issue that you didn’t notice before. In my case it pointed out that many of my JPEG images could be losslessly optimised.

Continue reading

Posted on by xoogu, last updated

SVG Image fallback for old browsers

In the process of updating an old site to be mobile friendly, I decided to replace the site’s logo with an SVG, so it will look better on high DPI screens. Old browsers, such as IE < 9 and Android 2.3 don’t support SVG. So we need some way of serving a bitmap image (PNG) to them while serving SVG to browsers that support it.

An excellent article on various SVG fallback techniques, and the pluses and minuses of each technique is SVG Fallbacks on CSS-Tricks. In the comments to that article it is suggested using an <object> tag for the SVG with a <img> for the PNG nested inside as the fallback.

No response for or against was given to this suggestion, so I thought I’d try it out. The code was like this:

Continue reading

Posted on by xoogu, last updated