简体   繁体   English

SPF4J Java Profiler 如何将缓冲的指标写入文件?

[英]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.我使用SPF4J (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.我正在运行一个简单的 for 循环并捕获 100-200 毫秒的随机睡眠时间,如下面的完整代码示例所示。

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).当我运行测试时,时间序列数据库 (TSDB) 文件已成功创建,但在测试运行时它们是空的(大约 2 分钟)。 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 (eg web service), when will the buffered metrics be written to file - on a timer, or when a certain amount of data is buffered?如果应用程序永远不会终止(例如 web 服务),缓冲的指标何时会写入文件 - 在计时器上,还是在缓冲一定数量的数据时? 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);
    }
}

You will need to set the system property: spf4j.perf.ms.periodicFlush=true to enable the periodic flush.您需要设置系统属性: spf4j.perf.ms.periodicFlush=true 以启用定期刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM