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