1 18 19 package org.objectweb.speedo.generation.start; 20 21 import java.util.HashMap ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Comparator ; 25 26 import java.text.DecimalFormat ; 27 28 import java.io.PrintWriter ; 29 30 31 34 public final class Timer 35 { 36 static private class MethodDescriptor 38 { 39 final String name; 40 int instantiations; 41 int calls; 42 long self; 43 long total; 44 45 MethodDescriptor(String name) 46 { 47 this.name = name; 48 } 49 } 50 51 static private class MethodCall 53 { 54 final MethodDescriptor method; 55 final String message; 56 long self; 57 long total; 58 59 MethodCall(MethodDescriptor method, 60 String message, 61 long self, 62 long total) 63 { 64 this.method = method; 65 this.message = message; 66 this.self = self; 67 this.total = total; 68 } 69 } 70 71 PrintWriter out = new PrintWriter (System.out, true); 73 74 HashMap methods = new HashMap (); 76 77 private final ArrayList calls = new ArrayList (16); 79 80 public Timer() 81 { 82 } 83 84 public Timer(PrintWriter out) 85 { 86 this.out = out; 87 } 88 89 public final synchronized void push(String name) 90 { 91 push(name, name); 92 } 93 94 public final synchronized void push(String name, String message) 95 { 96 final long now = System.currentTimeMillis(); 98 99 MethodDescriptor current = (MethodDescriptor)methods.get(name); 101 if (current == null) { 102 current = new MethodDescriptor(name); 103 methods.put(name, current); 104 } 105 106 current.calls++; 108 current.instantiations++; 109 110 calls.add(new MethodCall(current, message, now, now)); 112 } 113 114 public final synchronized void pop() 115 { 116 final long now = System.currentTimeMillis(); 118 119 final MethodCall call = (MethodCall)calls.remove(calls.size()-1); 121 122 final long currentSelf = now - call.self; 124 final long currentTotal = now - call.total; 125 126 if (calls.size() > 0) { 128 final MethodCall previous = (MethodCall)calls.get(calls.size()-1); 129 previous.self += currentTotal; 130 } 131 132 final MethodDescriptor current = call.method; 134 current.self += currentSelf; 135 if (--current.instantiations == 0) { 136 current.total += currentTotal; 137 } 138 139 if (false) { 140 out.println("Timer (n,g): " + call.message + " : (" 141 + currentSelf + ", " + currentTotal + ")"); 142 } 143 } 144 145 static private final String pad(String s, int i) 146 { 147 StringBuffer b = new StringBuffer (); 148 for (i -= s.length(); i > 0; i--) 149 b.append((char)' '); 150 b.append(s); 151 return b.toString(); 152 } 153 154 public final synchronized void print() 155 { 156 out.println("Timer : printing accumulated times ..."); 157 final Object [] _calls = methods.values().toArray(); 158 159 Arrays.sort(_calls, 160 new Comparator () { 161 public int compare(Object o1, 162 Object o2) { 163 return (int)(((MethodDescriptor)o2).total 164 - ((MethodDescriptor)o1).total); 165 } 166 public boolean equals(Object obj) { 167 return (compare(this, obj) == 0); 168 } 169 }); 170 171 out.println("Timer : total s self s #calls name"); 172 DecimalFormat nf = new DecimalFormat (); 173 nf.setMaximumFractionDigits(2); 174 nf.setMinimumFractionDigits(2); 175 for (int i = 0; i < _calls.length; i++) { 178 final MethodDescriptor current = (MethodDescriptor)_calls[i]; 179 180 out.println("Timer : " 181 + pad(nf.format(current.total / 1000.0), 8) + " " 182 + pad(nf.format(current.self / 1000.0), 8) + " " 183 + pad(String.valueOf(current.calls), 6) + " " 184 + current.name); 185 } 186 } 187 } 188 | Popular Tags |