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