MySQLslap – A quickstart guide

Let me set the scene of how I stumbled upon the need for MySQLslap.

The database server was getting hit hard and slowing down the application, we narrowed the issue down to one potentially bad query.

We then took a copy of some production data, put it on our local machines and started hacking to see if the query could be improved.

Even though using EXPLAIN we were able to see it was a bad query, and running our new query through EXPLAIN made it look like a good query, we couldn’t actually benchmark how much better it was. You see our issue was that when ran once the query finished in a reasonable amount of time, the issue only seemed to appear under production load.

There are plenty of tools available for testing your code under load, but I didn’t want anything above these queries dirtying the results, for example I didn’t want ActiveRecord to try and do anything smart with caching, I wanted only to see what SQL statement worked best.

Turns out MySQL comes with a tool to help you do just that, MySQLslap.

MySQLslap

As the name suggests MySQLslap slaps your MySQL tables with a load of queries at a rate and concurrency that you set.

After the queries have ran it will produce a small table breaking down the times for you, which allows you to easily compare and contrast.

Here is an example of some output from MySQLslap;


	Average number of seconds to run all queries: 0.014 seconds
	Minimum number of seconds to run all queries: 0.012 seconds
	Maximum number of seconds to run all queries: 0.017 seconds
	Number of clients running queries: 50
	Average number of queries per client: 2 

This was generated from one command, that took a very small amount of time to set up.

MySQLslap – Setting it up

In order to get the above output I just needed to enter one line into my terminal;

mysqlslap --concurrency=50 --iterations=5 --query=/Users/me/Desktop/test1.sql --create-schema=my_database  -umy_username -pmy_password

I will go through each flag in turn;

  • mysqlslap – This is the program we are running, if you don’t have it installed make sure you are running at least MySQL 5.1.4
  • –concurrency – This is how many connections you want to emulate  very roughly this adds more load onto MySQL, imagine 50 people hitting the same page at the same time.
  • –iterations – This is how many times you want to run the test, obviously the more you do it the more accurate your average will be.
  • –query – You can type the query or queries into here, but I preferred to have them saved in a .sql file, so in my case I just reference where my file is located (note: you cannot reference the home directory by typing ~/ mysql doesn’t understand it)
  • –create-schema – This is the most confusing flag in my opinion, this means which database you want the test to use, because it is called create I wrongly assumed at the start it would kill my current database, it does not (but any queries you run will affect it)
  • -u – The username you use to connect to your database (optional depending on your setup)
  • -p – The password you use to connect to your database (optional depending on your setup)

Hopefully that will help get you on your way.

MySQLslap resources

Here are a collection of useful resources (I will add to this over time);

Share this on

You should never publish a blog post on the day you write it

This is something I am continually guilty of, writing something, giving it a quick read over and then immediately posting it up. This isn’t a good idea and it leads to lower quality content being produced and potentially ill conceived ideas being aired.

Obviously in some situations there is an advantage to being first to market, and this applies to blog posts as well. If you are writing about some form of breaking news or writing a ‘live blog’ about something then it would make good sense to ignore this post, but for most other types of writing I think you would benefit from taking a break between writing and publishing.

There are several reasons for this;

  • During the time you finish writing and the time you publish you might come up with a new idea or angle to explore in the post.
  • Grammatical and spelling errors are easier to pick up with fresh eyes.
  • Social media along with sites like Reddit and Hacker News mean your post could be being seen by thousands of people within minutes, so you don’t have the luxury of making small incremental changes after you post.
  • You might think of better wording for something or a more eloquent way to write it.

I have read some people like to wait a week or more before revisiting a body work, I have done this before but I haven’t found that any more effective than just leaving it for a day or two. Your mileage may vary.

It also helps to steady your output, I for one have this massive creative surge for writing at around 11PM at night, depending on the length of the post I could have it written and proof-read by midnight, even if it was good to go and I didn’t think anything could be added it to this would not a good time to post, holding off means I have content that I can share at more opportune times.

Share this on

Unboxing the Firefox OS Developer Preview Phone

Here are some pictures of my unboxing of the Firefox OS Developer Preview Phone. I opted for the Peak model.

I had thought about writing up a “first impressions” post, but unfortunately my sim card won’t fit into it and I wouldn’t feel comfortable reviewing it purely based on its offerings over WiFi.

2013-05-14 13.14.09

The packaging it came in was very nice, simple but nice. I was kind of reminded of the packaging that the Amazon Kindle came in, that sort of recycled cardboard look.

2013-05-14 13.14.50

Accessories (not pictured) that came with the phone include;

  • Ear Buds
  • A USB charger
  • A USB power block (unfortunately this is 2-prong and is of no use to me)
  • A FireFox OS Developer Preview Sticker (I am partial to a good sticker now!)

2013-05-14 13.17.24Here is the phone turned on, and as I say this is pretty much as far as I will take this blog post. I will say that the screen isn’t as responsive as I would like, but I am happy with the weight and build of the phone and it picked up WiFi straight away and grabbed the latest update, which is all good.

 

Share this on

Review: jQuery HotShot

jQuery HotshotsI have followed and been a fan of Dan Wellman’s writing for some time and when I noticed he was asking on Twitter for people to review a new book he was writing I jumped at the chance.

I have been using jQuery for several years now on a mixture of commercial and personal websites and on various web applications and whilst I don’t always think it is the right solution for a problem, when a library is the right solution jQuery is one of the best. In my current role we heavily use jQuery so I believe I am qualified to accurately review this book.

For this review I am reading the Kindle version of jQuery HotShot which you can buy now from Packt and from Amazon, the book is also available as a paperback for all you cave-people.

jQuery HotShot

So, is jQuery HotShot a good read? Or does Dan fall into the trap that so many jQuery writers do and leave you with pages of poorly written jQuery that doesn’t conform to any best practice?

I am happy to report that true to form Dan has produced an excellent book that manages to explain some of the core features of the library in interesting and memorable ways. He has greatly increased my understanding of some of the features that make jQuery an excellent library.

With hardly any preamble we are thrown straight into a fun example of using jQuery to create a sliding puzzle game. The example manages to assume no prior knowledge but at the same time doesn’t bore someone with prior knowledge by walking at a snails pace, as the book continues the examples get more and more involved.

One of the reasons that jQuery HotShot is able to get straight into good examples is that the examples themselves are littered with little best practice tidbits, for example;

Joining an array of substrings to form a single string is much faster than building a string using the + operator on substrings, and as we’re working repetitively inside a loop, we should optimize the code within the loop as much as possible.

Without needing a chapter or section dedicated to optimisations we have been able to find out about the need to take care about what we do inside loops and also learn a quick performance tips when dealing with strings. This certainly doesn’t have any direct bearing on the task at hand, but helps point the reader towards the best way of doing things.

My favourite example used in the book is probably the Bounty Hunter mobile site that hooks into StackOverflow looking for questions to answer, but the book covers a wide range of examples including building browser add-ons using jQuery and how to build your own jQuery (which introduces things like grunt.js, node.js, git etc.).

I am kind of cheating, because I was already aware of Dan’s writing and work so I knew that he knows his stuff, but you honestly do get the impression that a real expert in their field is writing this book, the way the information is presented shows that he has a deep and broad understanding of the subject material.

Something that I loved but that some might not was how involved the examples were, by this I mean that you could tell the feature or features an example was trying to explain and there are far more concise examples that could have been used, but they would either not have been as fun or wouldn’t have had a grounding in the real world.

One small niggle I have with the book is that the terminology for the sections within a chapter don’t suit my personality, subtitles like “Prepare for Lift Off” and “Engage Thrusters” belong more in the fluffy self-help books I love to read as opposed to a technical book, but I am fully aware the majority of people probably love that kind of stuff! Another tiny irk is that one of the examples talks about infinite scrolling, and I personally think that infinite scrolling is the devil!

My Conclusions

All in all I thoroughly enjoyed this book, and as someone who has been using jQuery in anger for a very long time I did come away haven’t learnt a truck load and I imagine you will too. One day I might write a blog post about the things I have learned from this book, in the mean time you should just go ahead and buy it!

jQuery HotShot Resources

Here are some resources you might find useful related to this review;

Share this on

Location, Location, Location

Where your business is based as a massive impact on staff, at least in my opinion.

I have recently started working in the center of Belfast, which is the first time I have ever worked so close to plenty of places to shop and eat and I have to say I didn’t think location would matter as much to be, but it totally does. And it subconsciously probably does to you as well.

Belfast is a small enough city that you can get to most places in the city centre in about five to ten minutes, which essentially means there are very few places you couldn’t get to on your lunch break. This is a strain I didn’t even know I had lifted of my shoulders.

Things like collecting your post from a collection point, grabbing a quick birthday present for someone or picking up a bit of shopping so that you don’t have to on the way home are all massive plus points and it means that instead of sitting all day with a problem floating around the back of your mind you can take a quick note to pop out before work, at lunchtime or after work to do what you need to do, which leaves more capacity inside your noggin’ for problem solving.

The other massively useful thing I have found about working in a city is that you can easily meet up with friends, peers and collaborators at a moments notice, doing so really helps to properly relax you during lunch and helps keep your social wheels lubricated (I can’t believe I just said that, literally shaking my head at myself).

Finally, I really like the choices to do stuff for lunch, Belfast isn’t the greatest city in the world for places to eat, but it sure beats being limited to one or two choices. I would say most people working in a large town or city probably chose from 3 places regularly, but even knowing there are options is just a really nice feeling.

From a business point of view I can’t see why you wouldn’t want to be in town, your clients will know how to travel into town, your workforce are happier and yes I am sure the prices are a bit higher but I would say the returns you get from happier staff and the value of opportune meetings at lunchtime would surely make up for this.

Now that I have been exposed to working somewhere in the city I don’t think I could work in places like industrial or business estates again, and I don’t think I am the only one – I would imagine this is why more and more startups are biting the bullet and paying up for good property in town instead of saving some pennies and moving even 10 minutes out of the way.

If you aren’t in town, you should be.

Share this on