Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package es.capgemini.audit;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- public class Timer2 {
- private static List<TimerEntry> timeEntries = new ArrayList<TimerEntry>();
- private static HashMap<String, TimerEntry> teMap = new HashMap<String, TimerEntry>();
- private static TimerEntry lastStartedTimer = null;
- private Timer2() {}
- public static void start(String tag) {
- TimerEntry te = new TimerEntry(tag);
- if (lastStartedTimer != null) {
- lastStartedTimer.addChild(te);
- } else {
- timeEntries.add(te);
- }
- lastStartedTimer = te;
- teMap.put(tag, te);
- System.out.println("TIMER START " + te.toString());
- }
- public static void stop(String tag) {
- if (teMap.containsKey(tag)) {
- TimerEntry te = teMap.get(tag);
- if (!te.isStopped()) {
- te.stop();
- lastStartedTimer = te.getRunningParent();
- System.out.println("TIMER STOP " + te.toString());
- } else {
- System.err.println("TIMER ERROR - Timar ya habia finalizado. " + tag);
- }
- } else {
- System.err.println(" TIMER ERROR - no hay timer " + tag);
- }
- }
- public static void printTimes() {
- StringBuilder sb = new StringBuilder("\nTIMER: ======================================");
- timeEntries.forEach((te) -> {
- sb.append("\n" + te.toString());
- });
- System.out.println(sb.toString());
- }
- private static class TimerValue {
- Long start;
- Long end;
- public boolean isEnded() {
- return this.end != null;
- }
- public long getDuration() {
- if (isEnded()) {
- return end - start;
- } else {
- return System.currentTimeMillis() - start;
- }
- }
- @Override
- public String toString() {
- return getDuration() + "ms" + ((!isEnded()) ? " (running)" : "");
- }
- }
- private static class TimerEntry {
- TimerValue value;
- List<TimerEntry> children = new ArrayList<TimerEntry>();
- TimerEntry parent;
- String tag;
- public TimerEntry(String tag) {
- this.value = new TimerValue();
- this.value.start = System.currentTimeMillis();
- this.tag = tag;
- }
- public void setParent(TimerEntry te) {
- this.parent = te;
- }
- public void addChild(TimerEntry te) {
- this.children.add(te);
- te.parent = this;
- }
- public void stop() {
- if (!this.value.isEnded()) {
- this.value.end = System.currentTimeMillis();
- } else {
- System.err.println("TIMER: ERROR [" + tag + "] ya se había terminado antes.");
- }
- }
- public boolean isStopped() {
- return this.value.isEnded();
- }
- public TimerEntry getRunningParent() {
- TimerEntry result = null;
- TimerEntry p = this.parent;
- while (p != null) {
- if (!p.isStopped()) {
- result = p;
- break;
- }
- p = p.parent;
- }
- return result;
- }
- public int getDepth() {
- int result = 0;
- TimerEntry p = this.parent;
- while (p != null) {
- result++;
- p = p.parent;
- }
- return result;
- }
- private static String inBrackets(String text) {
- return "[" + fixedLen(text,10) + "]";
- }
- private static String fixedLen(String text, int len) {
- String result = text;
- while (result.length() < len) result+=" ";
- if (result.length() > len) {
- result = result.substring(0, len - 3) + "...";
- }
- return result;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- String offset = "";
- for (int i = 0; i < this.getDepth(); i++) offset += " ";
- sb.append(offset + inBrackets(tag));
- if (children.size() > 0) {
- sb.append("\n");
- children.forEach((child) -> {
- sb.append(child.toString());
- });
- sb.append(offset + inBrackets("/" + tag) + " -> " + value.toString() + "\n" );
- } else {
- sb.append("-> " + value.toString() + "\n");
- }
- return sb.toString();
- }
- }
- public static void main(String[] args) {
- Timer2.start("aplicacion");
- sleep(100);
- Timer2.start("paso1");sleep(50);
- Timer2.stop("paso1");
- Timer2.start("paso2");
- Timer2.start("paso 2.1");
- Timer2.start("paso 2.1.1");sleep(50);
- Timer2.stop("paso 2.1.1");
- Timer2.stop("paso 2.1"); sleep(50);
- Timer2.start("paso 2.2"); sleep(50);
- Timer2.stop("paso 2.2");
- Timer2.stop("paso2");sleep(50);
- Timer2.start("paso3");sleep(50);
- Timer2.stop("paso3");
- Timer2.stop("aplicacion");
- Timer2.printTimes();
- }
- private static void sleep(int time) {
- try {
- Thread.currentThread().sleep(time);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement