package codechicken.nei.util; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import codechicken.core.TaskProfiler; public class AsyncTaskProfiler extends TaskProfiler { public ThreadLocal threadedProfiler = ThreadLocal.withInitial(TaskProfiler::new); public Map times = new ConcurrentHashMap<>(); @Override public void start(String section) { if (section == null) section = ""; threadedProfiler.get().start(section); } @Override public void end() { TaskProfiler profiler = threadedProfiler.get(); if (profiler != null) { profiler.end(); if (profiler.times != null && !profiler.times.isEmpty()) { times.putAll(threadedProfiler.get().times); } } } @Override public List getResults() { ArrayList results = new ArrayList<>(times.size()); long totalTime = times.values().stream().reduce(0L, Long::sum); for (Entry e : times.entrySet()) results.add(new ProfilerResult(e.getKey(), e.getValue(), totalTime)); return results; } @Override public void clear() { if (threadedProfiler.get().currentSection != null) threadedProfiler.get().end(); threadedProfiler.get().clear(); times.clear(); } public void clearCurrent() { if (threadedProfiler.get().currentSection != null) threadedProfiler.get().end(); threadedProfiler.get().clear(); } }