1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| public class MetricsCollector {
List<?> list;
// Get a DescriptiveStatistics instance
DescriptiveStatistics stats = new DescriptiveStatistics();
MultiValueMap map = new MultiValueMap();
ConcurrentHashMap<String, Double> m=new ConcurrentHashMap<String, Double> ();
static Map<String, Double> result = new HashMap<String, Double>();
private static MetricsCollector instance = null;
private Timer timer;
private static long delay = 1 * 60 * 1000;
private MetricsCollector()
{
timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
SaveCollection();
CalculStats();
}
}, 0, delay);
}
public static synchronized MetricsCollector getInstance() {
if (null == instance) {
instance = new MetricsCollector();
}
return instance;
}
public void sendAvgRTMetric(String Classe,String method, double avgRTValue) throws ParserConfigurationException, SAXException, IOException, InterruptedException {
System.out.println(" ***--Class Name :"+Classe+"-- Method Name: " + method
+ " -- AVG RT = " + avgRTValue);
map.put(Classe+"."+method,Double.valueOf(avgRTValue));
m.putAll(map);
Iterator<Entry<String, Double>> iterator = m.entrySet().iterator();
System.out.println(" key new value new ");
while (iterator.hasNext()) {
Entry<String, Double> entry = iterator.next();
System.out.println("\t" + entry.getKey() + "\t " + entry.getValue());
}
}
private void SaveCollection() {
result.putAll(m);
map.clear();
m.clear();
Iterator<Entry<String, Double>> iterator = result.entrySet().iterator();
System.out.printf ("%tR\n", new Date());
System.out.println(" key result value result");
while (iterator.hasNext()) {
Entry<String, Double> entry = iterator.next();
System.out.println("\t" + entry.getKey() + "\t " + entry.getValue());
}
}
private void CalculStats()
{
Object[] ArrayKey = result.keySet().toArray();
String[] stringArray = Arrays.copyOf(ArrayKey, ArrayKey.length, String[].class);
Object[] arrayvalue = result.values().toArray() ;
for (int i = 0, n = stringArray.length; i < n; i++) {
System.out.println("methoddd:"+stringArray[i]);
System.out.println("valll:"+ arrayvalue[i]);
stats.addValue(((Double) arrayvalue[i]).doubleValue());
// Compute some statistics
//double mean = stats.getMean();
//double std = stats.getStandardDeviation();
//double median = stats.getPercentile(50);
// System.out.println("MAX:"+d);
}
//clean
result.clear();
}
} |
Partager