Sub topic 16
Change one thing, then measure again
This chapter assumes you can record a profile with Java Flight Recorder (JFR) and read a hot-method list. It also assumes you know what a latency percentile is. The goal here is the loop that turns a profile into a verified improvement.
Why one change at a time
A profile tells you where time goes. It does not tell you that a fix will help. If you change three things and the service gets faster, you don't know which change worked. One of them may even have made things worse. So you change one thing, measure again, and keep the change only if the numbers improve.
Step 1: Record a baseline
Fix the conditions before you touch the code. Use the same input, the same JVM flags, the same machine and the same load. Run long enough to get past JIT warmup, and repeat the run so you can see the normal spread. Record the metric from your goal, for example p99 latency or throughput.
Step 2: Change one thing
Take the top item in the profile. In the Slow program from earlier, the hot spot is string concatenation in a loop. Each s += i copies the whole string, so the work grows quadratically. The single change is to use a StringBuilder:
public class Fast {
public static void main(String[] args) {
long start = System.nanoTime();
for (int round = 0; round < 200; round++) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5_000; i++) {
sb.append(i);
}
if (sb.length() == 0) {
System.out.println("unreachable");
}
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ms");
}
}Step 3: Re-measure the same way
Run both versions with identical settings and compare the numbers. Do not compare one run of each. Run each several times and compare the ranges.
for i in 1 2 3 4 5; do java Slow; done
for i in 1 2 3 4 5; do java Fast; doneIf the ranges overlap, the change is within noise and you have not shown an improvement. Then record a new JFR profile of the fast version. The old hot spot should shrink or vanish, and a different method now leads the list. That method is your next target.
Common errors
- Changing several things at once. You can't attribute the result.
- Measuring under different conditions. A different input size, a cold cache or another process on the machine can outweigh your fix.
- Trusting one run. Single runs are dominated by warmup and noise.
- Optimizing what is not the bottleneck. Speeding up a method that takes 2% of the time gives at most a 2% gain.
- Improving the wrong metric. A change can raise throughput and worsen p99 latency. Check the metric from your goal.
When to stop
Stop when you meet the goal you set at the start. Further tuning costs readability and maintenance time, so it should buy something you need.
Exercise
You replace string concatenation with a StringBuilder and also raise the heap size in the same run. Latency improves by 30%. Why is this a weak result, and what should you do?
Show answer
Two changes were made together, so you can't tell which one caused the improvement, or whether one hurt. Revert to the baseline, apply each change separately, and measure each several times under identical conditions.