Search in sources :

Example 1 with MeasurementRecorder

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

the class CpuUsageSampler method start.

@JmxExport
public static synchronized void start(@JmxExport("sampleTimeMillis") final int sampleTime) {
    if (samplingFuture == null) {
        final MeasurementRecorder cpuUsage = RecorderFactory.createDirectRecorder("cpu-time", "ns", sampleTime);
        samplingFuture = DefaultScheduler.INSTANCE.scheduleWithFixedDelay(new AbstractRunnable() {

            private long lastValue = 0;

            @Override
            public void doRun() {
                long currTime = getProcessCpuTimeNanos();
                cpuUsage.record(currTime - lastValue);
                lastValue = currTime;
            }
        }, sampleTime, sampleTime, TimeUnit.MILLISECONDS);
    } else {
        throw new IllegalStateException("Cpu time Sampling already started " + samplingFuture);
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) MeasurementRecorder(org.spf4j.perf.MeasurementRecorder) JmxExport(org.spf4j.jmx.JmxExport)

Example 2 with MeasurementRecorder

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

the class GCUsageSampler method start.

@JmxExport
public static synchronized void start(@JmxExport("sampleTimeMillis") final int sampleTime) {
    if (samplingFuture == null) {
        final MeasurementRecorder gcUsage = RecorderFactory.createDirectRecorder("gc-time", "ms", sampleTime);
        samplingFuture = DefaultScheduler.INSTANCE.scheduleWithFixedDelay(new AbstractRunnable() {

            private final TObjectLongMap lastValues = new TObjectLongHashMap();

            @Override
            public void doRun() {
                synchronized (lastValues) {
                    gcUsage.record(getGCTimeDiff(MBEANS, lastValues));
                }
            }
        }, sampleTime, sampleTime, TimeUnit.MILLISECONDS);
    } else {
        throw new IllegalStateException("GC usage sampling already started " + samplingFuture);
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) TObjectLongHashMap(gnu.trove.map.hash.TObjectLongHashMap) MeasurementRecorder(org.spf4j.perf.MeasurementRecorder) TObjectLongMap(gnu.trove.map.TObjectLongMap) JmxExport(org.spf4j.jmx.JmxExport)

Example 3 with MeasurementRecorder

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

the class ThreadUsageSampler method start.

@JmxExport
public static synchronized void start(@JmxExport("sampleTimeMillis") final int sampleTime, @JmxExport("withStackTraces") final boolean withStackTraces) {
    if (samplingFuture == null) {
        final MeasurementRecorder cpuUsage = RecorderFactory.createDirectRecorder("peak-thread-count", "count", sampleTime);
        samplingFuture = DefaultScheduler.INSTANCE.scheduleWithFixedDelay(new AbstractRunnable() {

            private int maxThreadsNr = 0;

            @Override
            public void doRun() {
                final int peakThreadCount = TH_BEAN.getPeakThreadCount();
                cpuUsage.record(peakThreadCount);
                if (peakThreadCount > maxThreadsNr) {
                    Thread[] ths = Threads.getThreads();
                    if (ths.length > PEAK_THREAD_NAMES.size()) {
                        if (withStackTraces) {
                            StackTraceElement[][] stackTraces = Threads.getStackTraces(ths);
                            PEAK_THREAD_TRACES.clear();
                            PEAK_THREAD_TRACES.addAll(Arrays.asList(stackTraces));
                        }
                        PEAK_THREAD_NAMES.clear();
                        int i = 0;
                        for (Thread th : ths) {
                            PEAK_THREAD_NAMES.add(th.getName());
                            if (th.isDaemon()) {
                                PEAK_THREAD_DAEMON.set(i);
                            } else {
                                PEAK_THREAD_DAEMON.clear(i);
                            }
                            i++;
                        }
                    }
                    maxThreadsNr = peakThreadCount;
                }
                TH_BEAN.resetPeakThreadCount();
            }
        }, sampleTime, sampleTime, TimeUnit.MILLISECONDS);
    } else {
        throw new IllegalStateException("Thread sampling already started " + samplingFuture);
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) MeasurementRecorder(org.spf4j.perf.MeasurementRecorder) JmxExport(org.spf4j.jmx.JmxExport)

Example 4 with MeasurementRecorder

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

the class DirectRecorderSourceTest method testDirectRecorder.

@Test
public void testDirectRecorder() throws IOException {
    MeasurementRecorderSource recorderSource = RecorderFactory.createDirectRecorderSource("test", "description");
    MeasurementRecorder recorder = recorderSource.getRecorder("A");
    long time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        recorder.recordAt(time + (long) i * 1000, i);
    }
    MeasurementRecorder recorder2 = recorderSource.getRecorder("B");
    for (int i = 0; i < 100; i++) {
        recorder2.recordAt(time + (long) i * 1000, i);
    }
    recorderSource.close();
    org.spf4j.perf.RecorderFactoryTest.assertData("test,A", 4950);
    org.spf4j.perf.RecorderFactoryTest.assertData("test,B", 4950);
}
Also used : MeasurementRecorder(org.spf4j.perf.MeasurementRecorder) MeasurementRecorderSource(org.spf4j.perf.MeasurementRecorderSource) Test(org.junit.Test)

Example 5 with MeasurementRecorder

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

the class GraphiteUdpStoreTest method testStore.

@Test
@SuppressFBWarnings("MDM_THREAD_YIELD")
public void testStore() throws InterruptedException, IOException {
    MeasurementRecorder recorder = RecorderFactory.createScalableQuantizedRecorder("test measurement", "ms", 1000, 10, 0, 6, 10);
    for (int i = 0; i < 100; i++) {
        recorder.record(i);
        Thread.sleep(100);
    }
    recorder.close();
    RecorderFactory.MEASUREMENT_STORE.flush();
    // RecorderFactory.MEASUREMENT_STORE.close();
    List<String> lines = Files.readAllLines(TSDB_TXT.toPath(), StandardCharsets.UTF_8);
    LOG.debug("measurements = {}", lines);
    Assert.assertThat(lines, Matchers.hasItem(Matchers.allOf(Matchers.containsString("Q6_7"), Matchers.containsString("test measurement"))));
    String line = QUEUE.poll(5, TimeUnit.SECONDS);
    Assert.assertThat(line, Matchers.containsString("test-measurement"));
}
Also used : MeasurementRecorder(org.spf4j.perf.MeasurementRecorder) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

MeasurementRecorder (org.spf4j.perf.MeasurementRecorder)8 Test (org.junit.Test)5 AbstractRunnable (org.spf4j.base.AbstractRunnable)3 JmxExport (org.spf4j.jmx.JmxExport)3 MeasurementRecorderSource (org.spf4j.perf.MeasurementRecorderSource)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 TObjectLongMap (gnu.trove.map.TObjectLongMap)1 TObjectLongHashMap (gnu.trove.map.hash.TObjectLongHashMap)1 CompositeData (javax.management.openmbean.CompositeData)1