EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.test]

COVERAGE SUMMARY FOR SOURCE FILE [Benchmark.java]

nameclass, %method, %block, %line, %
Benchmark.java100% (5/5)68%  (19/28)78%  (488/628)75%  (89.6/120)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Benchmark100% (1/1)55%  (11/20)73%  (381/521)71%  (72.6/103)
getAverageLatency (): double 0%   (0/1)0%   (0/33)0%   (0/6)
getNumberOfJobs (): int 0%   (0/1)0%   (0/3)0%   (0/1)
getNumberOfSamples (): int 0%   (0/1)0%   (0/3)0%   (0/1)
getNumberOfThreads (): int 0%   (0/1)0%   (0/3)0%   (0/1)
getRuntimeMS (): int 0%   (0/1)0%   (0/3)0%   (0/1)
isDisplaySample (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
setDisplaySample (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setNumberOfJobs (int): void 0%   (0/1)0%   (0/4)0%   (0/2)
throwAssertionError (Throwable): void 0%   (0/1)0%   (0/11)0%   (0/3)
run (): void 100% (1/1)58%  (85/146)55%  (11.6/21)
runTests (ScheduledExecutorService, ExecutorService, int, int): double 100% (1/1)93%  (128/138)92%  (24/26)
getStdDev (): double 100% (1/1)97%  (62/64)92%  (11/12)
Benchmark (BenchmarkCase): void 100% (1/1)100% (26/26)100% (9/9)
Benchmark (Runnable): void 100% (1/1)100% (30/30)100% (9/9)
access$000 (Benchmark): BenchmarkCase 100% (1/1)100% (3/3)100% (1/1)
getAverageRate (): double 100% (1/1)100% (31/31)100% (6/6)
isToRun (): boolean 100% (1/1)100% (4/4)100% (1/1)
setNumberOfSamples (int): void 100% (1/1)100% (4/4)100% (2/2)
setNumberOfThreads (int): void 100% (1/1)100% (4/4)100% (2/2)
setRuntimeMS (int): void 100% (1/1)100% (4/4)100% (2/2)
     
class Benchmark$1100% (1/1)100% (2/2)100% (20/20)100% (5/5)
Benchmark$1 (Benchmark, Runnable): void 100% (1/1)100% (9/9)100% (1/1)
doTest (int, AtomicBoolean, AtomicInteger): void 100% (1/1)100% (11/11)100% (4/4)
     
class Benchmark$2100% (1/1)100% (2/2)100% (42/42)100% (5/5)
Benchmark$2 (Benchmark, int, AtomicBoolean, AtomicInteger, AtomicInteger): void 100% (1/1)100% (18/18)100% (1/1)
call (): Void 100% (1/1)100% (24/24)100% (4/4)
     
class Benchmark$3100% (1/1)100% (2/2)100% (14/14)100% (3/3)
Benchmark$3 (Benchmark, AtomicBoolean): void 100% (1/1)100% (9/9)100% (1/1)
run (): void 100% (1/1)100% (5/5)100% (2/2)
     
class Benchmark$4100% (1/1)100% (2/2)100% (31/31)100% (4/4)
Benchmark$4 (Benchmark, Future []): void 100% (1/1)100% (9/9)100% (1/1)
run (): void 100% (1/1)100% (22/22)100% (3/3)

1package org.jtoolkit.essence.test;
2 
3import org.jetbrains.annotations.Nullable;
4import org.jtoolkit.essence.concurrency.Threads;
5 
6import java.util.ArrayList;
7import java.util.List;
8import java.util.concurrent.*;
9import java.util.concurrent.atomic.AtomicBoolean;
10import java.util.concurrent.atomic.AtomicInteger;
11 
12public class Benchmark {
13    public static final int UNSET = -1;
14 
15    private final BenchmarkCase benchmarkCase;
16    private final List<Double> rates = new ArrayList<Double>();
17 
18    private int runtimeMS = 2500;
19    private int numberOfThreads = UNSET;
20    private int numberOfJobs = UNSET;
21    private int numberOfSamples = 1;
22    private boolean displaySample = false;
23 
24    public Benchmark(BenchmarkCase benchmarkCase) {
25        this.benchmarkCase = benchmarkCase;
26    }
27 
28    public Benchmark(final Runnable runnable) {
29        benchmarkCase = new BenchmarkCase() {
30            public void doTest(int run, AtomicBoolean running, AtomicInteger runCount) throws Exception {
31                while (running.get()) {
32                    runnable.run();
33                    runCount.incrementAndGet();
34                }
35            }
36        };
37    }
38 
39    public boolean isDisplaySample() {
40        return displaySample;
41    }
42 
43    public void setDisplaySample(boolean displaySample) {
44        this.displaySample = displaySample;
45    }
46 
47    public boolean isToRun() {
48        return rates.isEmpty();
49    }
50 
51    public int getNumberOfSamples() {
52        return numberOfSamples;
53    }
54 
55    public void setNumberOfSamples(int numberOfSamples) {
56        this.numberOfSamples = numberOfSamples;
57    }
58 
59    public int getNumberOfJobs() {
60        return numberOfJobs;
61    }
62 
63    public void setNumberOfJobs(int numberOfJobs) {
64        this.numberOfJobs = numberOfJobs;
65    }
66 
67    public int getNumberOfThreads() {
68        return numberOfThreads;
69    }
70 
71    public void setNumberOfThreads(int numberOfThreads) {
72        this.numberOfThreads = numberOfThreads;
73    }
74 
75    public int getRuntimeMS() {
76        return runtimeMS;
77    }
78 
79    public void setRuntimeMS(int runtimeMS) {
80        this.runtimeMS = runtimeMS;
81    }
82 
83    public void run() {
84        int nThreads = numberOfThreads >= 1 ? numberOfThreads : 1;
85        int jobs = numberOfJobs < nThreads ? nThreads : numberOfJobs;
86 
87        ScheduledExecutorService timeout = Executors.newSingleThreadScheduledExecutor();
88        ExecutorService executor = Executors.newFixedThreadPool(nThreads);
89 
90        // use the first sample to warm up.
91        int numberOfSamples = this.numberOfSamples > 1 ? this.numberOfSamples + 1 : 1;
92        int runtimeMS = (this.runtimeMS >= 1 ? this.runtimeMS : 2500) * nThreads / jobs / numberOfSamples;
93        try {
94            for (int s = 0; s < numberOfSamples; s++) {
95                double rate = runTests(timeout, executor, jobs, runtimeMS);
96                if (rate > 0)
97                    rates.add(rate);
98            }
99        } catch (RuntimeException e) {
100            throw e;
101        } catch (ExecutionException e) {
102            throwAssertionError(e.getCause());
103        } catch (Exception e) {
104            throwAssertionError(e);
105        } finally {
106            Threads.shutdown(timeout);
107            Threads.shutdown(executor);
108 
109            if (rates.size() > 1)
110                rates.remove(0);
111        }
112    }
113 
114    private static void throwAssertionError(Throwable e) {
115        AssertionError error = new AssertionError("Exception thrown running test");
116        error.initCause(e);
117        throw error;
118    }
119 
120    public double getAverageRate() {
121        if (isToRun())
122            run();
123        double total = 0;
124        for (double rate : rates)
125            total += rate;
126        return total / rates.size();
127    }
128 
129    public double getAverageLatency() {
130        if (isToRun())
131            run();
132        double total = 0;
133        for (double rate : rates)
134            total += 1 / rate;
135        return total / rates.size();
136    }
137 
138    public double getStdDev() {
139        if (isToRun())
140            run();
141        double total = 0;
142        for (double rate : rates)
143            total += rate;
144        double average = total / rates.size();
145        double totalDiffSqu = 0;
146        for (double rate : rates) {
147            double diff = rate - average;
148            totalDiffSqu += diff * diff;
149        }
150        return Math.sqrt(totalDiffSqu) / rates.size();
151    }
152 
153    private double runTests(ScheduledExecutorService timeout, ExecutorService executor, int jobs, final int runtimeMS) throws InterruptedException, ExecutionException, TimeoutException {
154        final AtomicBoolean running = new AtomicBoolean(true);
155        final AtomicInteger runCount = new AtomicInteger();
156        final AtomicInteger finishedCount = new AtomicInteger();
157        final Future[] futures = new Future[jobs];
158 
159        final long start = System.nanoTime();
160 
161        for (int j = 0; j < jobs; j++) {
162            final int j2 = j;
163            futures[j] = executor.submit(new Callable<Void>() {
164                @Nullable public Void call() throws Exception {
165                    try {
166                        benchmarkCase.doTest(j2, running, runCount);
167                    } finally {
168                        finishedCount.incrementAndGet();
169                    }
170                    return null;
171                }
172            });
173        }
174 
175        // add a timeout.
176        Future timeoutFuture = timeout.schedule(new Runnable() {
177            public void run() {
178//                System.out.println("Finishing run time " + runtimeMS);
179                running.set(false);
180            }
181        }, runtimeMS, TimeUnit.MILLISECONDS);
182 
183        Future maxTimeoutFuture = timeout.schedule(new Runnable() {
184            public void run() {
185                for (Future future : futures)
186                    future.cancel(true);
187            }
188        }, runtimeMS, TimeUnit.MILLISECONDS);
189 
190        // wait for tests to finish, or wait the timeout.
191        while (!timeoutFuture.isDone() && finishedCount.get() < jobs)
192            try {
193                timeoutFuture.get(100, TimeUnit.MILLISECONDS);
194            } catch (TimeoutException ignored) {
195                // ignored.
196            }
197//        System.out.println("Finished tests");
198        running.set(false);
199        // noinspection CallToThreadYield
200        Thread.yield();
201 
202        if (timeoutFuture != null) timeoutFuture.cancel(true);
203        maxTimeoutFuture.cancel(true);
204        // check for exceptions thrown.
205        for (Future future : futures)
206            try {
207                if (!future.isCancelled() && !future.isDone()) {
208                    future.cancel(true);
209                    future.get(runtimeMS, TimeUnit.MILLISECONDS);
210                }
211            } catch (CancellationException ignored) {
212                // ignored
213            }
214 
215        long time = System.nanoTime() - start;
216        return runCount.get() * 1.0e9 / time;
217    }
218}

[all classes][org.jtoolkit.essence.test]
EMMA 2.0.5312 (C) Vladimir Roubtsov