Search code examples
javaprofilerspf4j

How does the SPF4J Java Profiler write buffered metrics to files?


I wrote a simple test to capture timing metrics using the SPF4J (Simple Profiler Framework For Java) MeasurementRecorder. I'm running a simple for loop and capturing time of a random sleep from 100-200 ms as shown in the complete code sample below.

When I run the test, the Time-Series DataBase (TSDB) files are created successfully, but they're empty while the test is running (around 2 mins). The buffered data are written to files when the application completes, but samples at the end are missing and the last one is truncated, like the buffer is not getting flushed properly.

If the application never terminates (e.g. web service), when will the buffered metrics be written to file - on a timer, or when a certain amount of data is buffered? Is this configurable and, if so, how?

package com.examples.spf4j;

import org.spf4j.perf.MeasurementRecorder;
import org.spf4j.perf.impl.RecorderFactory;
import java.io.File;
import java.util.Random;
import org.junit.Test;

public class TestMeasurementRecorder {

    @Test
    public void testMeasurementRecorder() throws InterruptedException {
        initialize();
        MeasurementRecorder measurementRecorder = getMeasurementRecorder();
        Random random = new Random();

        for (int i=0; i<=1000; i++) {
            long startTime = System.currentTimeMillis();
            Thread.sleep(100 + random.nextInt(100));
            measurementRecorder.record(System.currentTimeMillis() - startTime);
        }
    }

    public static void initialize() {
        String tsDbFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitor.tsdb2";
        String tsTextFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitor.txt";
        System.setProperty("spf4j.perf.ms.config", "TSDB@" + tsDbFile + "," + "TSDB_TXT@" + tsTextFile);
    }

    public static MeasurementRecorder getMeasurementRecorder() {
        int sampleTimeMillis = 1000;
        return RecorderFactory.createScalableQuantizedRecorder("response time", "ms", sampleTimeMillis, 10, 0, 40, 10);
    }
}

Solution

  • You will need to set the system property: spf4j.perf.ms.periodicFlush=true to enable the periodic flush.