KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > Javolution


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution;
10
11 import j2me.lang.CharSequence;
12
13 import java.io.PrintStream JavaDoc;
14 import javolution.context.LogContext;
15 import javolution.text.Text;
16 import javolution.text.TextBuilder;
17
18 /**
19  * <p> This class contains the library {@link #main} method for
20  * versionning, self-tests, and performance analysis.</p>
21  * <p> It is also the base class for the library benchmarks and
22  * self-tests.</p>
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 1.0, September 14, 2004
26  */

27 public class Javolution {
28
29     /**
30      * Holds the version information.
31      */

32     public final static String JavaDoc VERSION = "@VERSION@";
33
34     /**
35      * Holds the current output stream (default System.out).
36      */

37     private static PrintStream JavaDoc Out = System.out;
38
39     /**
40      * Default constructor.
41      */

42     protected Javolution() {
43     }
44
45     /**
46      * The library {@link #main} method.
47      * The archive <code>javolution.jar</code> is auto-executable.
48      * [code]
49      * java -jar javolution.jar version <i>(show version information)</i>
50      * java -jar javolution.jar test <i>(perform self-tests)</i>
51      * java -jar javolution.jar perf <i>(run benchmark)</i>
52      * [/code]
53      *
54      * @param args the option arguments.
55      * @throws Exception if a problem occurs.
56      */

57     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
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     /**
80      * Performs simple tests.
81      *
82      * @throws Exception if a problem occurs.
83      */

84     private static void testing() throws Exception JavaDoc {
85         Out.print("Testing...");
86         Out.println("");
87         // TBD
88
Out.println("Success");
89     }
90
91     /**
92      * Measures performance.
93      */

94     private static void benchmark() throws Exception JavaDoc {
95         LogContext.setDefault(LogContext.STANDARD); // Logs info messages to console.
96
calibrate(); // Calculates timer offset.
97

98         Out.println("Run benchmark... (shortest execution times are displayed)");
99         Out.println("");
100         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
101         //new PerfIO().run();
102
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     /**
123      * Prints an object to <code>System.out</code> and then terminates the line.
124      *
125      * @param obj the object to be displayed.
126      */

127     public void println(Object JavaDoc obj) {
128         Out.println(obj);
129     }
130
131     /**
132      * Prints an object to current print stream.
133      *
134      * @param obj the object to be displayed.
135      */

136     public void print(Object JavaDoc obj) {
137         Out.print(obj);
138     }
139
140     /**
141      * Starts timer.
142      */

143     public void startTime() {
144         if (_time != 0) throw new Error JavaDoc("Timer not reset");
145         _time = nanoTime();
146     }
147
148     /**
149      * Sets the output stream.
150      *
151      * @param out the print stream.
152      */

153     public static void setOutputStream(PrintStream JavaDoc out) {
154         Out = out;
155     }
156
157     /**
158      * Ends measuring time and keeps the best time.
159      *
160      * @param iterations the number iterations performed since
161      * {@link #startTime}.
162      */

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     /**
174      * Ends measuring time and returns the best time.
175      *
176      * @param iterations the number iterations performed since
177      * {@link #startTime}.
178      */

179     public String JavaDoc endTime() {
180         long divisor;
181         String JavaDoc unit;
182         if (_picoDuration > 1000 * 1000 * 1000 * 1000L) { // 1 s
183
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(); // 4 digits precision.
198
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         /*@JVM-1.5+@
210         if (true) return System.nanoTime();
211         /**/

212        return System.currentTimeMillis() * 1000000;
213     }
214
215     //////////////////////////////////////
216
// Utilities for Javolution use only.
217
//
218

219     /**
220      * Returns the class having the specified name; for
221      * backward compatibility with CLDC 1.0 (cannot use .class as exception
222      * java.lang.NoClassDefFoundError does not exist for that platform).
223      */

224     public static Class JavaDoc j2meGetClass(String JavaDoc name) {
225         Class JavaDoc cls = null;
226         try {
227             cls = Class.forName(name); // Caller class loader.
228
} catch (ClassNotFoundException JavaDoc e0) { // Try context class loader.
229
/*@JVM-1.4+@
230              try {
231              ClassLoader cl = Thread.currentThread().getContextClassLoader();
232              cls = Class.forName(name, true, cl);
233              } catch (ClassNotFoundException e1) { // Try system class loader.
234              ClassLoader cl = ClassLoader.getSystemClassLoader();
235              try {
236              cls = Class.forName(name, true, cl);
237              } catch (ClassNotFoundException e) {
238              }
239              }
240              /**/

241         }
242         if (cls == null)
243             throw new JavolutionError("Class " + name + " not found");
244         return cls;
245     }
246
247     /**
248      * Converts the specified String as CharSequence (String is a
249      * CharSequence only for J2SE 1.4+).
250      *
251      * @param str the String to convert.
252      * @return <code>this</code> or a text wrapper.
253      */

254     public static CharSequence JavaDoc j2meToCharSeq(Object JavaDoc str) {
255         return (str instanceof CharSequence JavaDoc) ? (CharSequence JavaDoc) str
256                 : (str == null) ? null : Text.valueOf((String JavaDoc) str);
257     }
258 }
Popular Tags