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
.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
;
34 * Base abstract class for JUL benchmarks. Sub-classes can setup parameters to
35 * test different types of log handlers.
37 @ExtendWith(TestPrintExtension
.class)
38 public abstract class JulHandlerBenchmarkBase
{
40 // ------------------------------------------------------------------------
41 // Configurable test parameters
42 // ------------------------------------------------------------------------
44 /** Nb of runs per test, result will be averaged */
45 private static final int NB_RUNS
= 10;
47 /** Trace/log events per run */
48 private static final int NB_ITER
= 100000;
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};
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 protected Logger logger
;
58 protected Handler handler
;
60 // ------------------------------------------------------------------------
61 // Maintenance methods
62 // ------------------------------------------------------------------------
69 /* Set up the logger */
70 logger
= Logger
.getLogger("Test logger");
71 logger
.setUseParentHandlers(false);
72 logger
.setLevel(Level
.ALL
);
74 /* Sub-classes' @Before will setup the Handler */
81 public void teardown() {
82 if (handler
!= null) {
83 logger
.removeHandler(handler
);
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 * Main test class for running the benchmark
98 public void runBenchmark() {
99 if (logger
!= null && handler
!= null) {
100 logger
.addHandler(handler
);
103 System
.out
.println();
104 System
.out
.println("Running benchmark: " + this.getClass().getCanonicalName());
105 for (int i
: NB_THREADS
) {
110 private static void runTest(Logger log
, int nbThreads
) {
112 for (int i
= 0; i
< NB_RUNS
; i
++) {
113 Runner runner
= new Runner(nbThreads
, NB_ITER
, log
);
115 long start
= System
.nanoTime();
117 long end
= System
.nanoTime();
119 total
+= (end
- start
);
121 long average
= (total
/ NB_RUNS
);
122 System
.out
.println(nbThreads
+ " threads, average = " + average
/ NB_ITER
+ " ns/loop");
125 // ------------------------------------------------------------------------
127 // ------------------------------------------------------------------------
129 private static class Runner
implements Runnable
{
131 private final List
<Worker
> workers
= new LinkedList
<>();
132 private final List
<Thread
> workerThreads
= new LinkedList
<>();
134 public Runner(int nbThreads
, int nbIter
, Logger log
) {
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
));
145 workerThreads
.forEach(Thread
::start
);
147 workerThreads
.forEach(t
-> {
150 } catch (InterruptedException e
) {
156 private static class Worker
implements Runnable
{
158 private final Logger log
;
159 private final int threadId
;
160 private final int nbIter
;
162 @SuppressWarnings("unused")
163 private volatile int value
= 0;
165 public Worker(int threadId
, int nbIter
, Logger log
) {
167 this.threadId
= threadId
;
168 this.nbIter
= nbIter
;
173 for (int i
= 0; i
< nbIter
; i
++) {
176 log
.info("Thread " + threadId
+ ", iteration " + i
);
This page took 0.03495 seconds and 5 git commands to generate.