1 9 package javolution; 10 11 import j2me.lang.CharSequence; 12 13 import java.io.PrintStream ; 14 import javolution.context.LogContext; 15 import javolution.text.Text; 16 import javolution.text.TextBuilder; 17 18 27 public class Javolution { 28 29 32 public final static String VERSION = "@VERSION@"; 33 34 37 private static PrintStream Out = System.out; 38 39 42 protected Javolution() { 43 } 44 45 57 public static void main(String [] args) throws Exception { 58 Out.println("Javolution - Java(TM) Solution for Real-Time and Embedded Systems"); 59 Out.println("Version " + VERSION + " (http://javolution.org)"); 60 Out.println(""); 61 if (args.length > 0) { 62 if (args[0].equals("version")) { 63 return; 64 } else if (args[0].equals("test")) { 65 testing(); 66 return; 67 } else if (args[0].equals("perf")) { 68 benchmark(); 69 return; 70 } 71 } 72 Out.println("Usage: java -jar javolution.jar [arg]"); 73 Out.println("where arg is one of:"); 74 Out.println(" version (to show version information only)"); 75 Out.println(" test (to perform self-tests)"); 76 Out.println(" perf (to run benchmark)"); 77 } 78 79 84 private static void testing() throws Exception { 85 Out.print("Testing..."); 86 Out.println(""); 87 Out.println("Success"); 89 } 90 91 94 private static void benchmark() throws Exception { 95 LogContext.setDefault(LogContext.STANDARD); calibrate(); 98 Out.println("Run benchmark... (shortest execution times are displayed)"); 99 Out.println(""); 100 Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 101 new PerfContext().run(); 103 new PerfText().run(); 104 new PerfStream().run(); 105 new PerfXML().run(); 106 new PerfUtil().run(); 107 108 Out.println("More performance analysis in future versions..."); 109 } 110 111 private static void calibrate() { 112 Javolution test = new Javolution(); 113 for (int i=0; i < 1000; i++) { 114 test.startTime(); 115 test.keepBestTime(1); 116 } 117 _TimerOffset = test._picoDuration / 1000; 118 119 } 120 static long _TimerOffset = 0; 121 122 127 public void println(Object obj) { 128 Out.println(obj); 129 } 130 131 136 public void print(Object obj) { 137 Out.print(obj); 138 } 139 140 143 public void startTime() { 144 if (_time != 0) throw new Error ("Timer not reset"); 145 _time = nanoTime(); 146 } 147 148 153 public static void setOutputStream(PrintStream out) { 154 Out = out; 155 } 156 157 163 public void keepBestTime(int iterations) { 164 long nanoSeconds = nanoTime() - _time - _TimerOffset; 165 long picoDuration = (nanoSeconds * 1000 / iterations); 166 if (picoDuration < _picoDuration) { 167 _picoDuration = picoDuration; 168 } 169 _time = 0; 170 } 171 private long _picoDuration = Long.MAX_VALUE; 172 173 179 public String endTime() { 180 long divisor; 181 String unit; 182 if (_picoDuration > 1000 * 1000 * 1000 * 1000L) { unit = " s"; 184 divisor = 1000 * 1000 * 1000 * 1000L; 185 } else if (_picoDuration > 1000 * 1000 * 1000L) { 186 unit = " ms"; 187 divisor = 1000 * 1000 * 1000L; 188 } else if (_picoDuration > 1000 * 1000L) { 189 unit = " us"; 190 divisor = 1000 * 1000L; 191 } else { 192 unit = " ns"; 193 divisor = 1000L; 194 } 195 TextBuilder tb = TextBuilder.newInstance(); 196 tb.append(_picoDuration / divisor); 197 int fracDigits = 4 - tb.length(); tb.append("."); 199 for (int i = 0, j = 10; i < fracDigits; i++, j *= 10) { 200 tb.append((_picoDuration * j / divisor) % 10); 201 } 202 _picoDuration = Long.MAX_VALUE; 203 return tb.append(unit).toString(); 204 } 205 206 private static long _time; 207 208 private static long nanoTime() { 209 212 return System.currentTimeMillis() * 1000000; 213 } 214 215 219 224 public static Class j2meGetClass(String name) { 225 Class cls = null; 226 try { 227 cls = Class.forName(name); } catch (ClassNotFoundException e0) { 241 } 242 if (cls == null) 243 throw new JavolutionError("Class " + name + " not found"); 244 return cls; 245 } 246 247 254 public static CharSequence j2meToCharSeq(Object str) { 255 return (str instanceof CharSequence ) ? (CharSequence ) str 256 : (str == null) ? null : Text.valueOf((String ) str); 257 } 258 } | Popular Tags |