Skip to content

Commit 624fe82

Browse files
committed
util: cleanup SimpleProfiler
- remove redundant PROFILE flag - add method for explicitly querying single (cumulated) profiles - improve JavaDoc (cherry picked from commit 81dcf43)
1 parent f886d81 commit 624fe82

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

commons/util/src/main/java/de/learnlib/util/statistics/SimpleProfiler.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package de.learnlib.util.statistics;
1817

1918
import java.util.Map;
@@ -34,7 +33,6 @@ public final class SimpleProfiler {
3433

3534
private static final Map<String, Counter> CUMULATED = new ConcurrentHashMap<>();
3635
private static final Map<String, Long> PENDING = new ConcurrentHashMap<>();
37-
private static final boolean PROFILE = true;
3836
private static final LearnLogger LOGGER = LearnLogger.getLogger(SimpleProfiler.class.getName());
3937
private static final double MILLISECONDS_PER_SECOND = 1000.0;
4038

@@ -43,48 +41,54 @@ private SimpleProfiler() {
4341
}
4442

4543
/**
46-
* reset internal data.
44+
* Reset internal data.
4745
*/
4846
public static void reset() {
4947
CUMULATED.clear();
5048
PENDING.clear();
5149
}
5250

5351
/**
54-
* start activity.
52+
* Start the timer identified by the given key.
53+
*
54+
* @param name
55+
* The name of the timer to be started.
5556
*/
5657
public static void start(String name) {
57-
if (!PROFILE) {
58-
return;
59-
}
60-
long start = System.currentTimeMillis();
61-
62-
PENDING.put(name, start);
63-
58+
PENDING.put(name, System.currentTimeMillis());
6459
}
6560

6661
/**
67-
* stop activity.
62+
* Stop the timer identified by the given key. After stopping a timer, the time passed from its
63+
* {@link #start(String) initialization} will be added to the cumulated time of the specific timer.
64+
*
65+
* @param name
66+
* The name of the timer to be stopped.
6867
*/
6968
public static void stop(String name) {
70-
if (!PROFILE) {
71-
return;
72-
}
7369
Long start = PENDING.remove(name);
7470
if (start == null) {
7571
return;
7672
}
7773
long duration = System.currentTimeMillis() - start;
78-
Counter sum = CUMULATED.get(name);
79-
if (sum == null) {
80-
sum = new Counter(name, "ms");
81-
}
74+
Counter sum = CUMULATED.computeIfAbsent(name, k -> new Counter(k, "ms"));
8275
sum.increment(duration);
83-
CUMULATED.put(name, sum);
8476
}
8577

8678
/**
87-
* get profiling results as string.
79+
* Return the counter for the cumulated (passed) time of the given timer.
80+
*
81+
* @param name
82+
* The name of the timer to be returned.
83+
*
84+
* @return The counter for tracking the passed milliseconds of the timer
85+
*/
86+
public static Counter cumulated(String name) {
87+
return CUMULATED.get(name);
88+
}
89+
90+
/**
91+
* Get profiling results as string.
8892
*/
8993
@Nonnull
9094
public static String getResults() {
@@ -100,7 +104,7 @@ public static String getResults() {
100104
}
101105

102106
/**
103-
* log results in category PROFILING.
107+
* Log results in category PROFILING.
104108
*/
105109
public static void logResults() {
106110
for (Entry<String, Counter> e : CUMULATED.entrySet()) {

0 commit comments

Comments
 (0)