All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.quasardb.qdb.PerformanceTrace Maven / Gradle / Ivy

Go to download

API for the JNI components of the QuasarDB API for Java. Should not be included directly.

There is a newer version: 3.14.1
Show newest version
package net.quasardb.qdb;

import java.util.Collection;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

import net.quasardb.qdb.jni.qdb;
import net.quasardb.qdb.jni.Reference;

import net.quasardb.qdb.Session;
import net.quasardb.qdb.ts.Timespec;

public class PerformanceTrace {
    private static final Logger logger = LoggerFactory.getLogger(PerformanceTrace.class);

    public static class Measurement {
        private static final Logger logger = LoggerFactory.getLogger(Measurement.class);

        public String label;
        public long elapsed;

        public Measurement(String label, long elapsed) {
            this.label = label;
            this.elapsed = elapsed;
        }

        public String toString() {
            return "Measurement (label='" + this.label + "' elapsed=" + this.elapsed + ")";
        }

        public void log() {
            logger.debug("");
        }
    }

    public static class Trace {
        private static final Logger logger = LoggerFactory.getLogger(Trace.class);

        public String name;
        public Measurement[] measurements;

        public Trace(String name, Measurement[] measurements) {
            this.name = name;
            this.measurements = measurements;
        }

        public String toString() {
            String ret = "Trace (name='" + this.name + "', measurements='" + Arrays.toString(this.measurements) + ")";


            return ret;
        }

        public void log() {
            logger.debug("");
            for (Measurement m : measurements) {
                m.log();
            }
            logger.debug("");
        }

    }

    public static void enable(Session s) {
        s.throwIfClosed();

        qdb.enable_performance_trace(s.handle());
    }

    public static void disable(Session s) {
        s.throwIfClosed();

        qdb.disable_performance_trace(s.handle());
    }

    /**
     * Access available performance traces.
     *
     * This returns all performance traces that have been recorded recently.
     */
    public static Collection get(Session s) {
        s.throwIfClosed();

        Reference result = new Reference();

        qdb.get_performance_traces(s.handle(), result);

        return Arrays.asList(result.get());
    }

    /**
     * Access available performance traces, and clear the cache.
     *
     * This is effectively the same as calling get() and pop() after each other.
     */
    public static Collection pop(Session s) {
        Collection result = get(s);

        clear(s);

        return result;
    }

    /**
     * Clear performance trace cache.
     *
     * Clears all performance traces in cache associated with the session.
     */
    public static void clear(Session s) {
        s.throwIfClosed();

        qdb.clear_performance_traces(s.handle());
    }

    /**
     * Log all available traces directly through slf4j.
     *
     * Default implementation automatically clears the trace cache after logging.
     */
    public static void log(Session s) {
        log(s, true);
    }

    public static void log(Session s, boolean clear) {
        logger.warn("");

        for (Trace t : get(s)) {
            t.log();
        }

        logger.warn("");

        if (clear) {
            clear(s);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy