1 /***
2 * Atlassian Source Code Template.
3 * User: Scott Farquhar
4 * Date: Feb 19, 2003
5 * Time: 6:56:26 PM
6 * CVS Revision: $Revision$
7 * Last CVS Commit: $Date$
8 * Author of last CVS Commit: $Author$
9 *
10 * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
11 */
12 package com.atlassian.util.profiling;
13
14
15 /***
16 * A timer stack.
17 * <p>
18 * Usage:
19 * <pre>
20 * String logMessage = "Log message";
21 * UtilTimerStack.push(logMessage);
22 * try
23 * {
24 * //do some code
25 * }
26 * finally
27 * {
28 * UtilTimerStack.pop(logMessage); //this needs to be the same text as above
29 * }
30 * </pre>
31 */
32 public class UtilTimerStack
33 {
34
35 // A reference to the current ProfilingTimerBean
36 private static ThreadLocal current = new ThreadLocal();
37
38 /***
39 * System property that controls whether this timer should be used or not. Set to "true" activates
40 * the timer. Set to "false" to disactivate.
41 */
42 public static final String ACTIVATE_PROPERTY = "atlassian.profile.activate";
43
44 public static final String MIN_TIME = "atlassian.profile.mintime";
45
46 public static void push(String name)
47 {
48 if (!isActive())
49 return;
50
51 //create a new timer and start it
52 ProfilingTimerBean newTimer = new ProfilingTimerBean(name);
53 newTimer.setStartTime();
54
55 //if there is a current timer - add the new timer as a child of it
56 ProfilingTimerBean currentTimer = (ProfilingTimerBean) current.get();
57 if (currentTimer != null)
58 {
59 currentTimer.addChild(newTimer);
60 }
61
62 //set the new timer to be the current timer
63 current.set(newTimer);
64 }
65
66 public static void pop(String name)
67 {
68 if (!isActive())
69 return;
70
71 ProfilingTimerBean currentTimer = (ProfilingTimerBean) current.get();
72
73 //if the timers are matched up with each other (ie push("a"); pop("a"));
74 if (currentTimer != null && name != null && name.equals(currentTimer.getResource()))
75 {
76 currentTimer.setEndTime();
77 ProfilingTimerBean parent = currentTimer.getParent();
78 //if we are the root timer, then print out the times
79 if (parent == null)
80 {
81 printTimes(currentTimer);
82 current.set(null); //for those servers that use thread pooling
83 }
84 else
85 {
86 current.set(parent);
87 }
88 }
89 else
90 {
91 //if timers are not matched up, then print what we have, and then print warning.
92 if (currentTimer != null)
93 {
94 printTimes(currentTimer);
95 current.set(null); //prevent printing multiple times
96 System.out.println("Unmatched Timer. Was expecting " + currentTimer.getResource() + ", instead got " + name);
97 }
98 }
99
100
101 }
102
103 private static void printTimes(ProfilingTimerBean currentTimer)
104 {
105 System.out.println(currentTimer.getPrintable(getMinTime()));
106 }
107
108 private static long getMinTime()
109 {
110 try
111 {
112 return Long.parseLong(System.getProperty(MIN_TIME, "0"));
113 }
114 catch (NumberFormatException e)
115 {
116 return -1;
117 }
118 }
119
120 public static boolean isActive()
121 {
122 return "true".equalsIgnoreCase(System.getProperty(ACTIVATE_PROPERTY));
123 }
124
125 public static void setActive(boolean active)
126 {
127 if (active)
128 System.setProperty(ACTIVATE_PROPERTY, "true");
129 else
130 System.setProperty(ACTIVATE_PROPERTY, "false");
131 }
132
133 }
This page was automatically generated by Maven