KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > startup > MeasureJ2EEStartupTime


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package startup;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.netbeans.junit.NbPerformanceTestCase;
36 import org.netbeans.junit.NbTestSuite;
37
38 /**
39  * Measure startup time by org.netbeans.core.perftool.StartLog.
40  * Number of starts with new userdir is defined by property
41  * <br> <code> org.netbeans.performance.repeat.with.new.userdir </code>
42  * <br> and number of starts with old userdir is defined by property
43  * <br> <code> org.netbeans.performance.repeat </code>
44  * Run measurement defined number times, but forget first measured value,
45  * it's a attempt to have still the same testing conditions with
46  * loaded and cached files.
47  *
48  * @author Martin.Brehovsky@sun.com, Antonin.Nebuzelsky@sun.com, Marian.Mirilovic@sun.com
49  */

50 public class MeasureJ2EEStartupTime extends NbPerformanceTestCase {
51     
52     /** String used for unix platform. */
53     protected static final String JavaDoc UNIX = "unix";
54     
55     /** String used for windows platform. */
56     protected static final String JavaDoc WINDOWS = "windows";
57     
58     /** String used for unknown platform. */
59     protected static final String JavaDoc UNKNOWN = "unknown";
60     
61     /** Number of repeated runs on the same userdir (for first run is used new userdir). */
62     protected static int repeat = Integer.getInteger("org.netbeans.performance.repeat", 1).intValue();
63     
64     /** Number of repeated runs on new userdir. */
65     protected static int repeatNewUserdir = Integer.getInteger("org.netbeans.performance.repeat.with.new.userdir", 1).intValue();
66     
67     /** Number used for unknow time. */
68     protected final static long UNKNOWN_TIME = -1;
69     
70     /** Table of the supported platforms. */
71     protected static final String JavaDoc [][] SUPPORTED_PLATFORMS = {
72         // XXX need to add MacOS
73
{"Linux,i386",UNIX},
74         {"SunOS,sparc",UNIX},
75         {"SunOS,x86",UNIX},
76         {"Windows_NT,x86",WINDOWS},
77         {"Windows_2000,x86",WINDOWS},
78         {"Windows_XP,x86",WINDOWS},
79         {"Windows_95,x86",WINDOWS},
80         {"Windows_98,x86",WINDOWS},
81         {"Windows_Me,x86",WINDOWS}
82     };
83     
84     protected static final String JavaDoc[][] STARTUP_DATA = {
85         {"ModuleSystem.readList finished", "ModuleSystem.readList finished, took ","ms"},
86         {"Preparation finished", "Preparation finished, took ","ms"},
87         {"Window system loaded", "Window system loaded dT=", ""},
88         {"Window system shown" , "Window system shown dT=",""},
89         {"Start", "IDE starts t=", ""},
90         {"End", "IDE is running t=", ""}
91     };
92     
93     /** Define testcase
94      * @param testName name of the testcase
95      */

96     public MeasureJ2EEStartupTime(java.lang.String JavaDoc testName) {
97         super(testName);
98     }
99     
100     /** Testing start of IDE with measurement of the startup time.
101      * @throws IOException
102      */

103     public void testStartIDE() throws IOException JavaDoc {
104         // don't report first run, try to have still the same testing conditions
105
runIDE(getIdeHome(),new File JavaDoc(getWorkDir(),"ideuserdir_prepare"),getMeasureFile(0,0),0);
106         
107         for (int n=1;n <= repeatNewUserdir; n++){
108             for (int i=1; i <= repeat; i++) {
109                 long measuredTime = runIDEandMeasureStartup(getMeasureFile(i,n), getUserdirFile(n),5000);
110                 reportPerformance("Startup Time", measuredTime, "ms", i>1?2:1);
111             }
112         }
113     }
114     
115      /** Testing start of IDE with measurement of the startup time.
116      * @throws IOException
117      */

118     public void testStartIDEWithOpenedFiles() throws IOException JavaDoc {
119         for (int i=1; i <= repeat; i++) {
120             long measuredTime = runIDEandMeasureStartup(getMeasureFile(i), getUserdirFile(), 30000);
121             reportPerformance("Startup Time with opened J2EE projects", measuredTime, "ms", 2);
122         }
123         
124     }
125     
126     /** Testing start of IDE with measurement of the startup time.
127      * @throws IOException
128      */

129     public void testStartIDEWithWeb() throws IOException JavaDoc {
130         for (int i=1; i <= repeat; i++) {
131             long measuredTime = runIDEandMeasureStartup(getMeasureFile(i), getUserdirFile(), 30000);
132             reportPerformance("Startup Time with opened Web projects", measuredTime, "ms", 2);
133         }
134         
135     }
136     
137     /** Run IDE and read measured time from file
138      *
139      * @param measureFile file where the time of window system painting is stored
140      * @throws java.io.IOException
141      * @return startup time
142      */

143     private long runIDEandMeasureStartup(File JavaDoc measureFile, File JavaDoc userdir, long timeout) throws IOException JavaDoc {
144         long startTime = runIDE(getIdeHome(),userdir,measureFile,timeout);
145         Hashtable JavaDoc measuredValues = parseMeasuredValues(measureFile);
146         
147         if(measuredValues==null)
148             fail("It isn't possible measure startup time");
149         
150         long runTime=((Long JavaDoc)measuredValues.get("IDE starts t=")).longValue(); // from STARTUP_DATA
151
measuredValues.remove("IDE starts t="); // remove from measured values, the rest we will log as performance data
152
long endTime=((Long JavaDoc)measuredValues.get("IDE is running t=")).longValue(); // from STARTUP_DATA
153
measuredValues.remove("IDE is running t="); // remove from measured values, the rest we will log as performance data
154

155         long startupTime = endTime - startTime;
156         
157         System.out.println("Measured Startup Time=" + startupTime + " (run-start="+(runTime-startTime)+" , end-run="+(endTime-runTime)+")");
158         
159         if (startupTime <= 0)
160             fail("Measured value ["+startupTime+"] is not > 0 !");
161         
162         reportPerformance("IDE run",(runTime-startTime),"ms",1);
163         
164         
165         for(int i=0; i<STARTUP_DATA.length; i++){
166             if(measuredValues.containsKey(STARTUP_DATA[i][1])){
167                 long value = ((Long JavaDoc)measuredValues.get(STARTUP_DATA[i][1])).longValue();
168                 System.out.println(STARTUP_DATA[i][0]+"="+value);
169                 reportPerformance(STARTUP_DATA[i][0],value,"ms",1);
170             }else{
171                 System.out.println("Value for "+STARTUP_DATA[i][1]+" isn't present");
172             }
173         }
174         
175         return startupTime;
176     }
177     
178     
179     /** Get platform on which the code is executed
180      * @return current platform name
181      */

182     protected static String JavaDoc getPlatform() {
183         String JavaDoc platformString=(System.getProperty("os.name","")+","+ System.getProperty("os.arch","")).replace(' ','_');
184         for (int i=0; i<SUPPORTED_PLATFORMS.length; i++) {
185             if (platformString.equalsIgnoreCase(SUPPORTED_PLATFORMS[i][0])) {
186                 return SUPPORTED_PLATFORMS[i][1];
187             }
188         }
189         return UNKNOWN;
190     }
191     
192     
193     
194     /** Creates and executes the command line for running IDE.
195      * @param ideHome IDE home directory
196      * @param userdir User directory
197      * @param measureFile file where measured time is stored
198      * @throws IOException
199      */

200     protected static long runIDE(File JavaDoc ideHome, File JavaDoc userdir, File JavaDoc measureFile, long timeout) throws IOException JavaDoc {
201         
202         //check <userdir>/lock file
203
if(new File JavaDoc(userdir, "lock").exists())
204             fail("Original Userdir is locked!");
205         
206         //add guitracker on classpath
207
String JavaDoc classpath = System.getProperty("performance.testutilities.dist.jar");
208         
209         // create jdkhome switch
210
String JavaDoc jdkhome = System.getProperty("java.home");
211         if(jdkhome.endsWith("jre"))
212             jdkhome = jdkhome.substring(0, jdkhome.length()-4);
213         
214         String JavaDoc platform = getPlatform();
215         File JavaDoc ideBinDir = new File JavaDoc(ideHome,"bin");
216         String JavaDoc cmd;
217         if (platform.equals(WINDOWS)) {
218             cmd = (new File JavaDoc(ideBinDir,"netbeans.exe")).getAbsolutePath();
219         } else {
220             cmd = (new File JavaDoc(ideBinDir,"netbeans")).getAbsolutePath();
221         }
222         // add other argumens
223
// guiltracker lib
224
cmd += " --cp:a "+classpath;
225         // userdir
226
cmd += " --userdir "+userdir.getAbsolutePath();
227         // get jdkhome path
228
cmd += " --jdkhome "+jdkhome;
229         // netbeans full hack
230
cmd += " -J-Dnetbeans.full.hack=true";
231         // measure argument
232
cmd += " -J-Dorg.netbeans.log.startup.logfile="+measureFile.getAbsolutePath();
233         // measure argument - we have to set this one to ommit repaint of memory toolbar (see openide/actions/GarbageCollectAction)
234
cmd += " -J-Dorg.netbeans.log.startup=tests";
235         // close the IDE after startup
236
cmd += " -J-Dnetbeans.close=true";
237         // wait after startup, need to set longer time for complex startup because rescan rises
238
cmd += " -J-Dorg.netbeans.performance.waitafterstartup="+timeout;
239         // disable rescaning after startup
240
// cmd += " -J-Dnetbeans.javacore.noscan=true";
241

242         System.out.println("Running: "+cmd);
243         
244         Runtime JavaDoc runtime = Runtime.getRuntime();
245         
246         long startTime=System.currentTimeMillis();
247         
248         // need to create out and err handlers
249
Process JavaDoc ideProcess = runtime.exec(cmd,null,ideBinDir);
250         
251         // track out and errs from ide - the last parameter is PrintStream where the
252
// streams are copied - currently set to null, so it does not hit performance much
253
ThreadReader sout = new ThreadReader(ideProcess.getInputStream(), null);
254         ThreadReader serr = new ThreadReader(ideProcess.getErrorStream(), null);
255         try {
256             int exitStatus = ideProcess.waitFor();
257             System.out.println("IDE exited with status = "+exitStatus);
258         } catch (InterruptedException JavaDoc ie) {
259             ie.printStackTrace(System.err);
260             IOException JavaDoc ioe = new IOException JavaDoc("Caught InterruptedException :"+ie.getMessage());
261             ioe.initCause(ie);
262             throw ioe;
263         }
264         
265         return startTime;
266     }
267     
268     /** Get IDE home directory.
269      * @throws IOException
270      * @return IDE home directory
271      */

272     protected File JavaDoc getIdeHome() throws IOException JavaDoc {
273         String JavaDoc nbHome = System.getProperty("netbeans.dest.dir");
274         File JavaDoc ideHome = new File JavaDoc(nbHome);
275         if (!ideHome.isDirectory()) {
276             throw new IOException JavaDoc("Cannot found netbeans.dest.dir - supplied value is "+nbHome);
277         }
278         return ideHome;
279     }
280     
281     /** Get User directory
282      * @throws IOException
283      * @return User directory
284      */

285     protected File JavaDoc getUserdirFile(int n) throws IOException JavaDoc {
286         return new File JavaDoc(getWorkDir(),"ideuserdir_"+n);
287     }
288     
289     /** Get User directory.
290      * User directory is prepared and defined by property
291      * <br> <code> userdir.prepared </code> + "/sys/ide"
292      * @throws IOException
293      * @return User directory
294      */

295     protected File JavaDoc getUserdirFile() throws IOException JavaDoc {
296         return new File JavaDoc(new File JavaDoc(System.getProperty("userdir.prepared"),"sys"),"ide");
297     }
298     
299     /** Get Sketchpad directory.
300      * @throws IOException
301      * @return Sketchpad directory
302      */

303     private File JavaDoc getSketchpad() throws IOException JavaDoc {
304         String JavaDoc xtestSketchpad = System.getProperty("xtest.sketchpad");
305         if (xtestSketchpad == null) {
306             throw new IOException JavaDoc("Cannot find xtest.sketchpad");
307         } else {
308             return new File JavaDoc(xtestSketchpad);
309         }
310     }
311     
312     
313     /** Get file where measured time will be stored.
314      * @param i number of the new userdir used
315      * @param j number of the run with the same userdir
316      * @throws IOException
317      * @return file where the measured time will be stored
318      */

319     protected File JavaDoc getMeasureFile(int i, int j) throws IOException JavaDoc {
320         return new File JavaDoc(getWorkDir(),"measured_startup_"+i+"_"+j+".txt");
321     }
322     
323     
324     /** Get file where measured time will be stored.
325      * @param j number of the run with the same userdir
326      * @throws IOException
327      * @return file where the measured time will be stored
328      */

329     protected File JavaDoc getMeasureFile(int i) throws IOException JavaDoc {
330         return new File JavaDoc(getWorkDir(),"measured_startup_"+i+".txt");
331     }
332     
333     /** Parse logged startup time from the file.
334      * @param measuredFile file where the startup time is stored
335      * @return measured startup time
336      */

337     protected static Hashtable JavaDoc parseMeasuredValues(File JavaDoc measuredFile) {
338         Hashtable JavaDoc measuredValues = new Hashtable JavaDoc();
339         
340         Hashtable JavaDoc startup_data = new Hashtable JavaDoc();
341         for(int i=0; i<STARTUP_DATA.length; i++)
342             startup_data.put(STARTUP_DATA[i][1], STARTUP_DATA[i][2]);
343                 
344         BufferedReader JavaDoc br = null;
345         try {
346             br = new BufferedReader JavaDoc(new FileReader JavaDoc(measuredFile));
347             String JavaDoc readLine, str, value;
348             int begin, end;
349             while((readLine = br.readLine())!=null && !startup_data.isEmpty()){
350                 try {
351                     java.util.Iterator JavaDoc iter = startup_data.keySet().iterator();
352                     while(iter.hasNext()){
353                         str = (String JavaDoc)iter.next();
354                         begin = readLine.indexOf(str);
355                         if(begin!=-1){
356                             end = readLine.indexOf((String JavaDoc)startup_data.get(str));
357                             
358                             if(end<=begin) end=readLine.length();
359                             
360                             value = readLine.substring(begin+str.length(), end);
361                             measuredValues.put(str, new Long JavaDoc(value));
362                             startup_data.remove(str);
363                             break;
364                         }
365                             
366                     }
367                     
368                 } catch (NumberFormatException JavaDoc nfe) {
369                     nfe.printStackTrace(System.err);
370                     return null;
371                 }
372             }
373             return measuredValues;
374         } catch (IOException JavaDoc ioe) {
375             ioe.printStackTrace(System.err);
376             return null;
377         } finally {
378             if (br != null) {
379                 try {
380                     br.close();
381                 } catch (IOException JavaDoc ioe) {
382                     ioe.printStackTrace(System.err);
383                     return null;
384                 }
385             }
386         }
387     }
388     
389     
390     protected static class ThreadReader implements Runnable JavaDoc {
391         
392         private Thread JavaDoc thread;
393         private BufferedReader JavaDoc br;
394         private PrintStream JavaDoc out;
395         private String JavaDoc s;
396         
397         ThreadReader(InputStream JavaDoc in, PrintStream JavaDoc out) {
398             br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
399             this.out = out;
400             thread = new Thread JavaDoc(this);
401             thread.start();
402         }
403         
404         public void run() {
405             Thread JavaDoc myThread = Thread.currentThread();
406             while (thread == myThread) {
407                 try {
408                     s = br.readLine();
409                     if (s == null) {
410                         stop();
411                         break;
412                     } else {
413                         if (out != null) {
414                             out.println(s);
415                         }
416                     }
417                 } catch (IOException JavaDoc ioe) {
418                     ioe.printStackTrace(System.err);
419                     if (out != null) {
420                         out.println("Caught IOE when reading IDE's out or err streams:"+ioe);
421                     }
422                     stop();
423                 }
424             }
425         }
426         
427         public void stop() {
428             thread = null;
429         }
430         
431     }
432     
433 }
434
Popular Tags