Fork me on Github
Fork me on Github

Joe Dog Software

Proudly serving the Internets since 1999

up arrow Use Break Statements, People

A few years ago, Your JoeDog was re-working someone else’s code. Fun times!  It was written in java by a professional web shop out of Chicago. He’s not going to name the shop but it rhymes with “Oxy Dom.”

In one particular segment, there was a big mother of a switch statement with perhaps a couple dozen case conditions. That’s quite a few case conditions! But here’s the thing about this code which was executed by a highly paid software engineer. It contained no break statements.

Why does that matter? Let’s find out after the jump!

So here’s an example in PHP that we’ll use to illustrate the importance of break statements. It contains a fraction of the conditions Your JoeDog encountered back in the day but that doesn’t matter. We can still use it for to experiment (and it’s not even 4/20):

  /**
   * test.php
   */
  $req = array();
  $req = $_REQUEST;
  foreach ($req as $key => $val) {
    $req[$key] = stripslashes(trim($val));
  }
  switch ($req['action']) {
    case "request":
      print "This is a request statementn";
    case "put":
      print "This is a put statementn";
    case "get":
      print "This is a get statementn";
    case "post":
      print "This is a post statementn";
    case "delete":
      print "This is a delete statementn";
    case "modify":
      print "This is a modify statementn";
    default:
      print "I don't know what to do!n";
  }

Now let’s build a siege urls.txt file which hits every condition in the file:

  
HOST=server.joedog.org
FILE=test.php
PROTOCOL=http
$(PROTOCOL)://$(HOST)/$(FILE)?action=undef
$(PROTOCOL)://$(HOST)/$(FILE)?action=request
$(PROTOCOL)://$(HOST)/$(FILE)?action=put
$(PROTOCOL)://$(HOST)/$(FILE)?action=get
$(PROTOCOL)://$(HOST)/$(FILE)?action=post
$(PROTOCOL)://$(HOST)/$(FILE)?action=delete
$(PROTOCOL)://$(HOST)/$(FILE)?action=modify

We ran 15 users 1000 times. Let’s check the result:

Transactions: 15000 hits
Availability: 100.00 %
Elapsed time: 5.75 secs
Data transferred: 1.49 MB
Response time: 0.01 secs
Transaction rate: 2607.34 trans/sec
Throughput: 0.26 MB/sec
Concurrency: 14.26
Successful transactions: 15000
Failed transactions: 0
Longest transaction: 0.12
Shortest transaction: 0.00

Now we’ll modify our code with break statements. You place each one at the end of each condition. Here’s our new switch statement:

  switch ($req['action']) {
    case "request":
      print "This is a request statementn";
      break;
    case "put":
      print "This is a put statementn";
      break;
    case "get":
      print "This is a get statementn";
      break;
    case "post":
      print "This is a post statementn";
      break;
    case "delete":
      print "This is a delete statementn";
      break;
    case "modify":
      print "This is a modify statementn";
      break;
    default:
      print "I don't know what to do!n";
      break;
  }

Let’s siege it again. Same thing, 15 users 1000 times:

Transactions: 1500 hits
Availability: 100.00 %
Elapsed time: 0.61 secs
Data transferred: 0.04 MB
Response time: 0.01 secs
Transaction rate: 2450.98 trans/sec
Throughput: 0.06 MB/sec
Concurrency: 13.91
Successful transactions: 1500
Failed transactions: 0
Longest transaction: 0.11
Shortest transaction: 0.00

As you can see, we went from 5.75 seconds down to 0.61 seconds. This particular code is 9-1/2 times faster with break statements.

So what did we learn from all this?

  1. Siege is a great tool for testing your hunches
  2. Professional coding shops aren’t all that professional
  3. Beer takes the edge off of other people’s code….