Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.micro.bench;
- import java.util.*;
- public class Timer {
- private static final String ZEROES = "000000000000";
- private static final String BLANKS = " ";
- private long startTime = 0;
- private String msg = null;
- private Map<String, Long> currentMeasure = new LinkedHashMap<String, Long>();
- private Map<String, List<Long>> pastMeasures = new LinkedHashMap<String, List<Long>>();
- public void start(String msg) {
- if (startTime != 0) {
- throw new IllegalStateException("Already started");
- }
- startTime = System.currentTimeMillis();
- this.msg = msg;
- }
- public void stop() {
- long now = System.currentTimeMillis();
- Long value = currentMeasure.get(msg);
- if (value == null) {
- value = 0L;
- }
- value += now - startTime;
- currentMeasure.put(msg, value);
- startTime = 0;
- msg = null;
- }
- public void flush() {
- for (String event : currentMeasure.keySet()) {
- List<Long> list = pastMeasures.get(event);
- if (list == null) {
- list = new ArrayList<Long>();
- pastMeasures.put(event, list);
- }
- list.add(currentMeasure.get(event));
- currentMeasure.put(event, 0L);
- }
- }
- public String toString() {
- StringBuilder sb = new StringBuilder("Statistics: \n");
- sb.append(" total|")
- .append(" amount|")
- .append(" last|")
- .append(" last 5|")
- .append(" last 10|")
- .append(" avg|")
- .append(" dev|")
- .append(" operation\n");
- for (String event : pastMeasures.keySet()) {
- List<Long> data = pastMeasures.get(event);
- sb.append(format(getSummary(data))).append("|");
- sb.append(format(data.size())).append("|");
- sb.append(format(data.get(data.size() - 1))).append("|");
- sb.append(format(getAverage(data, 5))).append("|");
- sb.append(format(getAverage(data, 10))).append("|");
- sb.append(format(getAverage(data))).append("|");
- sb.append(format(getStandardDeviation(data))).append("| .. ");
- sb.append(event).append("\n");
- }
- return sb.toString();
- }
- private static String format(double num) {
- return format(num, 2, 10);
- }
- private static String format(double val, int n, int w) {
- if (Double.isNaN(val)) {
- return BLANKS.substring(0, w - "NaN".length()) + "NaN";
- }
- // rounding
- double incr = 0.5;
- for (int j = n; j > 0; j--) {
- incr /= 10;
- }
- val += incr;
- String s = Double.toString(val);
- int n1 = s.indexOf('.');
- int n2 = s.length() - n1 - 1;
- if (n > n2) {
- s = s + ZEROES.substring(0, n - n2);
- } else if (n2 > n) {
- s = s.substring(0, n1 + n + 1);
- }
- if (w > 0 & w > s.length()) {
- s = BLANKS.substring(0, w - s.length()) + s;
- } else if (w < 0 & (-w) > s.length()) {
- w = -w;
- s = s + BLANKS.substring(0, w - s.length());
- }
- return s;
- }
- private static long getSummary(List<Long> data, int last) {
- long sum = 0L;
- int start = Math.max(0, data.size() - last);
- for (int i = start; i < data.size(); i++) {
- sum += data.get(i);
- }
- return sum;
- }
- private static long getSummary(List<Long> data) {
- long sum = 0L;
- for (Long item : data) {
- sum = sum + item;
- }
- return sum;
- }
- private static double getAverage(List<Long> data, int last) {
- return getSummary(data, last) / last;
- }
- private static double getAverage(List<Long> data) {
- return (double) getSummary(data) / data.size();
- }
- private static double getStandardDeviation(List<Long> data) {
- double avg = getAverage(data);
- double stdev = 0L;
- for (Long v : data) {
- stdev += Math.pow(avg - v, 2);
- }
- return Math.sqrt(stdev) / (data.size() - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement