Created
February 26, 2014 21:40
-
-
Save aleemstreak/9239200 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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