Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package es.capgemini.audit;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Map.Entry;
  8.  
  9. public class Timer2 {
  10.  
  11. private static List<TimerEntry> timeEntries = new ArrayList<TimerEntry>();
  12. private static HashMap<String, TimerEntry> teMap = new HashMap<String, TimerEntry>();
  13. private static TimerEntry lastStartedTimer = null;
  14.  
  15. private Timer2() {}
  16.  
  17. public static void start(String tag) {
  18. TimerEntry te = new TimerEntry(tag);
  19. if (lastStartedTimer != null) {
  20. lastStartedTimer.addChild(te);
  21. } else {
  22. timeEntries.add(te);
  23. }
  24. lastStartedTimer = te;
  25. teMap.put(tag, te);
  26. System.out.println("TIMER START " + te.toString());
  27. }
  28.  
  29. public static void stop(String tag) {
  30. if (teMap.containsKey(tag)) {
  31. TimerEntry te = teMap.get(tag);
  32. if (!te.isStopped()) {
  33. te.stop();
  34. lastStartedTimer = te.getRunningParent();
  35. System.out.println("TIMER STOP " + te.toString());
  36. } else {
  37. System.err.println("TIMER ERROR - Timar ya habia finalizado. " + tag);
  38. }
  39. } else {
  40. System.err.println(" TIMER ERROR - no hay timer " + tag);
  41. }
  42. }
  43.  
  44. public static void printTimes() {
  45. StringBuilder sb = new StringBuilder("\nTIMER: ======================================");
  46. timeEntries.forEach((te) -> {
  47. sb.append("\n" + te.toString());
  48. });
  49. System.out.println(sb.toString());
  50. }
  51.  
  52. private static class TimerValue {
  53.  
  54. Long start;
  55. Long end;
  56.  
  57. public boolean isEnded() {
  58. return this.end != null;
  59. }
  60.  
  61. public long getDuration() {
  62. if (isEnded()) {
  63. return end - start;
  64. } else {
  65. return System.currentTimeMillis() - start;
  66. }
  67. }
  68.  
  69. @Override
  70. public String toString() {
  71. return getDuration() + "ms" + ((!isEnded()) ? " (running)" : "");
  72. }
  73.  
  74. }
  75.  
  76. private static class TimerEntry {
  77.  
  78. TimerValue value;
  79. List<TimerEntry> children = new ArrayList<TimerEntry>();
  80. TimerEntry parent;
  81.  
  82. String tag;
  83.  
  84. public TimerEntry(String tag) {
  85. this.value = new TimerValue();
  86. this.value.start = System.currentTimeMillis();
  87. this.tag = tag;
  88. }
  89.  
  90. public void setParent(TimerEntry te) {
  91. this.parent = te;
  92. }
  93.  
  94. public void addChild(TimerEntry te) {
  95. this.children.add(te);
  96. te.parent = this;
  97. }
  98.  
  99. public void stop() {
  100. if (!this.value.isEnded()) {
  101. this.value.end = System.currentTimeMillis();
  102. } else {
  103. System.err.println("TIMER: ERROR [" + tag + "] ya se había terminado antes.");
  104. }
  105. }
  106.  
  107. public boolean isStopped() {
  108. return this.value.isEnded();
  109. }
  110.  
  111. public TimerEntry getRunningParent() {
  112. TimerEntry result = null;
  113. TimerEntry p = this.parent;
  114. while (p != null) {
  115. if (!p.isStopped()) {
  116. result = p;
  117. break;
  118. }
  119. p = p.parent;
  120. }
  121. return result;
  122. }
  123.  
  124. public int getDepth() {
  125. int result = 0;
  126. TimerEntry p = this.parent;
  127. while (p != null) {
  128. result++;
  129. p = p.parent;
  130. }
  131. return result;
  132. }
  133.  
  134. private static String inBrackets(String text) {
  135. return "[" + fixedLen(text,10) + "]";
  136. }
  137.  
  138. private static String fixedLen(String text, int len) {
  139. String result = text;
  140. while (result.length() < len) result+=" ";
  141. if (result.length() > len) {
  142. result = result.substring(0, len - 3) + "...";
  143. }
  144. return result;
  145. }
  146.  
  147. @Override
  148. public String toString() {
  149. StringBuilder sb = new StringBuilder();
  150. String offset = "";
  151. for (int i = 0; i < this.getDepth(); i++) offset += " ";
  152. sb.append(offset + inBrackets(tag));
  153. if (children.size() > 0) {
  154. sb.append("\n");
  155. children.forEach((child) -> {
  156. sb.append(child.toString());
  157. });
  158. sb.append(offset + inBrackets("/" + tag) + " -> " + value.toString() + "\n" );
  159. } else {
  160. sb.append("-> " + value.toString() + "\n");
  161. }
  162. return sb.toString();
  163. }
  164. }
  165.  
  166. public static void main(String[] args) {
  167.  
  168. Timer2.start("aplicacion");
  169. sleep(100);
  170.  
  171. Timer2.start("paso1");sleep(50);
  172. Timer2.stop("paso1");
  173.  
  174. Timer2.start("paso2");
  175.  
  176. Timer2.start("paso 2.1");
  177. Timer2.start("paso 2.1.1");sleep(50);
  178. Timer2.stop("paso 2.1.1");
  179. Timer2.stop("paso 2.1"); sleep(50);
  180.  
  181. Timer2.start("paso 2.2"); sleep(50);
  182. Timer2.stop("paso 2.2");
  183.  
  184. Timer2.stop("paso2");sleep(50);
  185.  
  186. Timer2.start("paso3");sleep(50);
  187. Timer2.stop("paso3");
  188.  
  189. Timer2.stop("aplicacion");
  190. Timer2.printTimes();
  191. }
  192.  
  193. private static void sleep(int time) {
  194. try {
  195. Thread.currentThread().sleep(time);
  196. } catch (InterruptedException e) {
  197. e.printStackTrace();
  198. }
  199. }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement