From: Alexandre Montplaisir Date: Thu, 23 Jul 2015 15:22:44 +0000 (-0400) Subject: Rename the project to a general ust-java-tests X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=da8308fe655f5008237400e26160fab0792b7c1d;p=lttng-ust-java-tests.git Rename the project to a general ust-java-tests This can hold both unit/integration tests as well as benchmarks. Reshuffle the package names a bit to do so. Signed-off-by: Alexandre Montplaisir --- diff --git a/.project b/.project index 1b1828e..0617969 100644 --- a/.project +++ b/.project @@ -1,6 +1,6 @@ - lttng-ust-agent-jul-benchmarks + ust-java-tests diff --git a/src/org/lttng/ust/agent/benchmarks/jul/RunAllBenchmarks.java b/src/org/lttng/ust/agent/benchmarks/jul/RunAllBenchmarks.java new file mode 100644 index 0000000..e8fa16d --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/RunAllBenchmarks.java @@ -0,0 +1,17 @@ +package org.lttng.ust.agent.benchmarks.jul; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + org.lttng.ust.agent.benchmarks.jul.handler.NoHandlerBenchmark.class, + org.lttng.ust.agent.benchmarks.jul.handler.DummyHandlerBenchmark.class, +// org.lttng.ust.agent.jul.benchmarks.handler.FileHandlerBenchmark.class, + org.lttng.ust.agent.benchmarks.jul.handler.lttng.LttngJulHandlerTracingDisabledBenchmark.class, + org.lttng.ust.agent.benchmarks.jul.handler.lttng.LttngJulHandlerTracingEnabledBenchmark.class, + org.lttng.ust.agent.benchmarks.jul.handler.lttng.old.OldLttngJulHandlerTracingDisabledBenchmark.class, + org.lttng.ust.agent.benchmarks.jul.handler.lttng.old.OldLttngJulHandlerTracingEnabledBenchmark.class +}) +public class RunAllBenchmarks { +} diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/AbstractJulBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/AbstractJulBenchmark.java new file mode 100644 index 0000000..a089e9d --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/AbstractJulBenchmark.java @@ -0,0 +1,145 @@ +package org.lttng.ust.agent.benchmarks.jul.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/benchmarks/jul/handler/DummyHandlerBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/DummyHandlerBenchmark.java new file mode 100644 index 0000000..d962b42 --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/DummyHandlerBenchmark.java @@ -0,0 +1,31 @@ +package org.lttng.ust.agent.benchmarks.jul.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/benchmarks/jul/handler/FileHandlerBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/FileHandlerBenchmark.java new file mode 100644 index 0000000..d6095aa --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/FileHandlerBenchmark.java @@ -0,0 +1,16 @@ +package org.lttng.ust.agent.benchmarks.jul.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/benchmarks/jul/handler/NoHandlerBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/NoHandlerBenchmark.java new file mode 100644 index 0000000..424b32e --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/NoHandlerBenchmark.java @@ -0,0 +1,6 @@ +package org.lttng.ust.agent.benchmarks.jul.handler; + +public class NoHandlerBenchmark extends AbstractJulBenchmark { + + /* Do not setup any handler */ +} diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java new file mode 100644 index 0000000..690af1d --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java @@ -0,0 +1,28 @@ +package org.lttng.ust.agent.benchmarks.jul.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.benchmarks.jul.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.LttngLogHandler; +import org.lttng.ust.agent.utils.LttngSessionControl; +import org.lttng.ust.agent.utils.LttngSessionControl.Domain; + +public class LttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() throws IOException { + handler = new LttngLogHandler(); + + assertTrue(LttngSessionControl.setupSession(null, Domain.JUL)); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + } +} diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java new file mode 100644 index 0000000..eb0514e --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java @@ -0,0 +1,28 @@ +package org.lttng.ust.agent.benchmarks.jul.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.benchmarks.jul.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.LttngLogHandler; +import org.lttng.ust.agent.utils.LttngSessionControl; +import org.lttng.ust.agent.utils.LttngSessionControl.Domain; + +public class LttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() throws IOException { + handler = new LttngLogHandler(); + + assertTrue(LttngSessionControl.setupSessionAllEvents(null, Domain.JUL)); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + } +} diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java new file mode 100644 index 0000000..c8cbe6e --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java @@ -0,0 +1,31 @@ +package org.lttng.ust.agent.benchmarks.jul.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.benchmarks.jul.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.utils.LttngSessionControl; +import org.lttng.ust.agent.utils.LttngSessionControl.Domain; + +@SuppressWarnings("deprecation") +public class OldLttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { + + @Before + public void testSetup() throws IOException { + LTTngAgent.getLTTngAgent(); + + assertTrue(LttngSessionControl.setupSession(null, Domain.JUL)); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + + LTTngAgent.dispose(); + } +} diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java new file mode 100644 index 0000000..3e6636b --- /dev/null +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java @@ -0,0 +1,57 @@ +package org.lttng.ust.agent.benchmarks.jul.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.benchmarks.jul.handler.AbstractJulBenchmark; +import org.lttng.ust.agent.jul.LttngLogHandler; +import org.lttng.ust.agent.utils.LttngSessionControl; +import org.lttng.ust.agent.utils.LttngSessionControl.Domain; + +@SuppressWarnings("deprecation") +public class OldLttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { + + private LttngLogHandler agentHandler; + + @Before + public void testSetup() throws IOException { + LTTngAgent agentInstance = LTTngAgent.getLTTngAgent(); + + /* + * The "old API" works by attaching a handler managed by the agent to + * the root JUL logger. This causes problems 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 julHandlerField = LTTngAgent.class.getDeclaredField("julHandler"); + julHandlerField.setAccessible(true); + agentHandler = (LttngLogHandler) julHandlerField.get(agentInstance); + + logger.addHandler(agentHandler); + + } catch (ReflectiveOperationException e) { + fail(); + } + + assertTrue(LttngSessionControl.setupSessionAllEvents(null, Domain.JUL)); + } + + @After + public void testTeardown() { + assertTrue(LttngSessionControl.stopSession()); + assertTrue(LttngSessionControl.destroySession()); + + logger.removeHandler(agentHandler); + LTTngAgent.dispose(); + } +} diff --git a/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java b/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java new file mode 100644 index 0000000..27f033e --- /dev/null +++ b/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java @@ -0,0 +1,180 @@ +package org.lttng.ust.agent.integration.jul; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +import java.io.IOException; +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.BeforeClass; +import org.junit.Test; +import org.lttng.ust.agent.jul.LttngLogHandler; +import org.lttng.ust.agent.utils.LttngSessionControl; +import org.lttng.ust.agent.utils.LttngSessionControl.Domain; + +public class JulEnabledEventsTest { + + private static final Domain DOMAIN = Domain.JUL; + + private static final String EVENT_NAME_A = "EventA"; + private static final String EVENT_NAME_B = "EventAB"; + private static final String EVENT_NAME_C = "EventABC"; + private static final String EVENT_NAME_D = "EventABCD"; + + private Logger loggerA; + private Logger loggerB; + private Logger loggerC; + private Logger loggerD; + + private Handler handlerA; + private Handler handlerB; + private Handler handlerC; + + @BeforeClass + public static void classSetup() { + /* Skip tests if we can't find the JNI library or lttng-tools */ + try { + LttngLogHandler testHandler = new LttngLogHandler(); + testHandler.close(); + } catch (SecurityException | IOException e) { + assumeTrue(false); + } + + boolean ret1 = LttngSessionControl.setupSession(null, DOMAIN); + boolean ret2 = LttngSessionControl.stopSession(); + boolean ret3 = LttngSessionControl.destroySession(); + assumeTrue(ret1 && ret2 && ret3); + } + + @Before + public void setup() throws SecurityException, IOException { + loggerA = Logger.getLogger(EVENT_NAME_A); + loggerB = Logger.getLogger(EVENT_NAME_B); + loggerC = Logger.getLogger(EVENT_NAME_C); + loggerD = Logger.getLogger(EVENT_NAME_D); + + loggerA.setLevel(Level.ALL); + loggerB.setLevel(Level.ALL); + loggerC.setLevel(Level.ALL); + loggerD.setLevel(Level.ALL); + + handlerA = new LttngLogHandler(); + handlerB = new LttngLogHandler(); + handlerC = new LttngLogHandler(); + + loggerA.addHandler(handlerA); + loggerB.addHandler(handlerB); + loggerC.addHandler(handlerB); + } + + @After + public void teardown() { + loggerA.removeHandler(handlerA); + loggerB.removeHandler(handlerB); + loggerC.removeHandler(handlerC); + + handlerA.close(); + handlerB.close(); + handlerC.close(); + + loggerA = null; + loggerB = null; + loggerC = null; + loggerD = null; + handlerA = null; + handlerB = null; + handlerC = null; + } + + /** + * Test sending events on the Java side, but no events enabled in the + * tracing session. There should be nothing in the resulting trace. + */ + @Test + public void testNoEvents() { + assertTrue(LttngSessionControl.setupSession(null, DOMAIN)); + + send10Events(loggerA); + send10Events(loggerB); + send10Events(loggerC); + send10Events(loggerD); + + assertTrue(LttngSessionControl.stopSession()); + + List output = LttngSessionControl.viewSession(); + assertNotNull(output); + assertTrue(output.isEmpty()); + + assertTrue(LttngSessionControl.destroySession()); + } + + /** + * Test sending events on the Java side, and all events enabled in the + * tracing session. All handlers should have sent their events. + */ + @Test + public void testAllEvents() { + assertTrue(LttngSessionControl.setupSessionAllEvents(null, DOMAIN)); + + send10Events(loggerA); + send10Events(loggerB); + send10Events(loggerC); + send10Events(loggerD); + + assertTrue(LttngSessionControl.stopSession()); + + List output = LttngSessionControl.viewSession(); + assertNotNull(output); + assertEquals(20, output.size()); // loggerC has no handler attached + + assertTrue(LttngSessionControl.destroySession()); + } + + /** + * Test sending events on the Java side, with only some of them enabled in + * the tracing session. Only the subset that is enabled should be received. + */ + @Test + public void testSomeEvents() { + assertTrue(LttngSessionControl.setupSession(null, DOMAIN, + EVENT_NAME_A, EVENT_NAME_D)); + + send10Events(loggerA); + send10Events(loggerB); + send10Events(loggerC); + send10Events(loggerD); + + assertTrue(LttngSessionControl.stopSession()); + + List output = LttngSessionControl.viewSession(); + assertNotNull(output); + assertEquals(10, output.size()); // loggerC has no handler attached + + assertTrue(LttngSessionControl.destroySession()); + } + + private static void send10Events(Logger logger) { + String a = new String("a"); + Object[] params = {a, new String("b"), new Object()}; + + // Levels are FINE, FINER, FINEST, INFO, SEVERE, WARNING + logger.fine("A fine level message"); + logger.finer("A finer level message"); + logger.finest("A finest level message"); + logger.info("A info level message"); + logger.severe("A severe level message"); + logger.warning("A warning level message"); + logger.warning("Another warning level message"); + logger.log(Level.WARNING, "A warning message using Logger.log()"); + logger.log(Level.INFO, "A message with one parameter", a); + logger.log(Level.INFO, "A message with parameters", params); + } + +} diff --git a/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java b/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java deleted file mode 100644 index 42ce901..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/RunAllTests.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.lttng.ust.agent.jul.benchmarks; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({ - 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 deleted file mode 100644 index 0f51012..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/AbstractJulBenchmark.java +++ /dev/null @@ -1,145 +0,0 @@ -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 deleted file mode 100644 index 5675f9b..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/DummyHandlerBenchmark.java +++ /dev/null @@ -1,31 +0,0 @@ -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 deleted file mode 100644 index 203316d..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/FileHandlerBenchmark.java +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 3d1aab4..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/NoHandlerBenchmark.java +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index cd7a59a..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index d3db3c8..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index a7afc03..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java +++ /dev/null @@ -1,32 +0,0 @@ -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 deleted file mode 100644 index 28851a4..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java +++ /dev/null @@ -1,62 +0,0 @@ -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 deleted file mode 100644 index 8407be0..0000000 --- a/src/org/lttng/ust/agent/jul/benchmarks/utils/LttngSessionControl.java +++ /dev/null @@ -1,66 +0,0 @@ -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; - } - } -} diff --git a/src/org/lttng/ust/agent/utils/LttngSessionControl.java b/src/org/lttng/ust/agent/utils/LttngSessionControl.java new file mode 100644 index 0000000..48eacc5 --- /dev/null +++ b/src/org/lttng/ust/agent/utils/LttngSessionControl.java @@ -0,0 +1,174 @@ +package org.lttng.ust.agent.utils; + +import java.io.IOException; +import java.lang.ProcessBuilder.Redirect; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public final class LttngSessionControl { + + private LttngSessionControl() {} + + public enum Domain { + JUL("-j"), + LOG4J("-l"); + + private final String flag; + + private Domain(String flag) { + this.flag = flag; + } + + public String flag() { + return flag; + } + } + + // ------------------------------------------------------------------------ + // Public utility methods + // ------------------------------------------------------------------------ + + /** + * Setup a LTTng session by enabling certain events (or none). + * + * @param sessionName + * The name of the session to create. May be null to use the + * default one from lttng-tools. + * @param domain + * The tracing domain + * @param enabledEvents + * The list of events to enable. May be null or empty, to not + * enable any events. + * @return If the command executed successfully (return code = 0). + */ + public static boolean setupSession(String sessionName, Domain domain, String... enabledEvents) { + String[] createCommand = (sessionName == null ? + new String[] { "lttng", "create" } : + new String[] { "lttng", "create", sessionName} + ); + + String eventsToEnable = (enabledEvents == null || enabledEvents.length == 0 ? + /* + * 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 + */ + "non-event" : + Arrays.stream(enabledEvents).collect(Collectors.joining(",")) + ); + + return executeCommands(new String[][] { + createCommand, + { "lttng", "enable-event", domain.flag(), eventsToEnable}, + { "lttng", "start" } + }); + } + + /** + * Setup a LTTng session with all events enabled (lttng enable-event -a). + * + * @param sessionName + * The name of the session to create. May be null to use the + * default one from lttng-tools. + * @param domain + * The tracing domain + * @return If the command executed successfully (return code = 0). + */ + public static boolean setupSessionAllEvents(String sessionName, Domain domain) { + String[] createCommand = (sessionName == null ? + new String[] { "lttng", "create" } : + new String[] { "lttng", "create", sessionName} + ); + + return executeCommands(new String[][] { + createCommand, + { "lttng", "enable-event", domain.flag(), "-a" }, + { "lttng", "start" } + }); + } + + /** + * Stop the current tracing session + * + * @return If the command executed successfully (return code = 0). + */ + public static boolean stopSession() { + return executeCommand(new String[] { "lttng", "stop" }); + } + + public static List viewSession() { + return getOutputFromCommand(new String[] { "lttng", "view" }); + } + + /** + * Destroy the current tracing session + * + * @return If the command executed successfully (return code = 0). + */ + public static boolean destroySession() { + return executeCommand(new String[] { "lttng", "destroy" }); + } + + // ------------------------------------------------------------------------ + // Private helper methods + // ------------------------------------------------------------------------ + + private static boolean executeCommands(String [][] commands) { + for (String[] command : commands) { + if (executeCommand(command) == false) { + return false; + } + } + return true; + } + + /** + * Just to test the environment / stdout are working correctly + */ + 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; + } + } + + private static List getOutputFromCommand(String[] command) { + try { + Path tempFile = Files.createTempFile("test-output", null); + + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectErrorStream(true); + builder.redirectOutput(Redirect.to(tempFile.toFile())); + + Process p = builder.start(); + p.waitFor(); + + List lines = Files.readAllLines(tempFile); + Files.delete(tempFile); + + /* Also print the output to the console */ + lines.stream().forEach(s -> System.out.println(s)); + + return lines; + + } catch (IOException | InterruptedException e) { + return null; + } + } +}