planetiler/flatmap-core/src/main/java/com/onthegomap/flatmap/stats/Timers.java

39 wiersze
984 B
Java
Czysty Zwykły widok Historia

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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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() {
LOGGER.info("-".repeat(50));
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) {
Timer timer = new Timer().start();
timers.put(name, timer);
2021-08-10 10:55:30 +00:00
LOGGER.info("Starting...");
return () -> LOGGER.info("Finished in " + timers.get(name).stop() + "\n");
2021-06-04 11:22:40 +00:00
}
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-06-04 11:22:40 +00:00
public interface Finishable {
void stop();
}
}