Search in sources :

Example 6 with MockNormalizer

use of com.newrelic.agent.MockNormalizer in project newrelic-java-agent by newrelic.

the class InstrumentTestUtils method getAndClearMetricData.

public static Map<String, Integer> getAndClearMetricData() {
    StatsEngine statsEngine = AgentHelper.getDefaultStatsEngine();
    List<MetricData> data = statsEngine.getMetricData(new MockNormalizer());
    Map<String, Integer> dataMap = new HashMap<>();
    for (MetricData metricData : data) {
        StatsBase stats = metricData.getStats();
        if (stats instanceof CountStats) {
            dataMap.put(metricData.getMetricName().getName(), ((CountStats) stats).getCallCount());
        } else {
            dataMap.put(metricData.getMetricName().getName(), 0);
        }
    }
    return dataMap;
}
Also used : HashMap(java.util.HashMap) CountStats(com.newrelic.agent.stats.CountStats) StatsBase(com.newrelic.agent.stats.StatsBase) StatsEngine(com.newrelic.agent.stats.StatsEngine) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData)

Example 7 with MockNormalizer

use of com.newrelic.agent.MockNormalizer in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDataNormalize.

@Test
public void getMetricDataNormalize() {
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    Stats stats = statsEngine.getStats("Test");
    stats.recordDataPoint(100);
    stats.recordDataPoint(200);
    MockNormalizer metricNormalizer = new MockNormalizer();
    Map<String, String> normalizationResults = new HashMap<>();
    normalizationResults.put("Test", "Test2");
    metricNormalizer.setNormalizationResults(normalizationResults);
    List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
    Assert.assertEquals(1, data.size());
    Assert.assertEquals(1, statsEngine.getSize());
    Assert.assertEquals("Test2", data.get(0).getMetricName().getName());
    Assert.assertNull(data.get(0).getMetricId());
    Assert.assertEquals(300f, ((CountStats) data.get(0).getStats()).getTotal(), 0);
    Assert.assertEquals(2, ((CountStats) data.get(0).getStats()).getCallCount());
}
Also used : HashMap(java.util.HashMap) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 8 with MockNormalizer

use of com.newrelic.agent.MockNormalizer in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDataScopedAndUnscoped2.

@Test
public void getMetricDataScopedAndUnscoped2() {
    try {
        StatsEngineImpl statsEngine = new StatsEngineImpl();
        // create the stats
        Transaction tx = Transaction.getTransaction();
        String methodName = "foo";
        ResponseTimeStats fooStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        String unscopedMetricName = "hi";
        ResponseTimeStats hiStats = tx.getTransactionActivity().getTransactionStats().getUnscopedStats().getOrCreateResponseTimeStats(unscopedMetricName);
        hiStats.recordResponseTimeInNanos(500000, 400000);
        hiStats.recordResponseTimeInNanos(100000, 100000);
        // add the scoped metrics under the scope
        statsEngine.mergeStatsResolvingScope(tx.getTransactionActivity().getTransactionStats(), "theScope");
        MockNormalizer metricNormalizer = new MockNormalizer();
        List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
        Assert.assertEquals(3, data.size());
        // do the validation
        // foo scoped, foo unscoped, hi unscoped
        ResponseTimeStats[] output = new ResponseTimeStats[3];
        for (MetricData d : data) {
            String name = d.getMetricName().getName();
            if (name.equals(methodName)) {
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[2]);
                    output[0] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[2]);
                    output[1] = (ResponseTimeStats) d.getStats();
                }
            } else {
                Assert.assertEquals(unscopedMetricName, name);
                if (d.getMetricName().isScoped()) {
                    Assert.fail("There should not be a scoped metric for " + unscopedMetricName);
                } else {
                    Assert.assertNull(output[2]);
                    output[2] = (ResponseTimeStats) d.getStats();
                }
            }
        }
        // validate the stats
        // they should be different objects with the same numbers
        Assert.assertFalse("The object should have the same values but be different objects", output[0] == output[1]);
        // validate variables
        Assert.assertEquals(1, output[0].getCallCount());
        Assert.assertEquals("The total was incorrect.", 2.0f, output[0].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 1.0f, output[0].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(1, output[1].getCallCount());
        Assert.assertEquals("The total was incorrect.", 2.0f, output[1].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 1.0f, output[1].getTotalExclusiveTime(), .000001f);
        Assert.assertEquals(2, output[2].getCallCount());
        Assert.assertEquals("The total was incorrect.", .0006f, output[2].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", .0005f, output[2].getTotalExclusiveTime(), .000001f);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 9 with MockNormalizer

use of com.newrelic.agent.MockNormalizer in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDataFromDataUsageStats.

@Test
public void getMetricDataFromDataUsageStats() {
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    DataUsageStats stats = statsEngine.getDataUsageStats(MetricName.create("Test"));
    stats.recordDataUsage(100, 5);
    stats.recordDataUsage(300, 10);
    MockNormalizer metricNormalizer = new MockNormalizer();
    List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
    Assert.assertEquals(1, data.size());
    Assert.assertEquals(1, statsEngine.getSize());
    Assert.assertEquals("Test", data.get(0).getMetricName().getName());
    Assert.assertNull(data.get(0).getMetricId());
    Assert.assertEquals(400, ((DataUsageStats) data.get(0).getStats()).getBytesSent());
    Assert.assertEquals(15, ((DataUsageStats) data.get(0).getStats()).getBytesReceived());
}
Also used : MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 10 with MockNormalizer

use of com.newrelic.agent.MockNormalizer in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDifferentScopes.

/**
 * Tests that metrics with the same name but a different scope get merged into the same unscoped metric.
 */
@Test
public void getMetricDifferentScopes() {
    try {
        StatsEngineImpl statsEngine = new StatsEngineImpl();
        // create the stats
        Transaction tx = Transaction.getTransaction();
        String methodName1 = "foo";
        ResponseTimeStats fooStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName1);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(1000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        // add the scoped metrics under the scope
        statsEngine.mergeStatsResolvingScope(tx.getTransactionActivity().getTransactionStats(), "theScope");
        statsEngine.mergeStatsResolvingScope(tx.getTransactionActivity().getTransactionStats(), "secondTrans");
        MockNormalizer metricNormalizer = new MockNormalizer();
        List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
        Assert.assertEquals(3, data.size());
        // do the validation
        // foo scoped, foo unscoped, hi scoped, hi unscoped
        ResponseTimeStats[] output = new ResponseTimeStats[3];
        for (MetricData d : data) {
            String name = d.getMetricName().getName();
            if (name.equals(methodName1)) {
                if (d.getMetricName().isScoped()) {
                    if (output[0] == null) {
                        output[0] = (ResponseTimeStats) d.getStats();
                    } else {
                        Assert.assertNull(output[1]);
                        output[1] = (ResponseTimeStats) d.getStats();
                    }
                } else {
                    Assert.assertNull(output[2]);
                    output[2] = (ResponseTimeStats) d.getStats();
                }
            } else {
                Assert.fail("The method name is invalid: " + name);
            }
        }
        // validate the stats
        // they should be different objects
        Assert.assertFalse("The objects should be different.", output[1] == output[2]);
        // validate call count
        Assert.assertEquals(6, output[0].getCallCount());
        Assert.assertEquals("The total was incorrect.", 11.0f, output[0].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 6.0f, output[0].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(6, output[1].getCallCount());
        Assert.assertEquals("The total was incorrect.", 11.0f, output[1].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 6.0f, output[1].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(12, output[2].getCallCount());
        Assert.assertEquals("The total was incorrect.", 22.0f, output[2].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 12.0f, output[2].getTotalExclusiveTime(), .000001f);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Aggregations

MetricData (com.newrelic.agent.MetricData)10 MockNormalizer (com.newrelic.agent.MockNormalizer)10 Test (org.junit.Test)8 Transaction (com.newrelic.agent.Transaction)4 HashMap (java.util.HashMap)3 CountStats (com.newrelic.agent.stats.CountStats)1 StatsBase (com.newrelic.agent.stats.StatsBase)1 StatsEngine (com.newrelic.agent.stats.StatsEngine)1