KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > UIStats


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.misc;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.core.runtime.PerformanceStats;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.ui.PlatformUI;
18
19 /**
20  * This class is used for monitoring performance events. Each performance
21  * event has an associated option in the org.eclipse.ui plugin's .options file
22  * that specifies an maximum acceptable duration for that event.
23  *
24  * @see org.eclipse.core.runtime.PerformanceStats
25  */

26 public class UIStats {
27     
28      private static HashMap JavaDoc operations = new HashMap JavaDoc();
29
30     public static final int CREATE_PART = 0;
31
32     public static final int CREATE_PART_CONTROL = 1;
33
34     public static final int INIT_PART = 2;
35
36     public static final int CREATE_PERSPECTIVE = 3;
37
38     public static final int RESTORE_WORKBENCH = 4;
39
40     public static final int START_WORKBENCH = 5;
41
42     public static final int CREATE_PART_INPUT = 6;
43
44     public static final int ACTIVATE_PART = 7;
45
46     public static final int BRING_PART_TO_TOP = 8;
47
48     public static final int NOTIFY_PART_LISTENERS = 9;
49
50     public static final int SWITCH_PERSPECTIVE = 10;
51     
52     public static final int NOTIFY_PAGE_LISTENERS = 11;
53
54     public static final int NOTIFY_PERSPECTIVE_LISTENERS = 12;
55
56     public static final int UI_JOB = 13;
57     
58     public static final int CONTENT_TYPE_LOOKUP = 14;
59
60     /**
61      * Change this value when you add a new event constant.
62      */

63     public static final int LAST_VALUE = CONTENT_TYPE_LOOKUP;
64
65     private static boolean debug[] = new boolean[LAST_VALUE+1];
66
67     private static String JavaDoc[] events = new String JavaDoc[LAST_VALUE+1];
68
69     static {
70         events[CREATE_PART] = PlatformUI.PLUGIN_ID + "/perf/part.create"; //$NON-NLS-1$
71
events[CREATE_PART_INPUT] = PlatformUI.PLUGIN_ID + "/perf/part.input"; //$NON-NLS-1$
72
events[CREATE_PART_CONTROL] = PlatformUI.PLUGIN_ID + "/perf/part.control"; //$NON-NLS-1$
73
events[INIT_PART] = PlatformUI.PLUGIN_ID + "/perf/part.init"; //$NON-NLS-1$
74
events[CREATE_PERSPECTIVE] = PlatformUI.PLUGIN_ID + "/perf/perspective.create"; //$NON-NLS-1$
75
events[SWITCH_PERSPECTIVE] = PlatformUI.PLUGIN_ID + "/perf/perspective.switch"; //$NON-NLS-1$
76
events[RESTORE_WORKBENCH] = PlatformUI.PLUGIN_ID + "/perf/workbench.restore"; //$NON-NLS-1$
77
events[START_WORKBENCH] = PlatformUI.PLUGIN_ID + "/perf/workbench.start"; //$NON-NLS-1$
78
events[ACTIVATE_PART] = PlatformUI.PLUGIN_ID + "/perf/part.activate"; //$NON-NLS-1$
79
events[BRING_PART_TO_TOP] = PlatformUI.PLUGIN_ID + "/perf/part.activate"; //$NON-NLS-1$
80
events[NOTIFY_PART_LISTENERS] = PlatformUI.PLUGIN_ID + "/perf/part.listeners"; //$NON-NLS-1$
81
events[NOTIFY_PAGE_LISTENERS] = PlatformUI.PLUGIN_ID + "/perf/page.listeners"; //$NON-NLS-1$
82
events[NOTIFY_PERSPECTIVE_LISTENERS] = PlatformUI.PLUGIN_ID + "/perf/perspective.listeners"; //$NON-NLS-1$
83
events[UI_JOB] = PlatformUI.PLUGIN_ID + "/perf/uijob"; //$NON-NLS-1$
84
events[CONTENT_TYPE_LOOKUP] = PlatformUI.PLUGIN_ID + "/perf/contentTypes"; //$NON-NLS-1$
85

86         for (int i = 0; i <= LAST_VALUE; i++) {
87             //don't log any performance events if the general performance stats is disabled
88
if (events[i] != null && PerformanceStats.ENABLED) {
89                 debug[i] = PerformanceStats.isEnabled(events[i]);
90             }
91         }
92     }
93
94     /**
95      * Returns whether tracing of the given debug event is turned on.
96      *
97      * @param event The event id
98      * @return <code>true</code> if tracing of this event is turned on,
99      * and <code>false</code> otherwise.
100      */

101     public static boolean isDebugging(int event) {
102         return debug[event];
103     }
104     
105     /**
106      * Indicates the start of a performance event
107      *
108      * @param event The event id
109      * @param label The event label
110      */

111     public static void start(int event, String JavaDoc label) {
112         if (debug[event]) {
113             operations.put(event + label, new Long JavaDoc(System.currentTimeMillis()));
114         }
115     }
116
117     /**
118      * Indicates the end of a performance operation
119      *
120      * @param event The event id
121      * @param blame An object that is responsible for the event that occurred,
122      * or that uniquely describes the event that occurred
123      * @param label The event label
124      */

125     public static void end(int event, Object JavaDoc blame, String JavaDoc label) {
126         if (debug[event]) {
127             Long JavaDoc startTime = (Long JavaDoc) operations.remove(event + label);
128             if (startTime == null) {
129                 return;
130             }
131             final long elapsed = System.currentTimeMillis() - startTime.longValue();
132 // System.out.println("Time - " + //$NON-NLS-1$
133
// elapsed + events[event] + label);
134
PerformanceStats.getStats(events[event], blame).addRun(elapsed, label);
135         }
136     }
137     
138     /**
139      * Special hook to signal that application startup is complete and the event
140      * loop has started running.
141      */

142     public static void startupComplete() {
143         // We use a runtime debug option here for backwards compatibility (bug 96672)
144
// Note that this value is only relevant if the workspace chooser is not used.
145
String JavaDoc option = Platform.getDebugOption(Platform.PI_RUNTIME + "/debug"); //$NON-NLS-1$
146
if (option == null || !"true".equalsIgnoreCase(option)) { //$NON-NLS-1$
147
return;
148         }
149         String JavaDoc startString = System.getProperty("eclipse.startTime"); //$NON-NLS-1$
150
if (startString == null) {
151             return;
152         }
153         try {
154             long start = Long.parseLong(startString);
155             long end = System.currentTimeMillis();
156             System.out.println("Startup complete: " + (end - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
157
} catch (NumberFormatException JavaDoc e) {
158             //this is just debugging code -- ok to swallow exception
159
}
160     }
161 }
162
Popular Tags