2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 package org.lttng.ust.agent.benchmarks.jul.handler;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.logging.Handler;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
32 * Base abstract class for JUL benchmarks. Sub-classes can setup parameters to
33 * test different types of log handlers.
35 public abstract class JulHandlerBenchmarkBase {
37 // ------------------------------------------------------------------------
38 // Configurable test parameters
39 // ------------------------------------------------------------------------
41 /** Nb of runs per test, result will be averaged */
42 private static final int NB_RUNS = 10;
44 /** Trace/log events per run */
45 private static final int NB_ITER = 100000;
47 /** Which tests to run (for different number of threads) */
48 private static final int[] NB_THREADS = {1, 1, 2, 3, 4, 5, 6, 7, 8};
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 protected Logger logger;
55 protected Handler handler;
57 // ------------------------------------------------------------------------
58 // Maintenance methods
59 // ------------------------------------------------------------------------
66 /* Set up the logger */
67 logger = Logger.getLogger("Test logger");
68 logger.setUseParentHandlers(false);
69 logger.setLevel(Level.ALL);
71 /* Sub-classes' @Before will setup the Handler */
78 public void teardown() {
79 if (handler != null) {
80 logger.removeHandler(handler);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * Main test class for running the benchmark
95 public void runBenchmark() {
96 if (logger != null && handler != null) {
97 logger.addHandler(handler);
100 System.out.println();
101 System.out.println("Running benchmark: " + this.getClass().getCanonicalName());
102 for (int i : NB_THREADS) {
107 private static void runTest(Logger log, int nbThreads) {
109 for (int i = 0; i < NB_RUNS; i++) {
110 Runner runner = new Runner(nbThreads, NB_ITER, log);
112 long start = System.nanoTime();
114 long end = System.nanoTime();
116 total += (end - start);
118 long average = (total / NB_RUNS);
119 System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/loop");
122 // ------------------------------------------------------------------------
124 // ------------------------------------------------------------------------
126 private static class Runner implements Runnable {
128 private final List<Worker> workers = new LinkedList<>();
129 private final List<Thread> workerThreads = new LinkedList<>();
131 public Runner(int nbThreads, int nbIter, Logger log) {
133 for (int id = 0; id < nbThreads; id++) {
134 Worker curWorker = new Worker(id, nbIter, log);
135 workers.add(curWorker);
136 workerThreads.add(new Thread(curWorker, "worker " + id));
142 workerThreads.forEach(Thread::start);
144 workerThreads.forEach(t -> {
147 } catch (InterruptedException e) {
153 private static class Worker implements Runnable {
155 private final Logger log;
156 private final int threadId;
157 private final int nbIter;
159 @SuppressWarnings("unused")
160 private volatile int value = 0;
162 public Worker(int threadId, int nbIter, Logger log) {
164 this.threadId = threadId;
165 this.nbIter = nbIter;
170 for (int i = 0; i < nbIter; i++) {
173 log.info("Thread " + threadId + ", iteration " + i);