Will Warren

I code things, therefore I am.


Timing PHP code using a simple stopwatch class

Posted on June 6, 2013 | no comments

If you’re writing a performance-focused app, it’s nice to be able to time how long various pieces of code take to execute. Below is the class I use (called StopWatch) and a really simple example of how I use it.

<?php
class StopWatch {
    /**
     * @var $start float The start time of the StopWatch
     */
    private static $start;

    /**
     * Start the timer
     * @return void
     */
    public static function start() {
        self::$start = microtime(true);
    }

    /**
     * Get the elapsed time in seconds
     * @return float The elapsed time since start() was called
     */
    public static function elapsed() {
        return microtime(true) - self::$start; 
    }
}

Here’s a silly example to time how long it takes to sleep for 2 seconds using sleep(2).

// start the timer
StopWatch::start();

// sleep for 2 seconds
sleep(2);

// check how long 2 seconds is...
echo "Elapsed time: " . StopWatch::elapsed() . " seconds";

When I run this on my local WAMP stack, I get Elapsed time: 1.9999310970306 seconds. The results vary, but they’re all as close to 2 seconds as makes no difference.

A better example would be if you were sending a file to a remote location (such as Amazon S3) and you want to find out how long it takes.

// create a new S3 instance
$s3 = new S3('my access key', 'my secret key');
// start the timer
StopWatch::start();

// read & send the file
$f = $s3->inputFile('file_to_upload.zip');
$r = $s3->putObject($f, 'my-bucket-name', 'uploaded_file.zip', S3::ACL_PUBLIC_READ);

if ($r !== false) {
    // check how long it took
    echo "Elapsed time: " . StopWatch::elapsed() . " seconds";
} else {
    echo "S3 Error!";
}

Note: This example uses the PHP S3 class from https://github.com/tpyo/amazon-s3-php-class.

This would produce something more like Elapsed time: 3.7492766736612 seconds (depending on how big file_to_upload.zip is obviously. This little tool has come in useful for me a number of times, especially when trying to identify bottlenecks in systems that perform a large amount of juggling with complex data structures. You could add all kinds of other options to it as well, like defining what units to return (seconds, milliseconds, nanoseconds).

Tags: , ,

Transition from glossy to matte UI elements

Posted on June 5, 2013 | no comments

Something I noticed as a general trend with modern technology (especially in mobile development) is a trend away from shiny, glossy UI elements like icons and buttons to a more flat, conservative style.

Here’s a really interesting discussion I found about the subject on the UX stackexchange siteĀ http://ux.stackexchange.com/questions/35576/what-explains-the-current-shift-from-glossy-uis-to-matte-uis

Looks like the main culprits would be Windows and Android, while Apple seems to be sticking with what they know (for iOS at least).

Tags: , ,

A working day’s worth of mouse movement

Posted on May 23, 2013 | no comments

I recently came across a neat piece of software that maps out your mouse movements and creates artwork out of them. Check out the image below – it’s a graph of my mouse movements on my left monitor over a 9 – 5 working day. Click to see full resolution.

I move my mouse around a LOT

The black circles represent times when the mouse did not move – the huge black circle was when I went for a 2 hour meeting.

The software I used is called IOGraphica (http://iographica.com/) – try it out and see what you get!

Tags: ,

My latest hobby

Posted on May 15, 2013 | no comments

Golfing!

Golfing!

Tags: ,

Game 1

Posted on May 1, 2013 | no comments

Oli is ready for his first NHL playoffs – GO LEAFS GO

Oli is ready for game 1. #GoLeafsGo

The languages and technology running your favorite websites

Posted on April 26, 2013 | no comments

As a web developer, I can’t help but wonder what technology stack any given website is running. Most of the time it’s obvious (e.g. if the URLs end in .php it’s probably written in PHP), and other times not so much. Below is a list of various websites that I frequent and my best guess at the technology at work.

Take a read and comment with any others you’ve found, and I’ll add them to the list!

Website Tech Evidence
arstechnica.com
IT news, reviews, and analysis
PHP, Memcache, WordPress Tags like this: <body class="single single-post postid-35787 single-format-standard grid-view light">. A lot of self-referential links have wp-content in them and they appear to use batcache.
bbc.com
British news network
PHP? Server type is Apache and mysterious comments referencing .inc files. The Apache server could just be a proxy, however.
cnn.com
A news network that almost always checks its facts
Java with nginx as load balancer/proxy Hard to tell on the main site, but their Job Openings page returns a server type of Sun-ONE-Web-Server/6.1
facebook.com
Social network giant
PHP, HipHop, MySQL A lot of their pages have a .php extension. This is a great read also – gives a sense of the scale of their operation.
github.com
Social Coding for all
Ruby on Rails and Erlang Honestly, I could not tell so I googled it, and it says right on their Wiki page that it’s RoR with Erlang. The more you know.
linkedin.com
Career-oriented social network
Java probably Server HTTP response header was set to Apache-Coyote/1.1
nhl.com
National Hockey League website
Java? HTTP response header Server: Jetty(7.0.0.v20091005)
reddit.com
Pictures of cats and atheism
Python on Amazon AWS It’s open source. Also, their Server HTTP header responds as '; DROP TABLE servertypes; -- haha!
stackoverflow.com
Programming Q&A site
C#, ASP.NET MVC, SQL Server 2008 They make it quite obvious
steampowered.com
Game-centric content delivery system & online store
PHP?, Varnish HTTP Response headers Via: 1.1 varnish and X-Varnish: 1673704970. I guessed at PHP for the programming language because their forums and knowledge base are PHP-based.
twitter.com
Micro-blogging social networking
Some combination of Ruby on Rails, Scala, Java, Thrift, MySQL and memcached Lots of reading, but this was the most interesting.

This is not an exhaustive list, but I’d like to expand it. If you figure any more out, leave me a comment and I’ll add it to the list! Also let me know if I made any mistakes (highly probable) or if you find any more info to add to the “Evidence” column.

Stop Apache from serving the “.git” directory

Posted on April 20, 2013 | no comments

It’s not a good idea to have your .git directory or any of its contents web-accessible. In an ideal world, your deploy process will ignore those files so that they aren’t even in your web root to begin with. If you have to have it there for whatever reason, make sure to restrict access to it using something like:

<Directorymatch "^/.*/\.git/">
    Order deny,allow
    Deny from all
</Directorymatch>

Put this within the VirtualHost directive for your site, then reload Apache’s config with:

sudo service apache2 reload

Now you should be presented with a 403 Forbidden message when trying to access http://your-web-app.com/.git. Try it!

Tags: , , ,

Reverting a git commit after pushing to remote

Posted on April 20, 2013 | no comments

Imagine a scenario where you have a git repo with 2 branches; master, the production-ready branch and dev, the branch where all the developmentĀ occurs.

Now imagine that you accidentally made a commit on master, when really it should have been on dev. If you have not yet pushed to a remote repository (like Github), you can undo that commit using git reset like so:

git reset --soft HEAD~1

This will bring your repository back to the state it was in right before you did your git commit. Now you can switch to your dev branch and re-commit the changes in the right place.

The --soft option tells git to leave your index (or “staging area”) and your working tree alone. If you were to run this same command with --hard it would trash all your local changes. This is fine if you want to throw all your work away, but if the work is good, just the commit was bad, then use --soft.

The HEAD~1 just means “the latest commit’s parent”. It could also be written as HEAD~ or HEAD^.

This is all well and good, but what if you had git pushed right after doing the erroneous commit? If you just try the steps outlined above, and then try and push to your remote repo, you will get an error because the tip of your local repo is behind that of the remote and it will reject your push.

This is where you need to use git revert.

git revert HEAD

This command essentially says, “I want to create a new commit that undoes the commit pointed to by HEAD“. Once the command has been executed, it creates a new commit which you can push back to your remote repo which will effectively create a patch which undoes all the changes in the last commit.

Simple! If you want to really dig into the documentation, I suggest going here: http://git-scm.com/docs/git-reset.html and here: http://git-scm.com/docs/git-revert.html

Tags: ,