1 package org
.lttng
.ust
.agent
.jul
.benchmarks
;
3 import java
.util
.LinkedList
;
5 import java
.util
.logging
.Handler
;
6 import java
.util
.logging
.Level
;
7 import java
.util
.logging
.Logger
;
9 import org
.junit
.After
;
10 import org
.junit
.Before
;
11 import org
.junit
.Test
;
13 public abstract class AbstractJulBenchmark
{
15 // ------------------------------------------------------------------------
16 // Configurable test parameters
17 // ------------------------------------------------------------------------
19 /** Nb of runs per test, result will be averaged */
20 private static final int NB_RUNS
= 10;
22 /** Trace/log events per run */
23 private static final int NB_ITER
= 100000;
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};
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
32 private Logger logger
;
33 protected Handler handler
;
35 // ------------------------------------------------------------------------
36 // Maintenance methods
37 // ------------------------------------------------------------------------
41 /* Set up the logger */
42 logger
= Logger
.getLogger("Test logger");
43 logger
.setUseParentHandlers(false);
44 logger
.setLevel(Level
.ALL
);
46 /* Sub-classes' @Before will setup the Handler */
50 public void teardown() {
51 logger
.removeHandler(handler
);
52 if (handler
!= null) {
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 public void runBenchmark() {
65 if (handler
!= null) {
66 logger
.addHandler(handler
);
70 System
.out
.println("Running benchmark: " + this.getClass().getCanonicalName());
71 for (int i
: NB_THREADS
) {
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
);
81 start
= System
.nanoTime();
83 end
= System
.nanoTime();
85 total
+= (end
- start
);
87 average
= total
/ NB_RUNS
;
88 System
.out
.println(nbThreads
+ " threads, average = " + average
/ NB_ITER
+ " ns/event");
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
95 private static class Runner
implements Runnable
{
97 private final List
<Worker
> workers
= new LinkedList
<>();
98 private final List
<Thread
> workerThreads
= new LinkedList
<>();
100 public Runner(int nbThreads
, int nbIter
, Logger log
) {
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
));
111 for (Thread curThread
: workerThreads
) {
115 for (Thread curThread
: workerThreads
) {
118 } catch (InterruptedException e
) {
124 private static class Worker
implements Runnable
{
126 private final Logger log
;
127 private final int threadId
;
128 private final int nbIter
;
130 public Worker(int threadId
, int nbIter
, Logger log
) {
132 this.threadId
= threadId
;
133 this.nbIter
= nbIter
;
138 for (int i
= 0; i
< nbIter
; i
++) {
139 log
.info("Thread " + threadId
+ ", iteration " + i
);
This page took 0.033162 seconds and 5 git commands to generate.