Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jul 25, 2025. It is now read-only.
forked from strongback/strongback-java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutableTimer.java
More file actions
Latest commit
141 lines (127 loc) · 5.95 KB
/
Copy pathExecutableTimer.java
File metadata and controls
141 lines (127 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Strongback
* Copyright 2015, Strongback and individual contributors by the @authors tag.
* See the COPYRIGHT.txt in the distribution for a full listing of individual
* contributors.
*
* Licensed under the MIT License; you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
packageorg.strongback;
importjava.util.concurrent.CountDownLatch;
importjava.util.concurrent.TimeUnit;
importjava.util.concurrent.atomic.AtomicLong;
importjava.util.function.Consumer;
importorg.strongback.Executor.Priority;
importorg.strongback.Strongback.Configurator;
importorg.strongback.annotation.ThreadSafe;
importcom.codahale.metrics.Histogram;
importcom.codahale.metrics.Snapshot;
importcom.codahale.metrics.UniformReservoir;
/**
* A command that measures the {@link Strongback.Configurator#useExecutionPeriod(long, java.util.concurrent.TimeUnit) execution
* rate} of the Strongback {@link Executor}. This is purely for test purposes.
* <p>
*
* @author Randall Hauch
*/
@ThreadSafe
publicfinalclassExecutableTimerimplementsExecutable {
/**
* Measure the timing of the {@link Executor} and print the histogram of timing results to {@link System#out}. The resulting
* timer will be {@link Executor#register(Executable) registered} with the Executor, and upon completion will automatically
* unregister itself.
* <p>
* This method can be used to print a histogram of {@link Strongback#executor() Strongback's executor}. The following
* example uses 1000 samples, which corresponds to 5 seconds if the {@link Configurator#useExecutionPeriod(long, TimeUnit)
* execution rate} is 5 milliseconds:
*
* <pre>
* ExecutableTimer.measureTimingAndPrint(Strongback.executor(), 200 * 5);
* </pre>
*
* The result would be something like the following:
*
* @param executor the Executor to measure; may not be null
* @param desc the description
* @param numberOfSamples the number of samples to measure
* @return the executable timer
*/
publicstaticExecutableTimermeasureTimingAndPrint(Executorexecutor, Stringdesc, intnumberOfSamples) {
returnmeasureTiming(executor, numberOfSamples, snapshot -> {
System.out.println("Execution timing statistics" + (desc != null ? " (" + desc + ")" : ""));
System.out.println(" Size of histogram: " + snapshot.getValues().length);
System.out.println(" Minimum (ms): " + snapshot.getMin());
System.out.println(" Maximum (ms): " + snapshot.getMax());
System.out.println(" Mean (ms): " + snapshot.getMean());
System.out.println(" Median (ms): " + snapshot.getMedian());
System.out.println(" Standard dev (ms): " + snapshot.getStdDev());
System.out.println(" 75th percentile (ms): " + snapshot.get75thPercentile());
System.out.println(" 95th percentile (ms): " + snapshot.get95thPercentile());
System.out.println(" 98th percentile (ms): " + snapshot.get98thPercentile());
System.out.println(" 99th percentile (ms): " + snapshot.get99thPercentile());
System.out.println(" 99.9th percentile (ms): " + snapshot.get999thPercentile());
//snapshot.dump(System.out);
});
}
publicstaticExecutableTimermeasureTiming(Executorexecutor, intnumberOfSamples, Consumer<Snapshot> atCompletion) {
ExecutableTimertimer = newExecutableTimer(numberOfSamples, (theTimer) -> {
executor.unregister(theTimer);
if (atCompletion != null) atCompletion.accept(theTimer.getResults());
});
// Register the timer with the executor ...
executor.register(timer, Priority.HIGH);
returntimer;
}
privatefinalCountDownLatchlatch = newCountDownLatch(1);
privatefinalConsumer<ExecutableTimer> resultsHandler;
privatefinalHistogramhistogram;
privatefinalintnumSamples;
privatefinalAtomicLonglastStartTime = newAtomicLong();
privatefinalAtomicLongcount = newAtomicLong(-1);
/**
* Create an executable timer that measures the specified number of samples.
*
* @param numberOfSamples the maximum number of samples to take; must be positive
* @param uponCompletion the function that will be called
*/
privateExecutableTimer(intnumberOfSamples, Consumer<ExecutableTimer> uponCompletion) {
histogram = newHistogram(newUniformReservoir(numberOfSamples));
numSamples = numberOfSamples;
resultsHandler = uponCompletion;
}
@Override
publicvoidexecute(longtimeInMillis) {
longcurrentCount = count.getAndIncrement();
if (currentCount < 0) {
lastStartTime.set(timeInMillis);
} elseif (currentCount < numSamples) {
histogram.update(timeInMillis - lastStartTime.getAndSet(timeInMillis));
} else {
count.set(-1);
// We've completed, so first run the results handler and only then release the latch ...
resultsHandler.accept(this);
latch.countDown();
}
}
publicbooleanisComplete() {
returnlatch.getCount() == -1 && histogram.getCount() > 0;
}
publicExecutableTimerawait(longtimeout, TimeUnitunit) throwsInterruptedException {
latch.await(timeout, unit);
returnthis;
}
publicExecutableTimerawait() throwsInterruptedException {
latch.await();
returnthis;
}
publicSnapshotgetResults() {
returnhistogram.getSnapshot();
}
}