Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Timer |
|
| 1.8;1.8 |
1 | /* | |
2 | * Copyright 2006-2008 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
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 | } |