2021-08-06 09:56:24 +00:00
|
|
|
package com.onthegomap.flatmap.stats;
|
2021-06-04 11:22:40 +00:00
|
|
|
|
2021-06-05 12:02:51 +00:00
|
|
|
import java.util.Collections;
|
2021-06-04 11:22:40 +00:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
2021-09-10 00:46:20 +00:00
|
|
|
import javax.annotation.concurrent.ThreadSafe;
|
2021-06-04 11:22:40 +00:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-09-10 00:46:20 +00:00
|
|
|
/**
|
|
|
|
* A registry of tasks that are being timed.
|
|
|
|
*/
|
|
|
|
@ThreadSafe
|
2021-06-04 11:22:40 +00:00
|
|
|
public class Timers {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(Stats.InMemory.class);
|
2021-06-05 12:02:51 +00:00
|
|
|
private final Map<String, Timer> timers = Collections.synchronizedMap(new LinkedHashMap<>());
|
2021-06-04 11:22:40 +00:00
|
|
|
|
|
|
|
public void printSummary() {
|
2021-08-11 12:40:49 +00:00
|
|
|
for (var entry : all().entrySet()) {
|
|
|
|
LOGGER.info("\t" + entry.getKey() + "\t" + entry.getValue().elapsed());
|
2021-06-04 11:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Finishable startTimer(String name) {
|
2021-09-10 00:46:20 +00:00
|
|
|
Timer timer = Timer.start();
|
2021-06-04 11:22:40 +00:00
|
|
|
timers.put(name, timer);
|
2021-08-10 10:55:30 +00:00
|
|
|
LOGGER.info("Starting...");
|
2021-10-20 01:57:47 +00:00
|
|
|
return () -> LOGGER.info("Finished in " + timers.get(name).stop() + System.lineSeparator());
|
2021-06-04 11:22:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:46:20 +00:00
|
|
|
/** Returns a snapshot of all timers currently running. Will not reflect timers that start after it's called. */
|
2021-06-05 12:02:51 +00:00
|
|
|
public Map<String, Timer> all() {
|
2021-07-29 01:47:13 +00:00
|
|
|
synchronized (timers) {
|
|
|
|
return new LinkedHashMap<>(timers);
|
|
|
|
}
|
2021-06-05 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:46:20 +00:00
|
|
|
/** A handle that callers can use to indicate a task has finished. */
|
2021-06-04 11:22:40 +00:00
|
|
|
public interface Finishable {
|
|
|
|
|
|
|
|
void stop();
|
|
|
|
}
|
|
|
|
}
|