KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > perf > PerformanceMonitor


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util.perf;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import com.vladium.utils.timing.ITimer;
23 import com.vladium.utils.timing.TimerFactory;
24
25 /**
26  * Enables <b>begin ... end</b> style performance monitoring with summarisation
27  * using the <b>performance</b> logging category. It is designed to only incur
28  * a minor cost when performance logging is turned on using the DEBUG logging
29  * mechanism. See base class for details on enabling the <b>performance</b>
30  * logging categories.
31  * <p>
32  * This class is thread safe.
33  * <p>
34  * Usage:
35  * <pre>
36  * private PerformanceMonitor somethingTimer = new PerformanceMonitor("mytest", "doSomething");
37  * ...
38  * ...
39  * private void doSomething()
40  * {
41  * somethingTimer.start();
42  * ...
43  * ...
44  * somethingTimer.stop();
45  * }
46  * </pre>
47  *
48  * @author Derek Hulley
49  */

50 public class PerformanceMonitor extends AbstractPerformanceMonitor
51 {
52     private String JavaDoc methodName;
53     private ThreadLocal JavaDoc<ITimer> threadLocalTimer;
54     private boolean log;
55     
56     /**
57      * @param entityName name of the entity, e.g. a test name or a bean name against which to
58      * log the performance
59      * @param methodName the method for which the performance will be logged
60      */

61     public PerformanceMonitor(String JavaDoc entityName, String JavaDoc methodName)
62     {
63         super(entityName);
64         this.methodName = methodName;
65         this.threadLocalTimer = new ThreadLocal JavaDoc<ITimer>();
66         
67         // check if logging can be eliminated
68
Log methodLogger = LogFactory.getLog("performance." + entityName + "." + methodName);
69         this.log = AbstractPerformanceMonitor.isDebugEnabled() && methodLogger.isDebugEnabled();
70     }
71     
72     /**
73      * Threadsafe method to start the timer.
74      * <p>
75      * The timer is only started if the logging levels are enabled.
76      *
77      * @see #stop()
78      */

79     public void start()
80     {
81         if (!log)
82         {
83             // don't bother timing
84
return;
85         }
86         // overwrite the thread's timer
87
ITimer timer = TimerFactory.newTimer();
88         threadLocalTimer.set(timer);
89         // start the timer
90
timer.start();
91     }
92     
93     /**
94      * Threadsafe method to stop the timer.
95      *
96      * @see #start()
97      */

98     public void stop()
99     {
100         if (!log)
101         {
102             // don't bother timing
103
return;
104         }
105         // get the thread's timer
106
ITimer timer = threadLocalTimer.get();
107         if (timer == null)
108         {
109             // begin not called on the thread
110
return;
111         }
112         // time it
113
timer.stop();
114         recordStats(methodName, timer.getDuration());
115         
116         // drop the thread's timer
117
threadLocalTimer.set(null);
118     }
119 }
120
Popular Tags