Search in sources :

Example 96 with StatsEngine

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);
}
Also used : ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) StatsService(com.newrelic.agent.stats.StatsService) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test)

Aggregations

StatsEngine (com.newrelic.agent.stats.StatsEngine)96 Test (org.junit.Test)78 TransactionDataList (com.newrelic.agent.TransactionDataList)36 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)31 TransactionData (com.newrelic.agent.TransactionData)29 HashMap (java.util.HashMap)25 JmxMetric (com.newrelic.agent.jmx.metrics.JmxMetric)20 StatsWork (com.newrelic.agent.stats.StatsWork)8 MockRPMService (com.newrelic.agent.MockRPMService)7 ArrayList (java.util.ArrayList)6 AgentConfigFactoryTest (com.newrelic.agent.config.AgentConfigFactoryTest)5 Environment (com.newrelic.agent.environment.Environment)5 MetricName (com.newrelic.agent.metric.MetricName)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Attribute (javax.management.Attribute)5 MBeanServer (javax.management.MBeanServer)5 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)4 Stats (com.newrelic.agent.stats.Stats)4 HttpError (com.newrelic.agent.transport.HttpError)4 CountStatistic (javax.management.j2ee.statistics.CountStatistic)4