CostStatistics.java 991 Bytes
package com.yoho.search.consumer.common;

import java.util.ArrayList;
import java.util.List;

public class CostStatistics {
	
	private long begin;
	
	private List<Long> statistics = new ArrayList<Long>();
	
	private long totalCost = 0;
	
	public CostStatistics(){
		this.begin = System.currentTimeMillis();
	}
	
	public long getCost(){
		long current = System.currentTimeMillis();
		long cost = current - begin;
		totalCost = totalCost + cost;
		begin = current;
		statistics.add(cost);
		return cost;
	}
	
	public long getTotalCost(){
		return totalCost;
	}
	
	public List<Long> getCostStatistics(){
		return this.statistics;
	}
	
	public String getCostStatisticsString(){
		StringBuilder sb = new StringBuilder();
		int i = 1;
		long total = 0;
		for (Long statistic : statistics) {
			sb.append("[step").append(i++).append(" cost ").append(statistic).append("ms]");
			total = total + statistic;
		}
		sb.append("[total cost ").append(total).append("ms]");
		return sb.toString();
	}
	
}