Switch to use the Lttng-Ust Java Agent API, refactor using JUnit
[lttng-ust-java-tests.git] / src / org / lttng / ust / agent / jul / benchmarks / AbstractJulBenchmark.java
1 package org.lttng.ust.agent.jul.benchmarks;
2
3 import java.util.LinkedList;
4 import java.util.List;
5 import java.util.logging.Handler;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12
13 public abstract class AbstractJulBenchmark {
14
15 // ------------------------------------------------------------------------
16 // Configurable test parameters
17 // ------------------------------------------------------------------------
18
19 /** Nb of runs per test, result will be averaged */
20 private static final int NB_RUNS = 10;
21
22 /** Trace/log events per run */
23 private static final int NB_ITER = 100000;
24
25 /** Which tests to run (for different number of threads) */
26 private static final int[] NB_THREADS = {1, 1, 2, 3, 4, 5, 6, 7, 8};
27
28 // ------------------------------------------------------------------------
29 // Attributes
30 // ------------------------------------------------------------------------
31
32 private Logger logger;
33 protected Handler handler;
34
35 // ------------------------------------------------------------------------
36 // Maintenance methods
37 // ------------------------------------------------------------------------
38
39 @Before
40 public void setup() {
41 /* Set up the logger */
42 logger = Logger.getLogger("Test logger");
43 logger.setUseParentHandlers(false);
44 logger.setLevel(Level.ALL);
45
46 /* Sub-classes' @Before will setup the Handler */
47 }
48
49 @After
50 public void teardown() {
51 logger.removeHandler(handler);
52 if (handler != null) {
53 handler.close();
54 }
55 handler = null;
56 logger = null;
57 }
58
59 // ------------------------------------------------------------------------
60 // Test methods
61 // ------------------------------------------------------------------------
62
63 @Test
64 public void runBenchmark() {
65 if (handler != null) {
66 logger.addHandler(handler);
67 }
68
69 System.out.println();
70 System.out.println("Running benchmark: " + this.getClass().getCanonicalName());
71 for (int i : NB_THREADS) {
72 runTest(logger, i);
73 }
74 }
75
76 private static void runTest(Logger log, int nbThreads) {
77 long start, end, average, total = 0;
78 for (int i = 0; i < NB_RUNS; i++) {
79 Runner runner = new Runner(nbThreads, NB_ITER, log);
80
81 start = System.nanoTime();
82 runner.run();
83 end = System.nanoTime();
84
85 total += (end - start);
86 }
87 average = total / NB_RUNS;
88 System.out.println(nbThreads + " threads, average = " + average / NB_ITER + " ns/event");
89 }
90
91 // ------------------------------------------------------------------------
92 // Helper classes
93 // ------------------------------------------------------------------------
94
95 private static class Runner implements Runnable {
96
97 private final List<Worker> workers = new LinkedList<>();
98 private final List<Thread> workerThreads = new LinkedList<>();
99
100 public Runner(int nbThreads, int nbIter, Logger log) {
101
102 for (int id = 0; id < nbThreads; id++) {
103 Worker curWorker = new Worker(id, nbIter, log);
104 workers.add(curWorker);
105 workerThreads.add(new Thread(curWorker, "worker " + id));
106 }
107 }
108
109 @Override
110 public void run() {
111 for (Thread curThread : workerThreads) {
112 curThread.start();
113 }
114
115 for (Thread curThread : workerThreads) {
116 try {
117 curThread.join();
118 } catch (InterruptedException e) {
119 e.printStackTrace();
120 }
121 }
122 }
123
124 private static class Worker implements Runnable {
125
126 private final Logger log;
127 private final int threadId;
128 private final int nbIter;
129
130 public Worker(int threadId, int nbIter, Logger log) {
131 this.log = log;
132 this.threadId = threadId;
133 this.nbIter = nbIter;
134 }
135
136 @Override
137 public void run() {
138 for (int i = 0; i < nbIter; i++) {
139 log.info("Thread " + threadId + ", iteration " + i);
140 }
141 }
142
143 }
144 }
145 }
This page took 0.033162 seconds and 5 git commands to generate.