Sub topic 1
Start with a measurable goal
Profiling without a goal produces a long list of "things that could be faster" and no way to know when you are done. This chapter shows how to turn "the app feels slow" into numbers you can measure, compare and act on.
Prerequisites
This chapter assumes you can:
- Write and run basic Java programs from the command line.
- Read a stack trace.
- Explain what a thread and the heap are.
No profiling tools are needed yet. Later chapters cover them.
Why "faster" is not a goal
"Make it faster" cannot be checked. A goal needs a metric, a target value, and the conditions under which you measure. For example: "The /search endpoint has a p99 latency under 200 ms at 500 requests per second on the production-sized test environment."
The metrics that matter
Most Java performance goals use one or more of these.
| Metric | What it measures | Typical goal |
|---|---|---|
| Latency percentile | Time for a single request, at the 50th, 95th or 99th percentile | p99 under 200 ms |
| Throughput | Work completed per unit time | 2,000 orders per second |
| Memory footprint | Heap in use after garbage collection, and total process size | Live heap under 1.5 GB |
| GC pause time | How long the application is stopped by collection | No pause over 50 ms |
| CPU usage | Share of cores consumed at a given load | Under 60% at peak |
Why percentiles, not averages
An average hides slow requests. If 99 requests take 10 ms and one takes 1,010 ms, the average is 20 ms, but one user in a hundred waited a full second. The p99 value is the time that 99% of requests beat, so it exposes this tail.
Throughput and latency pull against each other
Raising throughput often raises latency. Batching work or adding threads can increase total work done while making each request wait longer. State both metrics in your goal, so that improving one does not silently break the other.
Capture a baseline first
Before changing anything, record the current numbers under a repeatable load. The baseline gives you a comparison point. Without it, you cannot tell a real improvement from noise.
A minimal timing harness is enough to start:
public class Baseline {
public static void main(String[] args) {
long start = System.nanoTime();
long sum = 0;
for (int i = 0; i < 100_000_000; i++) {
sum += i % 7;
}
long elapsedMs = (System.nanoTime() - start) / 1_000_000;
System.out.println("sum=" + sum + ", elapsed=" + elapsedMs + " ms");
}
}Run it several times. If results vary by 10% between runs, a 5% improvement is not detectable. Note the variance along with the mean.
Write the goal down
A usable goal has four parts:
- The metric (p99 latency).
- The target (under 200 ms).
- The load and environment (500 requests per second, production-sized hardware).
- The current baseline (now 450 ms).
When the target is met, stop. Further tuning costs time and adds complexity.
Exercise
A service reports an average response time of 40 ms, but users complain about occasional multi-second delays. Which metric should you add to the goal, and why?
Show answer
Add a high percentile such as p99 (or p99.9). The average is dominated by the many fast requests and hides the slow tail that users are noticing. A percentile shows how slow the worst few percent of requests are.
Exercise
Rewrite "make the checkout service use less memory" as a measurable goal. Name the metric, target, conditions and baseline you would need.
Show answer
For example: "Live heap after a full GC stays under 1 GB (target) while the service handles 200 checkouts per second (conditions), down from the current 1.6 GB (baseline)." Any version with all four parts is correct.