Skip to content

Instantly share code, notes, and snippets.

@aleemstreak
Created February 26, 2014 21:40
Show Gist options
  • Save aleemstreak/9239200 to your computer and use it in GitHub Desktop.
Save aleemstreak/9239200 to your computer and use it in GitHub Desktop.
public class AccumulatingProfiler {
public class AccumulatingProfileStep implements Comparable<AccumulatingProfileStep>, Serializable {
private static final long serialVersionUID = 1L;
public String name;
public Long lastStartTime;
public Long accumulatedTime;
public AccumulatingProfileStep(String name) {
this.name = name;
this.lastStartTime = 0L;
this.accumulatedTime = 0L;
}
public int compareTo(AccumulatingProfileStep o) {
return this.accumulatedTime.compareTo(o.accumulatedTime);
}
public String toString() {
return name + ": " + accumulatedTime/(1000*1000) + "ms";
}
}
private Map<String, AccumulatingProfileStep> steps = new HashMap<>();
public void start(String stepName) {
AccumulatingProfileStep ps = this.steps.get(stepName);
if (ps == null) {
ps = new AccumulatingProfileStep(stepName);
this.steps.put(stepName, ps);
}
ps.lastStartTime = System.nanoTime();
}
public void end(String stepName) {
AccumulatingProfileStep ps = this.steps.get(stepName);
if (ps == null) {
return;
}
ps.accumulatedTime += System.nanoTime() - ps.lastStartTime;
ps.lastStartTime = 0L;
}
public Map<String, AccumulatingProfileStep> getSteps() {
return steps;
}
public void setSteps(Map<String, AccumulatingProfileStep> steps) {
this.steps = steps;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment