Performance optimization of any software project is a mix of art, science, stubbornness and inexplicable improvement. Most developers will spend incredible amounts of time tweaking their application to squeeze out performance gains, regardless of the value they provide. As the old saying goes, given enough optimization rope, developers will quickly hang themselves.
Using the following five steps, you can ensure that the time you spend optimizing your application provides maximum value.
1. Benchmarking
Optimizing your application is worthless without a benchmark. You absolutely must have a measurement of performance before you begin so that your changes can be quantified when you are done. I have seen many developers start out with a complaint from a client about a “slow” page and then spend a few hours making changes only to conclude with “it seems a lot faster now”. If you’re optimizing a rails application, something like New Relic’s RPM is a great choice. Another tool I’ve had success with is brynary’s rack-bug.
2. Set a Timebox
Once you’ve recorded benchmarks for the problem areas of your application you need to determine what specifically is wrong and how to fix the problem. Since it’s possible to spend hours upon hours of time under the directive of “performance improvement” you need to set a hard limit on the time you spend optimizing. Two timeboxes should be used, one to locate the specifics of the problem and another to fix them.
3. Similarity of Environments
The vast majority of performance issues will be discovered by real users performing real tasks in a production environment. In a rails application, the behavior of the development environment is far different from that of the production environment, so why would you optimize in anything but the production environment? Of course you can’t optimize on the live production application, but you can setup a similar environment that behaves like the live production environment. Just recently I spent an hour optimizing a slow page and actually gained an 80% improvement, the only problem was, in production the page was slow for a completely different reason. Talk about a tough conversation, having to go back to the client and explain that my previous detailed explanation and assurance that the problem was solved was instead all wrong.
4. Accurate Dataset
How many times have you implemented a feature, ran it through some tests, demo’d to the client, deployed it to staging only to find that when it makes it to production it is impossibly slow to the point of being useless? If your production environment has a huge database with a hundred thousand rows and a few hundred concurrent users, testing on your development machine with 10 rows in the database is a waste of time. Make sure that the dataset on which you are testing your improvements accurately reflects your production dataset.
5. Low-hanging Fruit and Small Victories
Since you will have a fixed amount of time in which to optimize, you need to take small wins and get the low-hanging fruit when possible. Maybe the best solution to your problem is to drastically change the application’s design garnering a big boost to performance. However, if that route takes you beyond your timebox maybe a few database indexes and an optimized query are all you can afford. Always remember that a small performance improvement is better than no improvement and that the small stuff always adds up.
BONUS Stakeholders Care About Value
When a report of a slow feature comes in, make sure that you evaluate the time and effort it will take to implement the improvement. Communicate this with the client, product owner and/or stakeholders so that they can evaluate the value they’ll receive from the time you spend. If the feature in question is used infrequently it might not be worth the time or cost. Conversely, performance optimization should not always be viewed as something done for free. If an application has grown in size beyond what was originally envisioned, it is perfectly reasonable to charge your client for performance enhancements.
Don’t be like most developers, keep these steps in mind and make your next performance optimization session a success.