Search in sources :

Example 1 with MeasurementStoreQuery

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

the class FileMonitorAspectTest method testFileIOMonitoring.

/**
 * Test of nioReadLong method, of class FileMonitorAspect.
 */
@Test
public void testFileIOMonitoring() throws Exception {
    System.setProperty("spf4j.perf.file.sampleTimeMillis", "1000");
    File tempFile = File.createTempFile("test", ".tmp");
    tempFile.deleteOnExit();
    try (Writer fw = new OutputStreamWriter(Files.newOutputStream(tempFile.toPath()), StandardCharsets.UTF_8)) {
        for (int i = 0; i < 10; i++) {
            fw.write("bla bla test\n");
            Thread.sleep(500);
        }
    }
    Thread.sleep(1000);
    try (BufferedReader fr = new BufferedReader(new InputStreamReader(Files.newInputStream(tempFile.toPath()), StandardCharsets.UTF_8))) {
        String line = fr.readLine();
        while (line != null) {
            LOG.debug("Read: {}", line);
            line = fr.readLine();
            Thread.sleep(500);
        }
    }
    RecorderFactory.MEASUREMENT_STORE.flush();
    MeasurementStoreQuery query = RecorderFactory.MEASUREMENT_STORE.query();
    Collection<Schema> measurements = query.getMeasurements((x) -> true);
    LOG.debug("Tables {}", measurements);
    THashSet<String> collect = measurements.stream().map((x) -> x.getProp(TimeSeriesRecord.RAW_NAME)).collect(Collectors.toCollection(() -> new THashSet<String>(measurements.size())));
    Assert.assertThat(collect, (Matcher) Matchers.hasItem("file-write,org.spf4j.perf.aspects.FileMonitorAspectTest"));
    Assert.assertThat(collect, (Matcher) Matchers.hasItem("file-read,org.spf4j.perf.aspects.FileMonitorAspectTest"));
    Collection<Schema> fmw = query.getMeasurements((x) -> "file_write_org_spf4j_perf_aspects_FileMonitorAspectTest".equals(x));
    try (AvroCloseableIterable<Observation> obs = query.getObservations(fmw.iterator().next(), Instant.EPOCH, Instant.now())) {
        Assert.assertTrue(obs.iterator().hasNext());
    }
    Assert.assertTrue(OperatingSystem.getOpenFileDescriptorCount() > 0);
}
Also used : AvroCloseableIterable(org.spf4j.base.avro.AvroCloseableIterable) LoggerFactory(org.slf4j.LoggerFactory) RecorderFactory(org.spf4j.perf.impl.RecorderFactory) OutputStreamWriter(java.io.OutputStreamWriter) Observation(org.spf4j.tsdb2.avro.Observation) Schema(org.apache.avro.Schema) Logger(org.slf4j.Logger) MeasurementStoreQuery(org.spf4j.perf.MeasurementStoreQuery) Files(java.nio.file.Files) Collection(java.util.Collection) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Instant(java.time.Instant) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) OperatingSystem(org.spf4j.os.OperatingSystem) THashSet(gnu.trove.set.hash.THashSet) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) Matcher(org.hamcrest.Matcher) Writer(java.io.Writer) BufferedReader(java.io.BufferedReader) Assert(org.junit.Assert) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) InputStreamReader(java.io.InputStreamReader) Schema(org.apache.avro.Schema) THashSet(gnu.trove.set.hash.THashSet) MeasurementStoreQuery(org.spf4j.perf.MeasurementStoreQuery) BufferedReader(java.io.BufferedReader) Observation(org.spf4j.tsdb2.avro.Observation) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 2 with MeasurementStoreQuery

use of org.spf4j.perf.MeasurementStoreQuery 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 3 with MeasurementStoreQuery

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

the class AllocationMonitorAspectTest method testAfterAllocation.

/**
 * Test of afterAllocation method, of class AllocationMonitorAspect.
 */
@Test
public void testAfterAllocation() throws InterruptedException, IOException {
    System.setProperty("spf4j.perf.allocations.sampleTimeMillis", "1000");
    MemoryUsageSampler.start(500);
    OpenFilesSampler.start(500, 512, 1000, false);
    for (int i = 0; i < 1000; i++) {
        LOG.debug("T{}", i);
        if (i % 100 == 0) {
            Thread.sleep(500);
        }
    }
    testAllocInStaticContext();
    TestClass.testAllocInStaticContext();
    MeasurementStore measurementStore = ProcessMeasurementStore.getMeasurementStore();
    measurementStore.flush();
    MeasurementStoreQuery query = measurementStore.query();
    Collection<Schema> measurements = query.getMeasurements((x) -> "process_heap_used".equals(x));
    Assert.assertEquals(1, measurements.size());
    MemoryUsageSampler.stop();
    OpenFilesSampler.stop();
}
Also used : MeasurementStoreQuery(org.spf4j.perf.MeasurementStoreQuery) Schema(org.apache.avro.Schema) ProcessMeasurementStore(org.spf4j.perf.impl.ProcessMeasurementStore) MeasurementStore(org.spf4j.perf.MeasurementStore) Test(org.junit.Test)

Aggregations

Schema (org.apache.avro.Schema)3 Test (org.junit.Test)3 MeasurementStoreQuery (org.spf4j.perf.MeasurementStoreQuery)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 TimeSeriesRecord (org.spf4j.perf.TimeSeriesRecord)2 THashSet (gnu.trove.set.hash.THashSet)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Instant (java.time.Instant)1 Collection (java.util.Collection)1 Collectors (java.util.stream.Collectors)1 Matcher (org.hamcrest.Matcher)1 Matchers (org.hamcrest.Matchers)1 Assert (org.junit.Assert)1 Logger (org.slf4j.Logger)1