| package org.eclipse.osbp.xtext.oxtype.resource; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class DerivedStateMetric { |
| |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(DerivedStateMetric.class); |
| |
| Map<String, Data> cache = new HashMap<>(); |
| |
| static final DerivedStateMetric INSTANCE = new DerivedStateMetric(); |
| |
| public static DerivedStateMetric getInstance() { |
| return INSTANCE; |
| } |
| |
| private DerivedStateMetric() { |
| |
| } |
| |
| public void addMetric(String key, long timeExecuteState) { |
| Data data = cache.get(key); |
| if (data == null) { |
| data = new Data(key); |
| cache.put(key, data); |
| } |
| |
| data.add(timeExecuteState); |
| } |
| |
| public void print() { |
| List<Map.Entry<String, Data>> values = new ArrayList<>(cache.entrySet()); |
| Collections.sort(values, |
| (o1, o2) -> { |
| return (int) (o2.getValue().timeExecuteStateSum - o1 |
| .getValue().timeExecuteStateSum); |
| }); |
| |
| for (Map.Entry<String, Data> entry : values) { |
| System.out.println(entry.getValue().toString()); |
| } |
| } |
| |
| public void clear() { |
| cache.clear(); |
| } |
| |
| static class Data { |
| final String key; |
| int count; |
| long timeExecuteStateSum = 0; |
| long avgTime; |
| |
| public Data(String key) { |
| this.key = key; |
| } |
| |
| public void add(long timeExecuteState) { |
| this.count++; |
| this.timeExecuteStateSum += timeExecuteState; |
| this.avgTime = timeExecuteStateSum / count; |
| } |
| |
| @Override |
| public String toString() { |
| return "Data [key=" + key + ", count=" + count |
| + " timeExecuteStateSum=" + timeExecuteStateSum |
| + "ms, avgTime=" + avgTime + "ms]"; |
| } |
| } |
| } |