Search in sources :

Example 1 with MockNormalizer

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

the class StatsEngineTest method getMetricDataWithJavaOther.

@Test
public void getMetricDataWithJavaOther() {
    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);
        String methodName2 = "hi";
        ResponseTimeStats hiStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName2);
        hiStats.recordResponseTimeInNanos(1, 1);
        String methodName3 = "hello";
        ResponseTimeStats helloStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName3);
        helloStats.recordResponseTimeInNanos(2, 1);
        // 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(4, data.size());
        // do the validation
        // foo scoped, foo unscoped, hi scoped, hi unscoped
        ResponseTimeStats[] output = new ResponseTimeStats[4];
        for (MetricData d : data) {
            String name = d.getMetricName().getName();
            if (name.equals(methodName1)) {
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[0]);
                    output[0] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[1]);
                    output[1] = (ResponseTimeStats) d.getStats();
                }
            } else {
                // since the metrics are so small hi and hello
                // should be combined into a java other metric
                Assert.assertEquals("Java/other", name);
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[2]);
                    output[2] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[3]);
                    output[3] = (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]);
        Assert.assertFalse("The object should have the same values but be different objects", output[2] == output[3]);
        // validate call count
        Assert.assertEquals(3, output[0].getCallCount());
        Assert.assertEquals("The total was incorrect.", 5.0f, output[0].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 3.0f, output[0].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(3, output[1].getCallCount());
        Assert.assertEquals("The total was incorrect.", 5.0f, output[1].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 3.0f, output[1].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(2, output[2].getCallCount());
        Assert.assertEquals("The total was incorrect.", .000000003f, output[2].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", .000000002f, output[2].getTotalExclusiveTime(), .00000000001f);
        Assert.assertEquals(2, output[3].getCallCount());
        Assert.assertEquals("The total was incorrect.", .000000003f, output[3].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", .000000002f, output[3].getTotalExclusiveTime(), .00000000001f);
    } 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 2 with MockNormalizer

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

the class StatsEngineTest method getMetricData.

@Test
public void getMetricData() {
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    Stats stats = statsEngine.getStats("Test");
    stats.recordDataPoint(100);
    stats.recordDataPoint(200);
    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(300f, ((CountStats) data.get(0).getStats()).getTotal(), 0);
    Assert.assertEquals(2, ((CountStats) data.get(0).getStats()).getCallCount());
}
Also used : MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 3 with MockNormalizer

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

the class StatsEngineTest method getMetricDataNormalizeFromDataUsageStats.

@Test
public void getMetricDataNormalizeFromDataUsageStats() {
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    DataUsageStats stats = statsEngine.getDataUsageStats(MetricName.create("Test"));
    stats.recordDataUsage(100, 5);
    stats.recordDataUsage(300, 10);
    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(400, ((DataUsageStats) data.get(0).getStats()).getBytesSent());
    Assert.assertEquals(15, ((DataUsageStats) data.get(0).getStats()).getBytesReceived());
}
Also used : HashMap(java.util.HashMap) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 4 with MockNormalizer

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

the class StatsEngineTest method getMetricDataScopedAndUnscoped1.

@Test
public void getMetricDataScopedAndUnscoped1() {
    try {
        StatsEngineImpl statsEngine = new StatsEngineImpl();
        // create the stats
        Transaction tx = Transaction.getTransaction();
        String methodName1 = "foo";
        ResponseTimeStats fooStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName1);
        fooStats.setCallCount(1);
        String methodName2 = "hi";
        ResponseTimeStats hiStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName2);
        hiStats.setCallCount(3);
        // 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(4, data.size());
        // do the validation
        // foo scoped, foo unscoped, hi scoped, hi unscoped
        ResponseTimeStats[] output = new ResponseTimeStats[4];
        for (MetricData d : data) {
            String name = d.getMetricName().getName();
            if (name.equals(methodName1)) {
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[0]);
                    output[0] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[1]);
                    output[1] = (ResponseTimeStats) d.getStats();
                }
            } else {
                Assert.assertEquals(methodName2, name);
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[2]);
                    output[2] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[3]);
                    output[3] = (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]);
        Assert.assertFalse("The object should have the same values but be different objects", output[2] == output[3]);
        // validate call count
        Assert.assertEquals(1, output[0].getCallCount());
        Assert.assertEquals(1, output[1].getCallCount());
        Assert.assertEquals(3, output[2].getCallCount());
        Assert.assertEquals(3, output[3].getCallCount());
    } 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 5 with MockNormalizer

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

the class InstrumentTestUtils method verifyCountMetric.

public static void verifyCountMetric(Map<String, Integer> expected, StatsEngine engine) {
    List<MetricData> data = engine.getMetricData(new MockNormalizer());
    for (Entry<String, Integer> current : expected.entrySet()) {
        if (current.getValue() != null) {
            verifyMetricCount(current.getKey(), current.getValue(), data);
        } else {
            printData(data);
            Assert.fail(MessageFormat.format("The expected count for {0} should not be null", current.getKey()));
        }
    }
}
Also used : MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData)

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