From: Alexandre Montplaisir Date: Tue, 21 Jul 2015 20:12:53 +0000 (-0400) Subject: Add benchmarks for old LTTng-JUL API vs. new one X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=86316987b44d0adfa063772b97b4ee31a61c88fc;p=lttng-ust-java-tests.git Add benchmarks for old LTTng-JUL API vs. new one Also move the tests in subpackages. --- diff --git a/.classpath b/.classpath index bf7cf4e..0b8166e 100644 --- a/.classpath +++ b/.classpath @@ -4,5 +4,6 @@ + diff --git a/.project b/.project index b0299db..1b1828e 100644 --- a/.project +++ b/.project @@ -1,6 +1,6 @@ - test + lttng-ust-agent-jul-benchmarks diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 53d2118..3b5cbc1 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -365,7 +365,7 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constan org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.join_lines_in_comments=true -org.eclipse.jdt.core.formatter.join_wrapped_lines=true +org.eclipse.jdt.core.formatter.join_wrapped_lines=false org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false diff --git a/src/org/lttng/ust/agent/jul/benchmarks/AbstractJulBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/AbstractJulBenchmark.java deleted file mode 100644 index 01c213b..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/AbstractJulBenchmark.java +++ /dev/null @@ -1,145 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -import java.util.LinkedList; -import java.util.List; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public abstract class AbstractJulBenchmark { - - // ------------------------------------------------------------------------ - // Configurable test parameters - // ------------------------------------------------------------------------ - - /** Nb of runs per test, result will be averaged */ - private static final int NB_RUNS = 10; - - /** Trace/log events per run */ - private static final int NB_ITER = 100000; - - /** Which tests to run (for different number of threads) */ - private static final int[] NB_THREADS = {1, 1, 2, 3, 4, 5, 6, 7, 8}; - - // ------------------------------------------------------------------------ - // Attributes - // ------------------------------------------------------------------------ - - private Logger logger; - protected Handler handler; - - // ------------------------------------------------------------------------ - // Maintenance methods - // ------------------------------------------------------------------------ - - @Before - public void setup() { - /* Set up the logger */ - logger = Logger.getLogger("Test logger"); - logger.setUseParentHandlers(false); - logger.setLevel(Level.ALL); - - /* Sub-classes' @Before will setup the Handler */ - } - - @After - public void teardown() { - logger.removeHandler(handler); - if (handler != null) { - handler.close(); - } - handler = null; - logger = null; - } - - // ------------------------------------------------------------------------ - // Test methods - // ------------------------------------------------------------------------ - - @Test - public void runBenchmark() { - if (handler != null) { - logger.addHandler(handler); - } - - System.out.println(); - System.out.println("Running benchmark: " + this.getClass().getCanonicalName()); - for (int i : NB_THREADS) { - runTest(logger, i); - } - } - - private static void runTest(Logger log, int nbThreads) { - long start, end, average, total = 0; - for (int i = 0; i < NB_RUNS; i++) { - Runner runner = new Runner(nbThreads, NB_ITER, log); - - start = System.nanoTime(); - runner.run(); - end = System.nanoTime(); - - total += (end - start); - } - average = total / NB_RUNS; - System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/event"); - } - - // ------------------------------------------------------------------------ - // Helper classes - // ------------------------------------------------------------------------ - - private static class Runner implements Runnable { - - private final List workers = new LinkedList<>(); - private final List workerThreads = new LinkedList<>(); - - public Runner(int nbThreads, int nbIter, Logger log) { - - for (int id = 0; id < nbThreads; id++) { - Worker curWorker = new Worker(id, nbIter, log); - workers.add(curWorker); - workerThreads.add(new Thread(curWorker, "worker " + id)); - } - } - - @Override - public void run() { - for (Thread curThread : workerThreads) { - curThread.start(); - } - - for (Thread curThread : workerThreads) { - try { - curThread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - private static class Worker implements Runnable { - - private final Logger log; - private final int threadId; - private final int nbIter; - - public Worker(int threadId, int nbIter, Logger log) { - this.log = log; - this.threadId = threadId; - this.nbIter = nbIter; - } - - @Override - public void run() { - for (int i = 0; i < nbIter; i++) { - log.info("Thread " + threadId + ", iteration " + i); - } - } - - } - } -} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/DummyHandlerBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/DummyHandlerBenchmark.java deleted file mode 100644 index 0bf2f59..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/DummyHandlerBenchmark.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -import java.util.logging.Handler; -import java.util.logging.LogRecord; - -import org.junit.Before; - -public class DummyHandlerBenchmark extends AbstractJulBenchmark { - - @Before - public void testSetup() { - handler = new DummyHandler(); - } - - private static class DummyHandler extends Handler { - - public DummyHandler() { - super(); - } - - @Override - public void close() throws SecurityException {} - - @Override - public void flush() {} - - @Override - public void publish(LogRecord record) {} - - } -} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/FileHandlerBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/FileHandlerBenchmark.java deleted file mode 100644 index 91a0419..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/FileHandlerBenchmark.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -import java.io.IOException; -import java.util.logging.FileHandler; -import java.util.logging.SimpleFormatter; - -import org.junit.Before; - -public class FileHandlerBenchmark extends AbstractJulBenchmark { - - @Before - public void testSetup() throws SecurityException, IOException { - handler = new FileHandler("/tmp/log", false); - handler.setFormatter(new SimpleFormatter()); - } -} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/LttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/LttngJulHandlerTracingDisabledBenchmark.java deleted file mode 100644 index 22106b6..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/LttngJulHandlerTracingDisabledBenchmark.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -import static org.junit.Assert.assertFalse; - -import org.junit.Before; -import org.lttng.ust.agent.jul.LTTngLogHandler; -import org.lttng.ust.agent.jul.LttngJulRootAgent; - -public class LttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { - - @Before - public void testSetup() { - handler = new LTTngLogHandler(true); - - assertFalse(LttngJulRootAgent.getInstance().listEnabledEvents().iterator().hasNext()); - } -} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/LttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/LttngJulHandlerTracingEnabledBenchmark.java deleted file mode 100644 index 506c7ab..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/LttngJulHandlerTracingEnabledBenchmark.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.lttng.ust.agent.jul.LTTngLogHandler; -import org.lttng.ust.agent.jul.LttngJulRootAgent; - -public class LttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { - - @Before - public void testSetup() { - handler = new LTTngLogHandler(true); - - LttngJulRootAgent agent = LttngJulRootAgent.getInstance(); - agent.enableEvent("*"); - assertTrue(agent.listEnabledEvents().iterator().hasNext()); - } -} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/NoHandlerBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/NoHandlerBenchmark.java deleted file mode 100644 index ec569af..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/NoHandlerBenchmark.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -public class NoHandlerBenchmark extends AbstractJulBenchmark { - - /* Do not setup any handler */ -} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java b/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java index 0f61c03..42ce901 100644 --- a/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java +++ b/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java @@ -5,11 +5,13 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ - DummyHandlerBenchmark.class, - FileHandlerBenchmark.class, - LttngJulHandlerTracingDisabledBenchmark.class, - LttngJulHandlerTracingEnabledBenchmark.class, - NoHandlerBenchmark.class + org.lttng.ust.agent.jul.benchmarks.handler.NoHandlerBenchmark.class, + org.lttng.ust.agent.jul.benchmarks.handler.DummyHandlerBenchmark.class, + org.lttng.ust.agent.jul.benchmarks.handler.FileHandlerBenchmark.class, + org.lttng.ust.agent.jul.benchmarks.handler.lttng.LttngJulHandlerTracingDisabledBenchmark.class, + org.lttng.ust.agent.jul.benchmarks.handler.lttng.LttngJulHandlerTracingEnabledBenchmark.class, + org.lttng.ust.agent.jul.benchmarks.handler.lttng.old.OldLttngJulHandlerTracingDisabledBenchmark.class, + org.lttng.ust.agent.jul.benchmarks.handler.lttng.old.OldLttngJulHandlerTracingEnabledBenchmark.class }) public class RunAllTests { } diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/AbstractJulBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/AbstractJulBenchmark.java new file mode 100644 index 0000000..0f51012 --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/AbstractJulBenchmark.java @@ -0,0 +1,145 @@ +package org.lttng.ust.agent.jul.benchmarks.handler; + +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public abstract class AbstractJulBenchmark { + + // ------------------------------------------------------------------------ + // Configurable test parameters + // ------------------------------------------------------------------------ + + /** Nb of runs per test, result will be averaged */ + private static final int NB_RUNS = 10; + + /** Trace/log events per run */ + private static final int NB_ITER = 100000; + + /** Which tests to run (for different number of threads) */ + private static final int[] NB_THREADS = {1, 1, 2, 3, 4, 5, 6, 7, 8}; + + // ------------------------------------------------------------------------ + // Attributes + // ------------------------------------------------------------------------ + + protected Logger logger; + protected Handler handler; + + // ------------------------------------------------------------------------ + // Maintenance methods + // ------------------------------------------------------------------------ + + @Before + public void setup() { + /* Set up the logger */ + logger = Logger.getLogger("Test logger"); + logger.setUseParentHandlers(false); + logger.setLevel(Level.ALL); + + /* Sub-classes' @Before will setup the Handler */ + } + + @After + public void teardown() { + if (handler != null) { + logger.removeHandler(handler); + handler.close(); + } + handler = null; + logger = null; + } + + // ------------------------------------------------------------------------ + // Test methods + // ------------------------------------------------------------------------ + + @Test + public void runBenchmark() { + if (handler != null) { + logger.addHandler(handler); + } + + System.out.println(); + System.out.println("Running benchmark: " + this.getClass().getCanonicalName()); + for (int i : NB_THREADS) { + runTest(logger, i); + } + } + + private static void runTest(Logger log, int nbThreads) { + long start, end, average, total = 0; + for (int i = 0; i < NB_RUNS; i++) { + Runner runner = new Runner(nbThreads, NB_ITER, log); + + start = System.nanoTime(); + runner.run(); + end = System.nanoTime(); + + total += (end - start); + } + average = total / NB_RUNS; + System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/event"); + } + + // ------------------------------------------------------------------------ + // Helper classes + // ------------------------------------------------------------------------ + + private static class Runner implements Runnable { + + private final List workers = new LinkedList<>(); + private final List workerThreads = new LinkedList<>(); + + public Runner(int nbThreads, int nbIter, Logger log) { + + for (int id = 0; id < nbThreads; id++) { + Worker curWorker = new Worker(id, nbIter, log); + workers.add(curWorker); + workerThreads.add(new Thread(curWorker, "worker " + id)); + } + } + + @Override + public void run() { + for (Thread curThread : workerThreads) { + curThread.start(); + } + + for (Thread curThread : workerThreads) { + try { + curThread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private static class Worker implements Runnable { + + private final Logger log; + private final int threadId; + private final int nbIter; + + public Worker(int threadId, int nbIter, Logger log) { + this.log = log; + this.threadId = threadId; + this.nbIter = nbIter; + } + + @Override + public void run() { + for (int i = 0; i < nbIter; i++) { + log.info("Thread " + threadId + ", iteration " + i); + } + } + + } + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/DummyHandlerBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/DummyHandlerBenchmark.java new file mode 100644 index 0000000..5675f9b --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/DummyHandlerBenchmark.java @@ -0,0 +1,31 @@ +package org.lttng.ust.agent.jul.benchmarks.handler; + +import java.util.logging.Handler; +import java.util.logging.LogRecord; + +import org.junit.Before; + +public class DummyHandlerBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() { + handler = new DummyHandler(); + } + + private static class DummyHandler extends Handler { + + public DummyHandler() { + super(); + } + + @Override + public void close() throws SecurityException {} + + @Override + public void flush() {} + + @Override + public void publish(LogRecord record) {} + + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/FileHandlerBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/FileHandlerBenchmark.java new file mode 100644 index 0000000..203316d --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/FileHandlerBenchmark.java @@ -0,0 +1,16 @@ +package org.lttng.ust.agent.jul.benchmarks.handler; + +import java.io.IOException; +import java.util.logging.FileHandler; +import java.util.logging.SimpleFormatter; + +import org.junit.Before; + +public class FileHandlerBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() throws SecurityException, IOException { + handler = new FileHandler("/tmp/log", false); + handler.setFormatter(new SimpleFormatter()); + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/NoHandlerBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/NoHandlerBenchmark.java new file mode 100644 index 0000000..3d1aab4 --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/NoHandlerBenchmark.java @@ -0,0 +1,6 @@ +package org.lttng.ust.agent.jul.benchmarks.handler; + +public class NoHandlerBenchmark extends AbstractJulBenchmark { + + /* Do not setup any handler */ +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java new file mode 100644 index 0000000..cd7a59a --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java @@ -0,0 +1,27 @@ +package org.lttng.ust.agent.jul.benchmarks.handler.lttng; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.lttng.ust.agent.jul.LTTngLogHandler; +import org.lttng.ust.agent.jul.benchmarks.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.benchmarks.utils.LttngSessionControl; + +public class LttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() throws IOException { + handler = new LTTngLogHandler(true); + + assertTrue(LttngSessionControl.setupJulSessionNoEvents()); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java new file mode 100644 index 0000000..d3db3c8 --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java @@ -0,0 +1,27 @@ +package org.lttng.ust.agent.jul.benchmarks.handler.lttng; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.lttng.ust.agent.jul.LTTngLogHandler; +import org.lttng.ust.agent.jul.benchmarks.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.benchmarks.utils.LttngSessionControl; + +public class LttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() throws IOException { + handler = new LTTngLogHandler(true); + + assertTrue(LttngSessionControl.setupJulSessionAllEvents()); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java new file mode 100644 index 0000000..a7afc03 --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java @@ -0,0 +1,32 @@ +package org.lttng.ust.agent.jul.benchmarks.handler.lttng.old; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.lttng.ust.agent.LTTngAgent; +import org.lttng.ust.agent.jul.benchmarks.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.benchmarks.utils.LttngSessionControl; + +@SuppressWarnings("deprecation") +public class OldLttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { + + private LTTngAgent agent; + + @Before + public void testSetup() throws IOException { + agent = LTTngAgent.getLTTngAgent(); + + assertTrue(LttngSessionControl.setupJulSessionNoEvents()); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + + agent.dispose(); + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java new file mode 100644 index 0000000..28851a4 --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java @@ -0,0 +1,62 @@ +package org.lttng.ust.agent.jul.benchmarks.handler.lttng.old; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.lang.reflect.Field; + +import org.junit.After; +import org.junit.Before; +import org.lttng.ust.agent.LTTngAgent; +import org.lttng.ust.agent.jul.LTTngJUL; +import org.lttng.ust.agent.jul.LTTngLogHandler; +import org.lttng.ust.agent.jul.benchmarks.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.benchmarks.utils.LttngSessionControl; + +@SuppressWarnings("deprecation") +public class OldLttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { + + private LTTngAgent agent; + private LTTngLogHandler oldHandler; + + @Before + public void testSetup() throws IOException { + agent = LTTngAgent.getLTTngAgent(); + + /* + * The "old API" works by attaching a handler managed by the agent to + * the root JUL logger. This causes problem here, because we use + * logger.setUserParentHandler(false), which does not trigger the + * handler as would be expected. + * + * Instead we will retrieve this handler through reflection, and attach + * it to our own logger here for the duration of the test. + */ + try { + Field julRootField = LTTngAgent.class.getDeclaredField("julRoot"); + julRootField.setAccessible(true); + LTTngJUL lf = (LTTngJUL) julRootField.get(null); // static field + + Field handlerField = LTTngJUL.class.getDeclaredField("handler"); + handlerField.setAccessible(true); + oldHandler = (LTTngLogHandler) handlerField.get(lf); + + logger.addHandler(oldHandler); + + } catch (ReflectiveOperationException e) { + fail(); + } + + assertTrue(LttngSessionControl.setupJulSessionAllEvents()); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + + logger.removeHandler(oldHandler); + agent.dispose(); + } +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/utils/LttngSessionControl.java b/src/org/lttng/ust/agent/jul/benchmarks/utils/LttngSessionControl.java new file mode 100644 index 0000000..8407be0 --- /dev/null +++ b/src/org/lttng/ust/agent/jul/benchmarks/utils/LttngSessionControl.java @@ -0,0 +1,66 @@ +package org.lttng.ust.agent.jul.benchmarks.utils; + +import java.io.IOException; +import java.lang.ProcessBuilder.Redirect; + +public final class LttngSessionControl { + + private LttngSessionControl() {} + + public static boolean setupJulSessionNoEvents() { + return executeCommands(new String[][] { + { "lttng", "create" }, + /* + * We have to enable a channel for 'lttng start' to work. + * However, we cannot enable a channel directly, see + * https://bugs.lttng.org/issues/894 . Instead we will enable an + * event we know does not exist + */ + { "lttng", "enable-event", "-j", "non-event" }, + { "lttng", "start" } + }); + } + + public static boolean setupJulSessionAllEvents() { + return executeCommands(new String[][] { + { "lttng", "create" }, + { "lttng", "enable-event", "-j", "-a" }, + { "lttng", "start" } + }); + } + + public static boolean stopSession() { + return executeCommand(new String[] { "lttng", "stop" }); + } + + public static boolean destroySession() { + return executeCommand(new String[] { "lttng", "destroy" }); + } + + private static boolean executeCommands(String [][] commands) { + for (String[] command : commands) { + if (executeCommand(command) == false) { + return false; + } + } + return true; + } + + public static void main(String[] args) { + executeCommand(new String[] {"ls", "-l"}); + } + + private static boolean executeCommand(String[] command) { + try { + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectErrorStream(true); + builder.redirectOutput(Redirect.INHERIT); + + Process p = builder.start(); + int ret = p.waitFor(); + return (ret == 0); + } catch (IOException | InterruptedException e) { + return false; + } + } +}