Detach the test name printing listener after the test is run
[lttng-ust-java-tests.git] / src / test / java / org / lttng / ust / agent / benchmarks / jul / handler / JulHandlerBenchmarkBase.java
... / ...
CommitLineData
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
19package org.lttng.ust.agent.benchmarks.jul.handler;
20
21import java.util.LinkedList;
22import java.util.List;
23import java.util.logging.Handler;
24import java.util.logging.Level;
25import java.util.logging.Logger;
26
27import org.junit.After;
28import org.junit.Before;
29import 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 */
35public 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 (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 start, end, average, total = 0;
109 for (int i = 0; i < NB_RUNS; i++) {
110 Runner runner = new Runner(nbThreads, NB_ITER, log);
111
112 start = System.nanoTime();
113 runner.run();
114 end = System.nanoTime();
115
116 total += (end - start);
117 }
118 average = total / NB_RUNS;
119 System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/event");
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 for (Thread curThread : workerThreads) {
143 curThread.start();
144 }
145
146 for (Thread curThread : workerThreads) {
147 try {
148 curThread.join();
149 } catch (InterruptedException e) {
150 e.printStackTrace();
151 }
152 }
153 }
154
155 private static class Worker implements Runnable {
156
157 private final Logger log;
158 private final int threadId;
159 private final int nbIter;
160
161 public Worker(int threadId, int nbIter, Logger log) {
162 this.log = log;
163 this.threadId = threadId;
164 this.nbIter = nbIter;
165 }
166
167 @Override
168 public void run() {
169 for (int i = 0; i < nbIter; i++) {
170 log.info("Thread " + threadId + ", iteration " + i);
171 }
172 }
173
174 }
175 }
176}
This page took 0.029026 seconds and 4 git commands to generate.