[philiptellis] /bb|[^b]{2}/
Never stop Grokking

Saturday, January 02, 2010

Run your own bandwidth test

Happy 2010 to all my readers. A new post to start off this year. Back in November, I wrote about bandwidth testing through javascript. I've just created a github project for it, and am making the code available for anyone to download and use on their own servers. Use this if you want to know what your site's users' bandwidth is, and perhaps customise the experience for different bandwidths.

Get the ZIP file
(updated to 1.1)

The zip file contains:
  • bw-test.js: the javascript file you need
  • image-*.png: bandwidth testing images
  • README: brief instructions
  • tests/*.html: simple tests that show you how to use the code
Unzip the file into a directory in your web root. You really only need the javascript and the images, so you can get rid of the rest. The javascript does not need to be in the same directory as the images, just make sure the base_url variable points to the URL where the images are. This means that you can offload the javascript onto a CDN and keep the images on your own server. You shouldn't push the images to a CDN, because that will measure your user's effective bandwidth when accessing the CDN and not your server, which is presumably what you'd like to know. You'd probably also want to minify the javascript before using it. I'll provide a minified version in a later release.

The test runs automatically once the code is included, so to avoid interfering with the download of your page's components, make sure it's the last thing on your page.

If you want to get a little more adventurous, you could set the auto_run variable to false, and then start the test whenever you're ready to run it by calling PERFORMANCE.BWTest.run().

Once the test completes, it will fire the PERFORMANCE.BWTest.oncomplete event. You can attach your own function to this event to do what you want with the results. You can also beacon back the results to your server by setting the beacon_url variable. This URL will be called with the following URL parameters:
  • bw: The median bandwidth in bytes/second
  • latency: The median HTTP latency in milliseconds
  • bwg: The geometric mean of bandwidth measurements in bytes/second
  • latencyg: The geometric mean of HTTP latency measurements in milliseconds
Your script that handles this URL may store these details in a database keyed on the user's IP or at least some part of it, and perhaps set a cookie to avoid running the test if you already know their bandwidth.

The code is distributed under the BSD license, so use it as you like.

Note that all variables mentioned above are in the PERFORMANCE.BWTest namespace.


Post a Comment