| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.util; |
| 17 | |
|
| 18 | |
import org.apache.commons.logging.Log; |
| 19 | |
import org.apache.commons.logging.LogFactory; |
| 20 | |
|
| 21 | |
public class Timer { |
| 22 | 0 | private static final Log LOG = LogFactory.getLog(Timer.class); |
| 23 | |
|
| 24 | 0 | private static int indent = 0; |
| 25 | |
|
| 26 | |
private long t0; |
| 27 | |
private long t1; |
| 28 | |
private String name; |
| 29 | |
|
| 30 | 0 | public Timer(String name) { |
| 31 | 0 | indent++; |
| 32 | 0 | if (LOG.isDebugEnabled()) { |
| 33 | 0 | LOG.debug(indent() + name + ": started"); |
| 34 | |
} |
| 35 | 0 | this.name = name; |
| 36 | 0 | reset(); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
public long getElapsed() { |
| 40 | 0 | t1 = System.currentTimeMillis(); |
| 41 | 0 | return t1 - t0; |
| 42 | |
} |
| 43 | |
|
| 44 | |
public void reset() { |
| 45 | 0 | t0 = System.currentTimeMillis(); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
public String indent() { |
| 49 | 0 | String whitespace = " "; |
| 50 | |
try { |
| 51 | 0 | return whitespace.substring(whitespace.length() - indent); |
| 52 | |
} |
| 53 | 0 | catch (Exception e) { |
| 54 | 0 | return ""; |
| 55 | |
} |
| 56 | |
} |
| 57 | |
|
| 58 | |
public void log() { |
| 59 | 0 | if (LOG.isDebugEnabled()) { |
| 60 | 0 | long elapsed = getElapsed(); |
| 61 | 0 | LOG.debug(indent() + name + ": " + elapsed + "millisec"); |
| 62 | 0 | indent--; |
| 63 | |
} |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
} |