chapter five

5 Premature optimization vs optimizing hot-path: Decisions that impact code performance

 

This chapter covers

  • When premature optimization is evil; when it is a sane choice
  • Finding hot-path in your code using performance testing and measurements
  • Optimizing the hot-path

There is an old computer science saying that Premature Optimization is the root of all evil. It has a solid background because it’s accurate for a lot of use cases. Without any input data about expected traffic and SLA, it’s hard to reason about your code and its required performance. Optimizing random paths in code in such a situation is like shooting in the dark. You will complicate your code without a sane reason.

However, there is a situation when at the design stage, we know quite much about the expected traffic that our system will need to handle. In such a scenario, we can design performance benchmarks that will reflect the production traffic. Once we are able to simulate the traffic, we can measure paths in our code and find out the hot-path. The hot-path is a part of your code that does most of the work and is executed for almost every user request. We will learn that the Pareto principle can be used to find and estimate where the hot-path is. Once we have detected the hot-path, we can start trying to optimize it.

5.1   Finding when Premature Optimization is evil

5.1.1   Creating accounts processing logic

5.1.2   Optimizing processing based on false assumptions

5.1.3   Benchmark the performance optimization

5.2   Understanding hot-path in your code

5.2.1   The Pareto principle in the context of software systems

5.2.2   Configuring number of concurrent users(threads) for a given SLA

5.3   Creating service with a potential hot-path

5.3.1   Getting word of the day

5.3.2   Validating if the word exists

5.3.3   Exposing word service using the HTTP service

5.4   Detecting the hot-path in your code

5.4.1   Creating API performance tests using Gatling

5.4.2   Measuring code paths using MetricRegistry

5.5   Improving hot-path performance using cache and benchmarking

5.5.1   Creating JMH microbenchmark for the existing solution

5.5.2   Optimizing word-exists using Cache.

5.5.3   Modifying performance test to have more input words

5.6   Summary