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;
}
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());
}
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());
}
}
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());
}
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());
}
}
Aggregations