Search in sources :

Example 6 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord 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)

Example 7 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord in project spf4j by zolyfarkas.

the class AvroMeasurementStoreTest method getMetrics.

public static List<TimeSeriesRecord> getMetrics(final MeasurementStoreQuery query, final Schema metric, final Instant from, final Instant to) throws IOException {
    List<TimeSeriesRecord> results = new ArrayList<>();
    try (AvroCloseableIterable<TimeSeriesRecord> data = query.getMeasurementData(metric, from, to)) {
        for (TimeSeriesRecord rec : data) {
            LOG.debug("data", rec);
            results.add(rec);
        }
    }
    return results;
}
Also used : TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) ArrayList(java.util.ArrayList)

Aggregations

TimeSeriesRecord (org.spf4j.perf.TimeSeriesRecord)7 Schema (org.apache.avro.Schema)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 JFileChooser (javax.swing.JFileChooser)1 TreePath (javax.swing.tree.TreePath)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 CsvEncoder (org.spf4j.avro.csv.CsvEncoder)1 CsvWriter (org.spf4j.io.csv.CsvWriter)1 MeasurementStoreQuery (org.spf4j.perf.MeasurementStoreQuery)1 MeasurementsInfoImpl (org.spf4j.perf.impl.MeasurementsInfoImpl)1 Aggregation (org.spf4j.tsdb2.avro.Aggregation)1