1 19 20 package org.openide; 21 22 import java.io.*; 23 import java.util.*; 24 import org.netbeans.performance.Benchmark; 25 26 public class ErrorManagerTest extends Benchmark { 27 28 static { 29 Properties prop = System.getProperties(); 30 prop.put("perf.test.enabled", "-5"); 31 } 33 34 private static final ErrorManager enabled; 35 private static final ErrorManager disabled; 36 37 static { 38 ErrorManager en = ErrorManager.getDefault().getInstance("perf.test.enabled"); 39 enabled = en.isLoggable(ErrorManager.INFORMATIONAL) ? en : null; 40 ErrorManager dis = ErrorManager.getDefault().getInstance("perf.test.disabled"); 41 disabled = dis.isLoggable(ErrorManager.INFORMATIONAL) ? dis : null; 42 assertNull("disabled is loggable", disabled); 43 } 44 45 46 public ErrorManagerTest(String name) { 47 super( name ); 48 } 49 50 public void testLogEnabled() throws Exception { 51 int count = getIterationCount(); 52 ErrorManager en = ErrorManager.getDefault().getInstance("perf.test.enabled"); 53 54 while( count-- > 0 ) { 55 en.log("Logging event #" + count); 57 } 58 } 59 60 public void testLogDisabled() throws Exception { 61 int count = getIterationCount(); 62 ErrorManager dis = ErrorManager.getDefault().getInstance("perf.test.disabled"); 63 64 while( count-- > 0 ) { 65 dis.log("Logging event #" + count); 67 } 68 } 69 70 public void testCheckedEnabled() throws Exception { 71 int count = getIterationCount(); 72 ErrorManager en = ErrorManager.getDefault().getInstance("perf.test.enabled"); 73 74 while( count-- > 0 ) { 75 if(en.isLoggable(ErrorManager.INFORMATIONAL)) en.log("Logging event #" + count); 77 } 78 } 79 80 public void testCheckedDisabled() throws Exception { 81 int count = getIterationCount(); 82 ErrorManager dis = ErrorManager.getDefault().getInstance("perf.test.disabled"); 83 84 while( count-- > 0 ) { 85 if(dis.isLoggable(ErrorManager.INFORMATIONAL)) dis.log("Logging event #" + count); 87 } 88 } 89 90 public void testNullEnabled() throws Exception { 91 int count = getIterationCount(); 92 93 while( count-- > 0 ) { 94 if(enabled != null) enabled.log("Logging event #" + count); 96 } 97 } 98 99 public void testNullDisabled() throws Exception { 100 int count = getIterationCount(); 101 102 while( count-- > 0 ) { 103 if(disabled != null) disabled.log("Logging event #" + count); 105 } 106 } 107 108 public void testNull16Disabled() throws Exception { 109 int count = getIterationCount(); 110 111 while( count-- > 0 ) { 112 if(disabled != null) disabled.log("Logging event #" + count); 114 if(disabled != null) disabled.log("Logging event #" + count); 115 if(disabled != null) disabled.log("Logging event #" + count); 116 if(disabled != null) disabled.log("Logging event #" + count); 117 if(disabled != null) disabled.log("Logging event #" + count); 118 if(disabled != null) disabled.log("Logging event #" + count); 119 if(disabled != null) disabled.log("Logging event #" + count); 120 if(disabled != null) disabled.log("Logging event #" + count); 121 if(disabled != null) disabled.log("Logging event #" + count); 122 if(disabled != null) disabled.log("Logging event #" + count); 123 if(disabled != null) disabled.log("Logging event #" + count); 124 if(disabled != null) disabled.log("Logging event #" + count); 125 if(disabled != null) disabled.log("Logging event #" + count); 126 if(disabled != null) disabled.log("Logging event #" + count); 127 if(disabled != null) disabled.log("Logging event #" + count); 128 if(disabled != null) disabled.log("Logging event #" + count); 129 } 130 } 131 132 public static void main(String [] args) { 133 simpleRun( ErrorManager.class ); 134 } 135 136 137 public static final class EM extends ErrorManager { 138 139 private PrintWriter logWriter = new PrintWriter(System.err); 140 141 142 private int minLogSeverity = ErrorManager.INFORMATIONAL + 1; 144 145 private String prefix = null; 146 147 private int uniquifier = 0; static final Map uniquifiedIds = new HashMap(20); 151 152 154 private PrintWriter getLogWriter () { 155 return logWriter; 156 } 157 158 public synchronized Throwable annotate ( 159 Throwable t, 160 int severity, String message, String localizedMessage, 161 Throwable stackTrace, java.util.Date date 162 ) { 163 return t; 164 } 165 166 167 171 public synchronized Throwable attachAnnotations (Throwable t, Annotation[] arr) { 172 return t; 173 } 174 175 178 public synchronized void notify (int severity, Throwable t) { 179 } 180 181 public void log(int severity, String s) { 182 if (isLoggable (severity)) { 183 PrintWriter log = getLogWriter (); 184 185 if (prefix != null) { 186 boolean showUniquifier; 187 if (uniquifier > 1) { 190 showUniquifier = true; 191 } else if (uniquifier == 1) { 192 synchronized (uniquifiedIds) { 193 int count = ((Integer )uniquifiedIds.get(prefix)).intValue(); 194 showUniquifier = count > 1; 195 } 196 } else { 197 throw new IllegalStateException ("prefix != null yet uniquifier == 0"); 198 } 199 if (showUniquifier) { 200 log.print ("[" + prefix + " #" + uniquifier + "] "); } else { 202 log.print ("[" + prefix + "] "); } 204 } 205 log.println(s); 206 log.flush(); 207 } 208 } 209 210 218 public boolean isLoggable (int severity) { 219 return severity >= minLogSeverity; 220 } 221 222 223 227 public final ErrorManager getInstance(String name) { 228 EM newEM = new EM(); 229 newEM.prefix = (prefix == null) ? name : prefix + '.' + name; 230 synchronized (uniquifiedIds) { 231 Integer i = (Integer )uniquifiedIds.get(newEM.prefix); 232 if (i == null) { 233 newEM.uniquifier = 1; 234 } else { 235 newEM.uniquifier = i.intValue() + 1; 236 } 237 uniquifiedIds.put(newEM.prefix, new Integer (newEM.uniquifier)); 238 } 239 newEM.minLogSeverity = minLogSeverity; 240 String prop = newEM.prefix; 241 while (prop != null) { 242 String value = System.getProperty (prop); 243 if (value != null) { 245 try { 246 newEM.minLogSeverity = Integer.parseInt (value); 247 } catch (NumberFormatException nfe) { 248 notify (WARNING, nfe); 249 } 250 break; 251 } else { 252 int idx = prop.lastIndexOf ('.'); 253 if (idx == -1) 254 prop = null; 255 else 256 prop = prop.substring (0, idx); 257 } 258 } 259 return newEM; 261 } 262 263 264 268 public synchronized Annotation[] findAnnotations (Throwable t) { 269 return new Annotation[0]; 270 } 271 272 public String toString() { 273 return super.toString() + "<" + prefix + "," + minLogSeverity + ">"; } 275 } 276 } 277 | Popular Tags |