eccf1b363459f6f024d48f5ecde5386a29ebfe46
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
.Tag
;
30 import org
.junit
.jupiter
.api
.Test
;
31 import org
.junit
.jupiter
.api
.extension
.ExtendWith
;
32 import org
.lttng
.ust
.agent
.utils
.TestPrintExtension
;
35 * Base abstract class for JUL benchmarks. Sub-classes can setup parameters to
36 * test different types of log handlers.
38 @ExtendWith(TestPrintExtension
.class)
42 public abstract class JulHandlerBenchmarkBase
{
44 // ------------------------------------------------------------------------
45 // Configurable test parameters
46 // ------------------------------------------------------------------------
48 /** Nb of runs per test, result will be averaged */
49 private static final int NB_RUNS
= 10;
51 /** Trace/log events per run */
52 private static final int NB_ITER
= 100000;
54 /** Which tests to run (for different number of threads) */
55 private static final int[] NB_THREADS
= {1, 1, 2, 3, 4, 5, 6, 7, 8};
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 protected Logger logger
;
62 protected Handler handler
;
64 // ------------------------------------------------------------------------
65 // Maintenance methods
66 // ------------------------------------------------------------------------
73 /* Set up the logger */
74 logger
= Logger
.getLogger("Test logger");
75 logger
.setUseParentHandlers(false);
76 logger
.setLevel(Level
.ALL
);
78 /* Sub-classes' @Before will setup the Handler */
85 public void teardown() {
86 if (handler
!= null) {
87 logger
.removeHandler(handler
);
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * Main test class for running the benchmark
102 public void runBenchmark() {
103 if (logger
!= null && handler
!= null) {
104 logger
.addHandler(handler
);
107 System
.out
.println();
108 System
.out
.println("Running benchmark: " + this.getClass().getCanonicalName());
109 for (int i
: NB_THREADS
) {
114 private static void runTest(Logger log
, int nbThreads
) {
116 for (int i
= 0; i
< NB_RUNS
; i
++) {
117 Runner runner
= new Runner(nbThreads
, NB_ITER
, log
);
119 long start
= System
.nanoTime();
121 long end
= System
.nanoTime();
123 total
+= (end
- start
);
125 long average
= (total
/ NB_RUNS
);
126 System
.out
.println(nbThreads
+ " threads, average = " + average
/ NB_ITER
+ " ns/loop");
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
133 private static class Runner
implements Runnable
{
135 private final List
<Worker
> workers
= new LinkedList
<>();
136 private final List
<Thread
> workerThreads
= new LinkedList
<>();
138 public Runner(int nbThreads
, int nbIter
, Logger log
) {
140 for (int id
= 0; id
< nbThreads
; id
++) {
141 Worker curWorker
= new Worker(id
, nbIter
, log
);
142 workers
.add(curWorker
);
143 workerThreads
.add(new Thread(curWorker
, "worker " + id
));
149 workerThreads
.forEach(Thread
::start
);
151 workerThreads
.forEach(t
-> {
154 } catch (InterruptedException e
) {
160 private static class Worker
implements Runnable
{
162 private final Logger log
;
163 private final int threadId
;
164 private final int nbIter
;
166 @SuppressWarnings("unused")
167 private volatile int value
= 0;
169 public Worker(int threadId
, int nbIter
, Logger log
) {
171 this.threadId
= threadId
;
172 this.nbIter
= nbIter
;
177 for (int i
= 0; i
< nbIter
; i
++) {
180 log
.info("Thread " + threadId
+ ", iteration " + i
);
This page took 0.034781 seconds and 5 git commands to generate.