use of org.opennms.newts.api.Counter in project newts by OpenNMS.
the class RateTest method testMissing.
@Test
public void testMissing() {
Results<Sample> input = new Results<>();
Timestamp start = Timestamp.fromEpochMillis(1000);
Duration step = Duration.seconds(1);
// row_1
input.addElement(new Sample(start, m_resource, m_metrics[0], COUNTER, new Counter(0)));
input.addElement(new Sample(start, m_resource, m_metrics[1], COUNTER, new Counter(0)));
// row_2
input.addElement(new Sample(start.plus(step), m_resource, m_metrics[0], COUNTER, new Counter(100)));
input.addElement(new Sample(start.plus(step), m_resource, m_metrics[1], COUNTER, new Counter(100)));
// row_3 (sample for m_metrics[0] missing)
input.addElement(new Sample(start.plus(step.times(2)), m_resource, m_metrics[1], COUNTER, new Counter(200)));
// row_4
input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[0], COUNTER, new Counter(300)));
input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[1], COUNTER, new Counter(300)));
Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(2)).iterator();
// result_1 is always null
assertTrue(output.hasNext());
assertEquals(new Gauge(Double.NaN), output.next().getElement(m_metrics[0]).getValue());
// result_2, rate 100
assertTrue(output.hasNext());
assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
// result_3, missing because sample in row_3 is missing
assertTrue(output.hasNext());
assertNull(output.next().getElement(m_metrics[0]));
// result_4, rate of 100 calculated between row_4 and row_2
assertTrue(output.hasNext());
assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
}
use of org.opennms.newts.api.Counter in project opennms by OpenNMS.
the class NewtsConverter method toSample.
protected static Sample toSample(AbstractDS ds, Resource resource, Timestamp timestamp, double value) {
final String metric = ds.getName();
final MetricType type = ds.isCounter() ? MetricType.COUNTER : MetricType.GAUGE;
final ValueType<?> valueType = ds.isCounter() ? new Counter(UnsignedLong.valueOf(BigDecimal.valueOf(value).toBigInteger())) : new Gauge(value);
return new Sample(timestamp, resource, metric, type, valueType);
}
use of org.opennms.newts.api.Counter in project opennms by OpenNMS.
the class NewtsWriterTest method canWriteToSampleRepositoryUsingMultipleThreads.
/**
* Uses a latch to verify that multiple that multiple threads
* are used to concurrently insert samples into the SampleRepository.
*/
@Test
public void canWriteToSampleRepositoryUsingMultipleThreads() {
int ringBufferSize = 1024;
int numWriterThreads = 8;
LatchedSampleRepository sampleRepo = new LatchedSampleRepository(numWriterThreads);
MetricRegistry registry = new MetricRegistry();
NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
writer.setSampleRepository(sampleRepo);
for (int i = 0; i < ringBufferSize * 2; i++) {
Resource x = new Resource("x");
Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
writer.insert(Lists.newArrayList(s));
}
}
use of org.opennms.newts.api.Counter in project opennms by OpenNMS.
the class NewtsWriterTest method samplesAreDroppedWhenRingBufferIsFull.
/**
* Fills the ring buffer and locks all of the writer threads to verify
* that samples additional samples are dropped.
*/
@Test
public void samplesAreDroppedWhenRingBufferIsFull() throws Exception {
Resource x = new Resource("x");
int ringBufferSize = 1024;
int numWriterThreads = 8;
Lock lock = new ReentrantLock();
LockedSampleRepository sampleRepo = new LockedSampleRepository(lock);
MetricRegistry registry = new MetricRegistry();
NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
writer.setSampleRepository(sampleRepo);
lock.lock();
for (int i = 0; i < ringBufferSize; i++) {
Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
writer.insert(Lists.newArrayList(s));
}
// The ring buffer should be full, and all of the threads should be locked
Thread.sleep(250);
assertEquals(numWriterThreads, sampleRepo.getNumThreadsLocked());
// Attempt to insert another batch of samples
for (int i = 0; i < 8; i++) {
Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
writer.insert(Lists.newArrayList(s));
}
;
// Unlock the writer threads and wait for the ring buffer to drain
lock.unlock();
writer.destroy();
// Verify the number of inserted samples
assertEquals(0, sampleRepo.getNumThreadsLocked());
assertEquals(ringBufferSize, sampleRepo.getNumSamplesInserted());
}
use of org.opennms.newts.api.Counter in project newts by OpenNMS.
the class RateTest method test.
@Test
public void test() {
Results<Sample> input = new Results<>();
int rows = 10, cols = 2, rate = 100;
for (int i = 1; i <= rows; i++) {
Timestamp t = Timestamp.fromEpochMillis(i * 1000);
for (int j = 0; j < cols; j++) {
input.addElement(new Sample(t, m_resource, m_metrics[j], COUNTER, new Counter((i + j) * rate)));
}
}
Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(cols)).iterator();
for (int i = 1; i <= rows; i++) {
assertTrue("Insufficient number of results", output.hasNext());
Results.Row<Sample> row = output.next();
assertEquals("Unexpected row timestamp", Timestamp.fromEpochMillis(i * 1000), row.getTimestamp());
assertEquals("Unexpected row resource", m_resource, row.getResource());
assertEquals("Unexpected number of columns", cols, row.getElements().size());
for (int j = 0; j < cols; j++) {
String name = m_metrics[j];
assertNotNull("Missing sample" + name, row.getElement(name));
assertEquals("Unexpected sample name", name, row.getElement(name).getName());
assertEquals("Unexpected sample type", GAUGE, row.getElement(name).getType());
// Samples in the first row are null, this is normal.
if (i != 1) {
assertEquals("Incorrect rate value", 100.0d, row.getElement(name).getValue().doubleValue(), 0.0d);
}
}
}
}
Aggregations