1 19 20 package memory; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.io.LineNumberReader ; 28 29 import java.util.StringTokenizer ; 30 31 39 public class MeasureBaselineMemoryFootprint extends org.netbeans.junit.NbPerformanceTestCase{ 40 41 42 private static String platform; 43 44 45 private static long pid; 46 47 48 private static String PS_OUTPUT_FILENAME = "psOutput.txt"; 49 50 private static final String UNIX = "unix"; 51 private static final String WINDOWS = "windows"; 52 private static final String UNKNOWN = "unknown"; 53 54 private static final String [][] SUPPORTED_PLATFORMS = { 55 {"Linux,i386",UNIX}, 56 {"SunOS,sparc",UNIX}, 57 {"Windows_NT,x86",WINDOWS}, 58 {"Windows_2000,x86",WINDOWS}, 59 {"Windows_XP,x86",WINDOWS}, 60 {"Windows_95,x86",WINDOWS}, 61 {"Windows_98,x86",WINDOWS}, 62 {"Windows_Me,x86",WINDOWS} 63 }; 64 65 68 public MeasureBaselineMemoryFootprint(String testName) { 69 super(testName); 70 } 71 72 73 public void testMemoryFootprintAfterStart(){ 74 long memory; 75 try { 76 memory = getMemoryConsumption(); 77 78 if(memory>0){ 79 reportPerformance("Memory Consumption After Start", memory , "kB", 1); 80 }else 81 fail("Measured value = "+memory+"kB - it's wrong value!"); 82 83 }catch(Exception exc){ 84 exc.printStackTrace(getLog()); 85 fail("Exception rises during measurement : "+exc.toString()); 86 } 87 } 88 89 90 96 private long getMemoryConsumption() throws IOException { 97 String workdir = System.getProperty("xtest.tmpdir"); 98 99 File ideRunning; 100 log("Start"); 101 102 if (workdir!=null) { 103 log("Workdir {"+workdir+"}."); 104 workdir = workdir.substring(0,workdir.lastIndexOf(File.separator)); 105 106 ideRunning = new File (workdir,"ide.pid"); 108 log("Looking for file {"+ideRunning.getAbsolutePath()+"}."); 109 110 if (ideRunning.exists()) { 111 log("PID file exists."); 112 try { 113 LineNumberReader reader = new LineNumberReader (new FileReader (ideRunning)); 114 String line = reader.readLine(); 115 if (line != null) { 116 try { 117 pid = Long.parseLong(line); 118 log("Measure memory footprint of process with PID="+pid); 119 long measuredMemory = measureMemory(); 120 Thread.sleep(2000); 121 return measuredMemory; 122 } catch (NumberFormatException nfe) { 123 nfe.printStackTrace(getLog()); 124 fail("Cannot parse PID written in the ide.pid file: "+line+"- cannot measure."); 125 } 126 } 127 } catch (IOException ioe) { 128 ioe.printStackTrace(getLog()); 129 fail("IOException when reading PID from ide.pid file - cannot measure"); 130 } catch (Exception e) { 131 e.printStackTrace(getLog()); 132 fail("Exception when trying to measure IDE's used memory"); 133 } 134 } else { 135 fail("Cannot find file containing PID of running IDE ("+ideRunning.getAbsolutePath()+") - cannot measure"); 136 } 137 } else { 138 fail("xtest.workdir property is not specified - cannot measure"); 139 } 140 fail("Wrong state"); 141 return 0; 142 } 143 144 145 149 private long measureMemory(){ 150 platform = getPlatform(); 151 152 log("Platform="+platform); 153 154 if (platform.equals(UNIX)) 155 return psOnUnix(); 156 else if (platform.equals(WINDOWS)) 157 return psOnWindows(); 158 else 159 fail("Unsupported platform!"); 160 161 return 0; 162 } 163 164 168 private void executePsCommand(String psCommand){ 169 log("Ecexute command: ["+psCommand+"]."); 170 171 try { 172 Process ps = Runtime.getRuntime().exec(psCommand); 173 174 StringBuffer buffer = new StringBuffer (); 175 BufferedReader dataInput = new BufferedReader (new InputStreamReader (ps.getInputStream())); 176 String line; 177 178 while ((line = dataInput.readLine()) != null) { 179 buffer.append(line); 180 buffer.append('\n'); 181 } 182 183 getLog(PS_OUTPUT_FILENAME).print(buffer.toString()); 184 ps.waitFor(); 185 186 log("ps command exit value = "+ps.exitValue()); 187 } catch (InterruptedException ie) { 188 ie.printStackTrace(getLog()); 189 log("InterruptedException when ps :"+ie.toString()); 190 } catch (IOException ioe){ 191 ioe.printStackTrace(getLog()); 192 log("None output from command ps, exception arise "+ioe.toString()); 193 } 194 } 195 196 197 205 private long psOnUnix(){ 206 long returnValue = 0; 207 208 executePsCommand("ps -A -o pid,comm,rss"); 209 returnValue = parsePsFile(); 210 211 return returnValue; 212 } 213 214 222 private long psOnWindows(){ 223 String xtestHome = System.getProperty("xtest.tmpdir"); 224 if (xtestHome != null) { 225 File psFile = new File (xtestHome,"pslist.exe"); 226 String psPath = psFile.getAbsolutePath(); 227 String psCommand = psPath; 228 executePsCommand(psCommand); 229 return parsePsFile(); 230 } else { 231 fail("xtest.home system property not set - cannot find ps distributed with XTest on windows"); 232 } 233 return 0; 234 } 235 236 237 244 private long parsePsFile(){ 245 String workDirPath = ""; 246 247 try { 248 workDirPath = getWorkDir().getAbsolutePath(); 249 }catch(IOException ioe){ 250 ioe.printStackTrace(getLog()); 251 fail("It isn't possible to get work directory, arise exception :"+ioe.toString()); 252 } 253 254 try { 255 File psOutput = new File (workDirPath, PS_OUTPUT_FILENAME); 256 log("Parse file "+psOutput.getAbsolutePath()); 257 258 BufferedReader reader = new BufferedReader (new FileReader (psOutput)); 259 String line; 260 261 while((line = reader.readLine()) != null){ 262 log("\t Line=["+line+"]"); 263 long memory = getMemory(line); 264 if(memory!=0) 265 return memory; 266 } 267 268 270 } catch(IOException ioe){ 271 ioe.printStackTrace(getLog()); 272 log("None output from ps command, arise exception :"+ioe.toString()); 273 } 274 275 return 0; 276 } 277 278 279 284 private long getMemory(String line){ 285 StringTokenizer st = new StringTokenizer (line); 286 String line_pid,line_mem; 287 long memory = 0; 288 long ppid; 289 290 if(line.length()>0){ 291 if (platform.equals(UNIX)) { 292 line_pid = st.nextToken(); 293 try { 294 ppid = Long.parseLong(line_pid); 295 }catch(NumberFormatException exc){ 296 return 0; 297 } 298 299 log("\t proces pid="+ppid + " looking for pid="+pid); 300 301 if(pid == ppid){ 302 st.nextToken(); 303 line_mem = st.nextToken(); 304 memory = Long.parseLong(line_mem); 305 } 306 307 } else if (platform.equals(WINDOWS)) { 308 st.nextToken(); 309 line_pid = st.nextToken(); 310 try { 311 ppid = Long.parseLong(line_pid); 312 }catch(NumberFormatException exc){ 313 return 0; 314 } 315 316 log("\t proces pid="+ppid + " looking for pid="+pid); 317 if(pid == ppid){ 318 for(int i=0;i<3;i++) 319 st.nextToken(); 320 line_mem = st.nextToken(); 321 322 memory = Long.parseLong(line_mem); 323 } 324 } else { 325 fail("Unsupported platform!"); 326 } 327 328 } 329 log("Memory="+memory); 330 return memory; 331 } 332 333 337 private static String getPlatform() { 338 339 String platformString=(System.getProperty("os.name","")+","+ 340 343 System.getProperty("os.arch","")).replace(' ','_'); 344 for (int i=0; i<SUPPORTED_PLATFORMS.length; i++) { 345 if (platformString.equalsIgnoreCase(SUPPORTED_PLATFORMS[i][0])) { 346 return SUPPORTED_PLATFORMS[i][1]; 347 } 348 } 349 return UNKNOWN; 350 } 351 352 353 } 354 | Popular Tags |