use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class ThreadStateSamplerTest method testCpuTimeMetrics.
@Test
public void testCpuTimeMetrics() {
MockServiceManager serviceManager = new MockServiceManager();
ServiceFactory.setServiceManager(serviceManager);
serviceManager.setStatsService(new StatsServiceImpl());
StatsService statsService = ServiceFactory.getStatsService();
ThreadStateSampler threadStateSampler = new ThreadStateSampler(ManagementFactory.getThreadMXBean(), ThreadNameNormalizerTest.getThreadNameNormalizer());
CpuCalculationThread cpuCalculation = new CpuCalculationThread(threadStateSampler);
cpuCalculation.start();
try {
cpuCalculation.join();
} catch (InterruptedException e) {
}
StatsEngine engine = statsService.getStatsEngineForHarvest(null);
ResponseTimeStats systemTimeStats = engine.getResponseTimeStats("Threads/Time/CPU/CALCULATE_CPU/SystemTime");
ResponseTimeStats userTimeStats = engine.getResponseTimeStats("Threads/Time/CPU/CALCULATE_CPU/UserTime");
ResponseTimeStats totalTimeStats = engine.getResponseTimeStats("Threads/TotalTime/CALCULATE_CPU/CpuTime");
// these are approximate times
float systemTimeSeconds = systemTimeStats.getTotal();
float userTimeSeconds = userTimeStats.getTotal();
float totalTimeSeconds = totalTimeStats.getTotal();
float cpuCalculationTimeSeconds = (float) ((double) cpuCalculation.getCpuTime() / 1000000000) * 10;
assertTrue("CpuTime: " + cpuCalculationTimeSeconds + ", TotalTime: " + totalTimeSeconds, cpuCalculationTimeSeconds >= totalTimeSeconds);
assertTrue("UserTime: " + userTimeSeconds + ", SystemTime: " + systemTimeSeconds, userTimeSeconds > systemTimeSeconds);
// This test is prone to flickering due to high load scenarios and rounding errors, hence the modifier added to totalTimeSeconds
assertTrue("TotalTime: " + totalTimeSeconds + ", SystemTime: " + systemTimeSeconds + ", UserTime: " + userTimeSeconds, // account for rounding error
totalTimeSeconds + 4.0 >= systemTimeSeconds + userTimeSeconds);
// Since we can't guarantee the exact total time, it should be
// between 1 and 5 (since we had a busywork loop for ~5000ms)
assertTrue(totalTimeSeconds > 1.0f);
assertTrue(totalTimeSeconds < 6.0f);
}
Aggregations