Scenario Guide: CPU Hotspot (cpu-hotspot)
- Component:
com.shopsphere.demo.scenario.cpu.CpuHotspotScenario - Dominant JFR Event:
jdk.ExecutionSample - Thread State:
STATE_RUNNABLE
1. The Performance Symptom
Under modest concurrent user requests, the service experiences severe throughput degradation and latency inflation. Worker threads peg available CPU cores at 100% capacity while servicing relatively low transactions per second. Thread dumps confirm threads are continuously runnable, not blocked on I/O or locks.
2. How to Trigger the Scenario Safely
Run the standalone benchmark runner with an 8-thread workload capped at 15 seconds:
cd jfr-performance-demo-app
./scripts/run-demo.sh cpu-hotspot 15 8
Alternatively, if running the interactive Spring Boot server on port 8080:
curl -X POST "http://localhost:8080/api/scenarios/cpu-hotspot/start?mode=FAULTY&durationSeconds=15&concurrency=8"
Safety constraints ensure the benchmark terminates automatically after 15 seconds.
3. How to Capture the Recording
When using ./scripts/run-demo.sh, JFR capture is automated.
If capturing manually against the running Spring Boot server using JDK jcmd:
# 1. Identify JVM PID
jcmd | grep ShopSphereApplication
# 2. Start JFR profile recording
jcmd <PID> JFR.start name=cpu-run settings=profile duration=15s filename=recordings/cpu-hotspot-faulty.jfr
# 3. Check status
jcmd <PID> JFR.check
4. What to Inspect in JDK Mission Control (JMC)
- Launch
jmcand openrecordings/cpu-hotspot-faulty.jfr. - Navigate to Method Profiling → Hot Methods.
- Locate
CpuHotspotScenario.calculateComplexDiscountMatrix:- It accounts for >95% of all recorded execution samples.
- Select the Call Tree tab and expand callers:
- All worker threads converge on
executeStep() → calculateComplexDiscountMatrix().
- All worker threads converge on
- Check Threads:
- All 8 worker threads remain in
RUNNABLEstate throughout the sample duration.
- All 8 worker threads remain in
5. Which MCP Tools to Use
Query the MCP server through your AI assistant using:
recording_summary(recording="cpu-hotspot-faulty.jfr"): Confirms total recording timeframe and sample volume.analyze_cpu(recording="cpu-hotspot-faulty.jfr", topN=5): Returns top methods, execution sample count, sample share percentage, and sample stack trace.
{
"totalSamples": 14820,
"topMethods": [
{
"method": "com.shopsphere.demo.scenario.cpu.CpuHotspotScenario.calculateComplexDiscountMatrix()",
"samples": 14210,
"percentage": 95.88,
"isNative": false,
"sampleStackTrace": [
"com.shopsphere.demo.scenario.cpu.CpuHotspotScenario.calculateComplexDiscountMatrix():84",
"com.shopsphere.demo.scenario.cpu.CpuHotspotScenario.executeStep():48"
]
}
],
"averageJvmUserCpu": 0.88
}
6. What the Evidence Does and Does Not Prove
What It DOES Prove:
- Strong statistical dominance: Over 95% of execution ticks captured thread program counters inside
calculateComplexDiscountMatrix(). - Compute-bound execution: Threads were actively runnable on CPU, ruling out lock contention and downstream I/O waits.
What It DOES NOT Prove:
- Exact nanosecond accounting:
jdk.ExecutionSampleis a statistical sampling mechanism (10ms–20ms interval). It does not guarantee continuous execution between sample ticks. - Root algorithmic flaw: JFR identifies where the CPU cycles were burned; human or AI code analysis is required to determine why the loop is computationally redundant.
7. How to Inspect and Correct the Source Code
Open src/main/java/com/shopsphere/demo/scenario/cpu/CpuHotspotScenario.java:
Faulty Implementation
// O(N) loop computing redundant trigonometric polynomials per transaction
private double calculateComplexDiscountMatrix(String customerId, double subtotal, int itemCount) {
double discount = 0.0;
for (int i = 1; i <= 5000; i++) {
discount += Math.sin(i) * Math.cos(i) * Math.sqrt(subtotal) / 10000.0;
}
return Math.min(discount, subtotal * 0.30);
}
Corrected Implementation
// O(1) closed-form calculation
private double calculateClosedFormDiscount(String customerId, double subtotal, int itemCount) {
double baseRate = 0.30;
return subtotal * baseRate;
}
8. Comparing Before-and-After Measurements
Execute ./scripts/run-demo.sh cpu-hotspot 15 8 to generate the comparison report:
| Metric | Faulty Baseline | Corrected Version | Improvement |
|---|---|---|---|
| Throughput (ops/sec) | 358 ops/s | 468,210 ops/s | +130,538% |
| P50 Latency | 22.3 ms | 0.016 ms | -99.9% |
| P99 Latency | 31.4 ms | 0.042 ms | -99.8% |
| Hotspot Sample Share | 95.8% | < 1.0% | Eliminated |
The corrected recording demonstrates near-zero CPU time spent in discount calculation, allowing the worker pool to reach memory-bus throughput limits.