001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide;
043:
044: import java.io.*;
045: import java.util.*;
046: import org.netbeans.performance.Benchmark;
047:
048: public class ErrorManagerTest extends Benchmark {
049:
050: static {
051: Properties prop = System.getProperties();
052: prop.put("perf.test.enabled", "-5");
053: // prop.put("perf.test.disabled", "0x1000000");
054: }
055:
056: private static final ErrorManager enabled;
057: private static final ErrorManager disabled;
058:
059: static {
060: ErrorManager en = ErrorManager.getDefault().getInstance(
061: "perf.test.enabled");
062: enabled = en.isLoggable(ErrorManager.INFORMATIONAL) ? en : null;
063: ErrorManager dis = ErrorManager.getDefault().getInstance(
064: "perf.test.disabled");
065: disabled = dis.isLoggable(ErrorManager.INFORMATIONAL) ? dis
066: : null;
067: assertNull("disabled is loggable", disabled);
068: }
069:
070: public ErrorManagerTest(String name) {
071: super (name);
072: }
073:
074: public void testLogEnabled() throws Exception {
075: int count = getIterationCount();
076: ErrorManager en = ErrorManager.getDefault().getInstance(
077: "perf.test.enabled");
078:
079: while (count-- > 0) {
080: // do the stuff here,
081: en.log("Logging event #" + count);
082: }
083: }
084:
085: public void testLogDisabled() throws Exception {
086: int count = getIterationCount();
087: ErrorManager dis = ErrorManager.getDefault().getInstance(
088: "perf.test.disabled");
089:
090: while (count-- > 0) {
091: // do the stuff here,
092: dis.log("Logging event #" + count);
093: }
094: }
095:
096: public void testCheckedEnabled() throws Exception {
097: int count = getIterationCount();
098: ErrorManager en = ErrorManager.getDefault().getInstance(
099: "perf.test.enabled");
100:
101: while (count-- > 0) {
102: // do the stuff here,
103: if (en.isLoggable(ErrorManager.INFORMATIONAL))
104: en.log("Logging event #" + count);
105: }
106: }
107:
108: public void testCheckedDisabled() throws Exception {
109: int count = getIterationCount();
110: ErrorManager dis = ErrorManager.getDefault().getInstance(
111: "perf.test.disabled");
112:
113: while (count-- > 0) {
114: // do the stuff here,
115: if (dis.isLoggable(ErrorManager.INFORMATIONAL))
116: dis.log("Logging event #" + count);
117: }
118: }
119:
120: public void testNullEnabled() throws Exception {
121: int count = getIterationCount();
122:
123: while (count-- > 0) {
124: // do the stuff here,
125: if (enabled != null)
126: enabled.log("Logging event #" + count);
127: }
128: }
129:
130: public void testNullDisabled() throws Exception {
131: int count = getIterationCount();
132:
133: while (count-- > 0) {
134: // do the stuff here,
135: if (disabled != null)
136: disabled.log("Logging event #" + count);
137: }
138: }
139:
140: public void testNull16Disabled() throws Exception {
141: int count = getIterationCount();
142:
143: while (count-- > 0) {
144: // do the stuff here,
145: if (disabled != null)
146: disabled.log("Logging event #" + count);
147: if (disabled != null)
148: disabled.log("Logging event #" + count);
149: if (disabled != null)
150: disabled.log("Logging event #" + count);
151: if (disabled != null)
152: disabled.log("Logging event #" + count);
153: if (disabled != null)
154: disabled.log("Logging event #" + count);
155: if (disabled != null)
156: disabled.log("Logging event #" + count);
157: if (disabled != null)
158: disabled.log("Logging event #" + count);
159: if (disabled != null)
160: disabled.log("Logging event #" + count);
161: if (disabled != null)
162: disabled.log("Logging event #" + count);
163: if (disabled != null)
164: disabled.log("Logging event #" + count);
165: if (disabled != null)
166: disabled.log("Logging event #" + count);
167: if (disabled != null)
168: disabled.log("Logging event #" + count);
169: if (disabled != null)
170: disabled.log("Logging event #" + count);
171: if (disabled != null)
172: disabled.log("Logging event #" + count);
173: if (disabled != null)
174: disabled.log("Logging event #" + count);
175: if (disabled != null)
176: disabled.log("Logging event #" + count);
177: }
178: }
179:
180: public static void main(String[] args) {
181: simpleRun(ErrorManager.class);
182: }
183:
184: /** Crippled version of NbErrorManager that does the logging the same way */
185: public static final class EM extends ErrorManager {
186: /** The writer to the log file*/
187: private PrintWriter logWriter = new PrintWriter(System.err);
188:
189: /** Minimum value of severity to write message to the log file*/
190: private int minLogSeverity = ErrorManager.INFORMATIONAL + 1; // NOI18N
191:
192: /** Prefix preprended to customized loggers, if any. */
193: private String prefix = null;
194:
195: // Make sure two distinct EM impls log differently even with the same name.
196: private int uniquifier = 0; // 0 for root EM (prefix == null), else >= 1
197: static final Map uniquifiedIds = new HashMap(20); // Map<String,Integer>
198:
199: /** Initializes the log stream.
200: */
201: private PrintWriter getLogWriter() {
202: return logWriter;
203: }
204:
205: public synchronized Throwable annotate(Throwable t,
206: int severity, String message, String localizedMessage,
207: Throwable stackTrace, java.util.Date date) {
208: return t;
209: }
210:
211: /** Associates annotations with this thread.
212: *
213: * @param arr array of annotations (or null)
214: */
215: public synchronized Throwable attachAnnotations(Throwable t,
216: Annotation[] arr) {
217: return t;
218: }
219:
220: /** Notifies all the exceptions associated with
221: * this thread.
222: */
223: public synchronized void notify(int severity, Throwable t) {
224: }
225:
226: public void log(int severity, String s) {
227: if (isLoggable(severity)) {
228: PrintWriter log = getLogWriter();
229:
230: if (prefix != null) {
231: boolean showUniquifier;
232: // Print a unique EM sequence # if there is more than one
233: // with this name. Shortcut: if the # > 1, clearly there are.
234: if (uniquifier > 1) {
235: showUniquifier = true;
236: } else if (uniquifier == 1) {
237: synchronized (uniquifiedIds) {
238: int count = ((Integer) uniquifiedIds
239: .get(prefix)).intValue();
240: showUniquifier = count > 1;
241: }
242: } else {
243: throw new IllegalStateException(
244: "prefix != null yet uniquifier == 0");
245: }
246: if (showUniquifier) {
247: log.print("[" + prefix + " #" + uniquifier
248: + "] "); // NOI18N
249: } else {
250: log.print("[" + prefix + "] "); // NOI18N
251: }
252: }
253: log.println(s);
254: log.flush();
255: }
256: }
257:
258: /** Allows to test whether messages with given severity will be logged
259: * or not prior to constraction of complicated and time expensive
260: * logging messages.
261: *
262: * @param severity the severity to check
263: * @return false if the next call to log method with the same severity will
264: * discard the message
265: */
266: public boolean isLoggable(int severity) {
267: return severity >= minLogSeverity;
268: }
269:
270: /** Returns an instance with given name. The name
271: * can be dot separated list of names creating
272: * a hierarchy.
273: */
274: public final ErrorManager getInstance(String name) {
275: EM newEM = new EM();
276: newEM.prefix = (prefix == null) ? name : prefix + '.'
277: + name;
278: synchronized (uniquifiedIds) {
279: Integer i = (Integer) uniquifiedIds.get(newEM.prefix);
280: if (i == null) {
281: newEM.uniquifier = 1;
282: } else {
283: newEM.uniquifier = i.intValue() + 1;
284: }
285: uniquifiedIds.put(newEM.prefix, new Integer(
286: newEM.uniquifier));
287: }
288: newEM.minLogSeverity = minLogSeverity;
289: String prop = newEM.prefix;
290: while (prop != null) {
291: String value = System.getProperty(prop);
292: //System.err.println ("Trying; prop=" + prop + " value=" + value);
293: if (value != null) {
294: try {
295: newEM.minLogSeverity = Integer.parseInt(value);
296: } catch (NumberFormatException nfe) {
297: notify(WARNING, nfe);
298: }
299: break;
300: } else {
301: int idx = prop.lastIndexOf('.');
302: if (idx == -1)
303: prop = null;
304: else
305: prop = prop.substring(0, idx);
306: }
307: }
308: //System.err.println ("getInstance: prefix=" + prefix + " mls=" + minLogSeverity + " name=" + name + " prefix2=" + newEM.prefix + " mls2=" + newEM.minLogSeverity);
309: return newEM;
310: }
311:
312: /** Finds annotations associated with given exception.
313: * @param t the exception
314: * @return array of annotations or null
315: */
316: public synchronized Annotation[] findAnnotations(Throwable t) {
317: return new Annotation[0];
318: }
319:
320: public String toString() {
321: return super .toString() + "<" + prefix + ","
322: + minLogSeverity + ">"; // NOI18N
323: }
324: }
325: }
|