Search in sources :

Example 6 with Aggregation

use of org.spf4j.tsdb2.avro.Aggregation in project spf4j by zolyfarkas.

the class AvroMeasurementStoreTest method testStore.

@Test
@SuppressFBWarnings({ "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS", "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN" })
public void testStore() throws IOException {
    AvroMeasurementStore store = new AvroMeasurementStore(org.spf4j.base.Runtime.TMP_FOLDER_PATH, "testMetrics", Compressor.ZSTANDARD);
    try {
        long mid = store.alocateMeasurements(new MeasurementsInfoImpl("test", "test", new String[] { "v1", "v2" }, new String[] { "t1", "t2" }, new Aggregation[] { Aggregation.SUM, Aggregation.LAST }, MeasurementType.GAUGE), 1000);
        store.saveMeasurements(mid, 0L, 1, 2);
        store.saveMeasurements(mid, 1000L, 3, 4);
        store.saveMeasurements(mid, 2000L, 5, 6);
        store.saveMeasurements(mid, 2000L, 7, 8);
        store.saveMeasurements(mid, 3000L, 9, 10);
        store.saveMeasurements(mid, 4000L, 11, 12);
        store.flush();
        MeasurementStoreQuery query = store.query();
        Collection<Schema> measurements = query.getMeasurements((x) -> true);
        Schema metric = measurements.iterator().next();
        Assert.assertEquals("test", metric.getName());
        List<TimeSeriesRecord> results = getMetrics(query, metric, Instant.EPOCH, Instant.now());
        Assert.assertEquals(6, results.size());
        TimeSeriesRecord rec = results.get(0);
        Assert.assertEquals(Instant.ofEpochMilli(0L), rec.getTimeStamp());
        Assert.assertEquals(1L, rec.getLongValue("v1"));
        Assert.assertEquals(2L, rec.getLongValue("v2"));
        rec = results.get(5);
        Assert.assertEquals(Instant.ofEpochMilli(4000L), rec.getTimeStamp());
        Assert.assertEquals(11L, rec.getLongValue("v1"));
        Assert.assertEquals(12L, rec.getLongValue("v2"));
        results = getMetrics(query, metric, Instant.ofEpochMilli(2000L), Instant.ofEpochMilli(2000L));
        Assert.assertEquals(2, results.size());
        rec = results.get(0);
        Assert.assertEquals(Instant.ofEpochMilli(2000L), rec.getTimeStamp());
        Assert.assertEquals(5L, rec.getLongValue("v1"));
        Assert.assertEquals(6L, rec.getLongValue("v2"));
        rec = results.get(1);
        Assert.assertEquals(Instant.ofEpochMilli(2000L), rec.getTimeStamp());
        Assert.assertEquals(7L, rec.getLongValue("v1"));
        Assert.assertEquals(8L, rec.getLongValue("v2"));
        List<TimeSeriesRecord> aggr = getMetrics(query, metric, Instant.EPOCH, Instant.now(), 1000);
        Assert.assertEquals(5, aggr.size());
        rec = aggr.get(0);
        Assert.assertEquals(Instant.ofEpochMilli(0L), rec.getTimeStamp());
        Assert.assertEquals(1L, rec.getLongValue("v1"));
        Assert.assertEquals(2L, rec.getLongValue("v2"));
        rec = aggr.get(2);
        Assert.assertEquals(Instant.ofEpochMilli(2000L), rec.getTimeStamp());
        Assert.assertEquals(12L, rec.getLongValue("v1"));
        Assert.assertEquals(8L, rec.getLongValue("v2"));
        aggr = getMetrics(query, metric, Instant.EPOCH, Instant.now(), 1500);
        Assert.assertEquals(5, aggr.size());
        aggr = getMetrics(query, metric, Instant.EPOCH, Instant.now(), 0);
        Assert.assertEquals(6, aggr.size());
        try (AvroCloseableIterable<TimeSeriesRecord> it = query.getAggregatedMeasurementData(metric, Instant.EPOCH, Instant.now(), Duration.between(Instant.EPOCH, Instant.now()).getSeconds(), TimeUnit.SECONDS)) {
            Iterator<TimeSeriesRecord> iterator = it.iterator();
            TimeSeriesRecord next = iterator.next();
            LOG.debug("The aggregate", next);
            Assert.assertFalse(iterator.hasNext());
            Assert.assertEquals(36L, next.getLongValue("v1"));
            Assert.assertEquals(12L, next.getLongValue("v2"));
        }
    } finally {
        store.close();
        Files.delete(store.getInfoFile());
        Files.delete(store.getDataFile());
    }
}
Also used : Aggregation(org.spf4j.tsdb2.avro.Aggregation) MeasurementStoreQuery(org.spf4j.perf.MeasurementStoreQuery) MeasurementsInfoImpl(org.spf4j.perf.impl.MeasurementsInfoImpl) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) Schema(org.apache.avro.Schema) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Aggregation (org.spf4j.tsdb2.avro.Aggregation)6 Schema (org.apache.avro.Schema)3 Beta (com.google.common.annotations.Beta)2 DirectStoreMultiAccumulator (org.spf4j.perf.impl.acc.DirectStoreMultiAccumulator)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 MeasurementStoreQuery (org.spf4j.perf.MeasurementStoreQuery)1 TimeSeriesRecord (org.spf4j.perf.TimeSeriesRecord)1 MeasurementsInfoImpl (org.spf4j.perf.impl.MeasurementsInfoImpl)1 ColumnDef (org.spf4j.tsdb2.avro.ColumnDef)1 TableDef (org.spf4j.tsdb2.avro.TableDef)1