Commit | Line | Data |
---|---|---|
2b408e85 AM |
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 | ||
da8308fe | 19 | package org.lttng.ust.agent.benchmarks.jul.handler; |
7ac7128a AM |
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 | ||
7a4f0255 MJ |
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; | |
7ac7128a | 32 | |
8a0613fa AM |
33 | /** |
34 | * Base abstract class for JUL benchmarks. Sub-classes can setup parameters to | |
35 | * test different types of log handlers. | |
36 | */ | |
7a4f0255 | 37 | @ExtendWith(TestPrintExtension.class) |
d4e2e87c | 38 | public abstract class JulHandlerBenchmarkBase { |
7ac7128a AM |
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 | ||
86316987 | 57 | protected Logger logger; |
7ac7128a AM |
58 | protected Handler handler; |
59 | ||
60 | // ------------------------------------------------------------------------ | |
61 | // Maintenance methods | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
8a0613fa AM |
64 | /** |
65 | * Base test setup | |
66 | */ | |
7a4f0255 | 67 | @BeforeEach |
7ac7128a AM |
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 | ||
8a0613fa AM |
77 | /** |
78 | * Base test teardown | |
79 | */ | |
7a4f0255 | 80 | @AfterEach |
7ac7128a | 81 | public void teardown() { |
7ac7128a | 82 | if (handler != null) { |
86316987 | 83 | logger.removeHandler(handler); |
7ac7128a AM |
84 | handler.close(); |
85 | } | |
86 | handler = null; | |
87 | logger = null; | |
88 | } | |
89 | ||
90 | // ------------------------------------------------------------------------ | |
91 | // Test methods | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
8a0613fa AM |
94 | /** |
95 | * Main test class for running the benchmark | |
96 | */ | |
7ac7128a AM |
97 | @Test |
98 | public void runBenchmark() { | |
249f8797 | 99 | if (logger != null && handler != null) { |
7ac7128a AM |
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) { | |
249f8797 | 111 | long total = 0; |
7ac7128a AM |
112 | for (int i = 0; i < NB_RUNS; i++) { |
113 | Runner runner = new Runner(nbThreads, NB_ITER, log); | |
114 | ||
249f8797 | 115 | long start = System.nanoTime(); |
7ac7128a | 116 | runner.run(); |
249f8797 | 117 | long end = System.nanoTime(); |
7ac7128a AM |
118 | |
119 | total += (end - start); | |
120 | } | |
249f8797 AM |
121 | long average = (total / NB_RUNS); |
122 | System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/loop"); | |
7ac7128a AM |
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() { | |
249f8797 | 145 | workerThreads.forEach(Thread::start); |
7ac7128a | 146 | |
249f8797 | 147 | workerThreads.forEach(t -> { |
7ac7128a | 148 | try { |
249f8797 | 149 | t.join(); |
7ac7128a AM |
150 | } catch (InterruptedException e) { |
151 | e.printStackTrace(); | |
152 | } | |
249f8797 | 153 | }); |
7ac7128a AM |
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 | ||
249f8797 AM |
162 | @SuppressWarnings("unused") |
163 | private volatile int value = 0; | |
164 | ||
7ac7128a AM |
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++) { | |
249f8797 AM |
174 | value = i; |
175 | if (log != null) { | |
176 | log.info("Thread " + threadId + ", iteration " + i); | |
177 | } | |
7ac7128a AM |
178 | } |
179 | } | |
7ac7128a AM |
180 | } |
181 | } | |
182 | } |