201ff838d6b32974d4b0e94c81a451bf6ff12021
[lttng-ust-java-tests.git] /
1 /*
2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 package org.lttng.ust.agent.benchmarks.jul.handler;
20
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;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 /**
32 * Base abstract class for JUL benchmarks. Sub-classes can setup parameters to
33 * test different types of log handlers.
34 */
35 public abstract class JulHandlerBenchmarkBase {
36
37 // ------------------------------------------------------------------------
38 // Configurable test parameters
39 // ------------------------------------------------------------------------
40
41 /** Nb of runs per test, result will be averaged */
42 private static final int NB_RUNS = 10;
43
44 /** Trace/log events per run */
45 private static final int NB_ITER = 100000;
46
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};
49
50 // ------------------------------------------------------------------------
51 // Attributes
52 // ------------------------------------------------------------------------
53
54 protected Logger logger;
55 protected Handler handler;
56
57 // ------------------------------------------------------------------------
58 // Maintenance methods
59 // ------------------------------------------------------------------------
60
61 /**
62 * Base test setup
63 */
64 @Before
65 public void setup() {
66 /* Set up the logger */
67 logger = Logger.getLogger("Test logger");
68 logger.setUseParentHandlers(false);
69 logger.setLevel(Level.ALL);
70
71 /* Sub-classes' @Before will setup the Handler */
72 }
73
74 /**
75 * Base test teardown
76 */
77 @After
78 public void teardown() {
79 if (handler != null) {
80 logger.removeHandler(handler);
81 handler.close();
82 }
83 handler = null;
84 logger = null;
85 }
86
87 // ------------------------------------------------------------------------
88 // Test methods
89 // ------------------------------------------------------------------------
90
91 /**
92 * Main test class for running the benchmark
93 */
94 @Test
95 public void runBenchmark() {
96 if (logger != null && handler != null) {
97 logger.addHandler(handler);
98 }
99
100 System.out.println();
101 System.out.println("Running benchmark: " + this.getClass().getCanonicalName());
102 for (int i : NB_THREADS) {
103 runTest(logger, i);
104 }
105 }
106
107 private static void runTest(Logger log, int nbThreads) {
108 long total = 0;
109 for (int i = 0; i < NB_RUNS; i++) {
110 Runner runner = new Runner(nbThreads, NB_ITER, log);
111
112 long start = System.nanoTime();
113 runner.run();
114 long end = System.nanoTime();
115
116 total += (end - start);
117 }
118 long average = (total / NB_RUNS);
119 System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/loop");
120 }
121
122 // ------------------------------------------------------------------------
123 // Helper classes
124 // ------------------------------------------------------------------------
125
126 private static class Runner implements Runnable {
127
128 private final List<Worker> workers = new LinkedList<>();
129 private final List<Thread> workerThreads = new LinkedList<>();
130
131 public Runner(int nbThreads, int nbIter, Logger log) {
132
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));
137 }
138 }
139
140 @Override
141 public void run() {
142 workerThreads.forEach(Thread::start);
143
144 workerThreads.forEach(t -> {
145 try {
146 t.join();
147 } catch (InterruptedException e) {
148 e.printStackTrace();
149 }
150 });
151 }
152
153 private static class Worker implements Runnable {
154
155 private final Logger log;
156 private final int threadId;
157 private final int nbIter;
158
159 @SuppressWarnings("unused")
160 private volatile int value = 0;
161
162 public Worker(int threadId, int nbIter, Logger log) {
163 this.log = log;
164 this.threadId = threadId;
165 this.nbIter = nbIter;
166 }
167
168 @Override
169 public void run() {
170 for (int i = 0; i < nbIter; i++) {
171 value = i;
172 if (log != null) {
173 log.info("Thread " + threadId + ", iteration " + i);
174 }
175 }
176 }
177 }
178 }
179 }
This page took 0.032568 seconds and 3 git commands to generate.