CostStatistics.java
991 Bytes
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
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();
}
}