1 19 20 25 package org.netbeans.core.modules; 26 27 import java.io.*; 28 import java.lang.reflect.Method ; 29 import java.text.DecimalFormat ; 30 import java.util.*; 31 import java.util.zip.*; 32 import java.util.jar.*; 33 34 import org.w3c.dom.*; 35 import org.apache.xml.serialize.*; 36 37 import org.netbeans.core.NbTopManager; 38 import org.netbeans.performance.Benchmark; 39 import org.openide.xml.XMLUtil; 40 41 47 55 61 80 82 86 94 public class ModuleInitTest extends Benchmark { 95 96 public static void main(String [] args) { 97 simpleRun(ModuleInitTest.class); 98 } 99 100 private static Map[] parseParams(String [] ps) { 101 Map[] m = new Map[ps.length]; 102 for (int i = 0; i < ps.length; i++) { 103 m[i] = new HashMap(); StringTokenizer tok = new StringTokenizer(ps[i], ","); 105 while (tok.hasMoreTokens()) { 106 String kv = tok.nextToken(); 107 int x = kv.indexOf('='); 108 String k = kv.substring(0, x); 109 String v = kv.substring(x + 1); 110 try { 111 m[i].put(k, new Integer (v)); 112 } catch (NumberFormatException nfe) { 113 m[i].put(k, v); 114 } 115 } 116 } 117 return m; 118 } 119 120 public ModuleInitTest(String name) { 121 super(name, parseParams(new String [] { 122 "modules=0,jarSize=200,layerSize=23", 124 "modules=10,jarSize=200,layerSize=23", 125 "modules=100,jarSize=200,layerSize=23", 126 "modules=1000,jarSize=200,layerSize=23", 127 133 })); 134 } 135 136 private static File getTmpDir() { 137 File ramdisk = new File("/dev/shm"); 138 if (ramdisk.isDirectory() && ramdisk.canWrite()) { 139 return ramdisk; 140 } else { 141 return new File(System.getProperty("java.io.tmpdir")); 142 } 143 } 144 private File topdir = new File(getTmpDir(), "ModuleInitTest"); 145 private File homedir = new File(topdir, "home"); 146 private File skeldir = new File(topdir, "skeluser"); 147 private File userdir = new File(topdir, "user"); 148 private Map lastParams = null; 149 150 protected void setUp() throws Exception { 151 Map params = (Map)getArgument(); 152 if (!params.equals(lastParams)) { 153 if (homedir.exists()) { 155 deleteRec(homedir); 156 } 157 File mods = new File(homedir, "modules"); 158 File amods = new File(mods, "autoload"); 159 amods.mkdirs(); 160 File emods = new File(mods, "eager"); 161 emods.mkdirs(); 162 createModules(params, mods, amods, emods); 163 new File(homedir, "system").mkdirs(); 164 if (skeldir.exists()) { 166 deleteRec(skeldir); 167 } 168 runNB(homedir, skeldir, false, params); 169 lastParams = params; 170 } 171 if (userdir.exists()) { 174 deleteRec(userdir); 175 } 176 copyRec(skeldir, userdir); 177 } 178 protected void tearDown() throws Exception { 179 } 180 181 private static void deleteRec(File x) throws IOException { 182 File[] kids = x.listFiles(); 183 if (kids != null) { 184 for (int i = 0; i < kids.length; i++) { 185 deleteRec(kids[i]); 186 } 187 } 188 if (!x.delete()) throw new IOException("Could not delete: " + x); 189 } 190 private static void copyStream(InputStream is, OutputStream os) throws IOException { 191 try { 192 byte[] b = new byte[4096]; 193 int i; 194 while ((i = is.read(b)) != -1) { 195 os.write(b, 0, i); 196 } 197 } finally { 198 is.close(); 199 } 200 } 201 private static void copyRec(File x, File y) throws IOException { 202 if (x.isDirectory()) { 203 if (!y.mkdirs()) throw new IOException("Could not mkdir: " + y); 204 String [] kids = x.list(); 205 if (kids == null) throw new IOException("Could not list: " + x); 206 for (int i = 0; i < kids.length; i++) { 207 copyRec(new File(x, kids[i]), new File(y, kids[i])); 208 } 209 } else { 210 y.getParentFile().mkdirs(); 211 OutputStream os = new FileOutputStream(y); 212 try { 213 copyStream(new FileInputStream(x), os); 214 } finally { 215 os.close(); 216 } 217 } 218 } 219 220 private static final int cyclesize = 8; 223 private static final String [] names = {"aut1", "prv1", "reg1", "aut2", "reg2", "reg3", "dis1", "eag1"}; 225 private static final int[] types = {1, 1, 0, 1, 0, 0, 0, 2}; 227 private static final int[][] intradeps = {{}, {}, {0}, {}, {0, 3}, {3}, {3}, {4, 5}}; 229 private static final int[][] interdeps = {{0}, {}, {2}, {}, {}, {}, {6}, {5}}; 231 private static final boolean[] enabled = {true, true, true, true, true, true, false, true}; 233 private static final String [][] provides = {{}, {"tok#"}, {}, {}, {}, {}, {}, {}}; 235 private static final String [][] requires = {{}, {}, {"tok#"}, {}, {}, {}, {}, {}}; 237 static { 242 if (names.length != cyclesize || 243 types.length != cyclesize || 244 intradeps.length != cyclesize || 245 interdeps.length != cyclesize || 246 enabled.length != cyclesize || 247 provides.length != cyclesize || 248 requires.length != cyclesize) { 249 throw new Error (); 250 } 251 } 252 253 private void createModules(Map params, File mods, File amods, File emods) throws IOException { 254 int size = ((Integer )params.get("modules")).intValue(); 255 int jarSize = ((Integer )params.get("jarSize")).intValue(); 256 int layerSize = ((Integer )params.get("layerSize")).intValue(); 257 File[] moddirs = {mods, amods, emods}; for (int i = 0; i < size; i++) { 259 int which = i % cyclesize; 260 int cycle = i / cyclesize; 261 String cycleS = Integer.toString(cycle); 262 while (cycleS.length() < 3) cycleS = "0" + cycleS; 264 Manifest mani = new Manifest(); 265 Attributes attr = mani.getMainAttributes(); 266 attr.putValue("Manifest-Version", "1.0"); 267 String nameBase = names[which] + "_" + cycleS; 268 String name = "com.testdomain." + nameBase; 269 String nameSlashes = name.replace('.', '/'); 270 attr.putValue("OpenIDE-Module", name + "/1"); 271 attr.putValue("OpenIDE-Module-IDE-Dependencies", "IDE/1 > 2.2"); 273 attr.putValue("OpenIDE-Module-Specification-Version", "1.0"); 274 StringBuffer deps = null; 275 for (int j = 0; j < intradeps[which].length; j++) { 276 if (deps == null) { 277 deps = new StringBuffer (1000); 278 } else { 279 deps.append(", "); 280 } 281 deps.append("com.testdomain." + names[intradeps[which][j]]); 282 deps.append('_'); 283 deps.append(cycleS); 284 deps.append("/1 > 1.0"); 285 } 286 if (cycle > 0) { 287 String oldCycleS = Integer.toString(cycle - 1); 288 while (oldCycleS.length() < 3) oldCycleS = "0" + oldCycleS; 289 for (int j = 0; j < interdeps[which].length; j++) { 290 if (deps == null) { 291 deps = new StringBuffer (1000); 292 } else { 293 deps.append(", "); 294 } 295 deps.append("com.testdomain." + names[interdeps[which][j]]); 296 deps.append('_'); 297 deps.append(oldCycleS); 298 deps.append("/1 > 1.0"); 299 } 300 } 301 if (!enabled[which]) { 302 if (deps == null) { 303 deps = new StringBuffer (1000); 304 } else { 305 deps.append(", "); 306 } 307 deps.append("honest.man.in.washington"); 309 } 310 if (deps != null) { 311 attr.putValue("OpenIDE-Module-Module-Dependencies", deps.toString()); 312 } 313 if (provides[which].length > 0) { 314 StringBuffer buf = new StringBuffer (100); 315 for (int j = 0; j < provides[which].length; j++) { 316 if (j > 0) { 317 buf.append(", "); 318 } 319 String tok = provides[which][j]; 320 if (tok.endsWith("#")) { 321 tok = tok.substring(0, tok.length() - 1) + "_" + cycleS; 322 } 323 buf.append("com.testdomain." + tok); 324 } 325 attr.putValue("OpenIDE-Module-Provides", buf.toString()); 326 } 327 if (requires[which].length > 0) { 328 StringBuffer buf = new StringBuffer (100); 329 for (int j = 0; j < requires[which].length; j++) { 330 if (j > 0) { 331 buf.append(", "); 332 } 333 String tok = requires[which][j]; 334 if (tok.endsWith("#")) { 335 tok = tok.substring(0, tok.length() - 1) + "_" + cycleS; 336 } 337 buf.append("com.testdomain." + tok); 338 } 339 attr.putValue("OpenIDE-Module-Requires", buf.toString()); 340 } 341 Map contents = new TreeMap(); String locb = nameSlashes + "/Bundle.properties"; 344 contents.put(locb, ("OpenIDE-Module-Name=Module #" + i + "\n").getBytes()); 345 attr.putValue("OpenIDE-Module-Localizing-Bundle", locb); 346 contents.put(nameSlashes + "/foo", "stuff here\n".getBytes()); 347 contents.put(nameSlashes + "/subdir/foo", "more stuff here\n".getBytes()); 348 for (int j = 0; j < jarSize; j++) { 349 contents.put(nameSlashes + "/sub/subdir/file" + j, new byte[j]); 350 } 351 Map layer = new TreeMap(); int start = i * layerSize; 355 int end = (i + 1) * layerSize; 356 for (int j = start; j < end; j++) { 357 String filename = "file" + j + ".txt"; 358 int bit = 0; 359 int x = j; 360 while (x > 0) { 361 if (x % 2 == 1) { 362 filename = "dir" + bit + "/" + filename; 363 } 364 bit++; 365 x /= 2; 366 } 367 layer.put(filename, (j % 2 == 0) ? ("Contents #" + j + "\n").getBytes() : null); 368 } 369 ByteArrayOutputStream baos = new ByteArrayOutputStream(layer.size() * 100 + 1); 370 Document doc = XMLUtil.createDocument("filesystem", null, "-//NetBeans//DTD Filesystem 1.1//EN", "http://www.netbeans.org/dtds/filesystem-1_1.dtd"); 371 doc.getDocumentElement().appendChild(doc.createComment(" Layer filenames for module #" + i + ": " + layer.keySet() + " ")); 372 Iterator it = layer.entrySet().iterator(); 373 while (it.hasNext()) { 374 Map.Entry e = (Map.Entry)it.next(); 375 String filename = (String )e.getKey(); 376 byte[] filebytes = (byte[])e.getValue(); 377 Element el = doc.getDocumentElement(); 378 StringTokenizer tok = new StringTokenizer(filename, "/"); 379 while (tok.hasMoreTokens()) { 380 String piece = tok.nextToken(); 381 if (tok.hasMoreTokens()) { 382 Element child = null; 384 NodeList kids = el.getChildNodes(); 385 for (int j = 0; j < kids.getLength(); j++) { 386 Node n = kids.item(j); 387 if (!(n instanceof Element)) continue; 388 Element kid = (Element)n; 389 if (!kid.getNodeName().equals("folder")) continue; 390 if (kid.getAttribute("name").equals(piece)) { 391 child = kid; 392 break; 393 } 394 } 395 if (child == null) { 396 child = doc.createElement("folder"); 397 child.setAttribute("name", piece); 398 if (Math.abs(filename.hashCode()) % 5 == 0) { 400 Element a = doc.createElement("attr"); 401 a.setAttribute("name", "SystemFileSystem.localizingBundle"); 402 a.setAttribute("stringvalue", name + ".Bundle"); 403 child.appendChild(a); 404 a = doc.createElement("attr"); 405 a.setAttribute("name", "SystemFileSystem.icon"); 406 a.setAttribute("urlvalue", "nbresloc:/" + nameSlashes + "/resources/" + piece + ".gif"); 407 child.appendChild(a); 408 } 409 el.appendChild(child); 410 } 411 el = child; 412 } else { 413 Element child = doc.createElement("file"); 416 child.setAttribute("name", piece); 417 if (filebytes != null) { 418 String contentsName = "resources/layerfile" + Integer.toHexString(filename.hashCode()); 419 contents.put(nameSlashes + "/" + contentsName, filebytes); 420 child.setAttribute("url", contentsName); 421 } 422 if (Math.abs(filename.hashCode()) % 3 == 0) { 424 Element a = doc.createElement("attr"); 425 a.setAttribute("name", "instanceOf"); 426 a.setAttribute("stringvalue", name + ".Whatever"); 427 child.appendChild(a); 428 } 429 el.appendChild(child); 430 } 431 } 432 } 433 XMLSerializer ser = new XMLSerializer(baos, new OutputFormat(doc, "UTF-8", true)); 434 ser.serialize(doc); 435 String layerName = nameSlashes + "/layer.xml"; 436 contents.put(layerName, baos.toByteArray()); 437 attr.putValue("OpenIDE-Module-Layer", layerName); 438 OutputStream os = new FileOutputStream(new File(moddirs[types[which]], nameBase + ".jar")); 439 try { 440 JarOutputStream jos = new JarOutputStream(os, mani); 441 Set addedDirs = new HashSet(1000); it = contents.entrySet().iterator(); 443 while (it.hasNext()) { 444 Map.Entry e = (Map.Entry)it.next(); 445 String filename = (String )e.getKey(); 446 byte[] filebytes = (byte[])e.getValue(); 447 String dircheck = filename; 448 while (true) { 449 int idx = dircheck.lastIndexOf('/'); 450 if (idx == -1) break; 451 dircheck = dircheck.substring(0, idx); 452 if (!addedDirs.add(dircheck)) break; 453 JarEntry je = new JarEntry(dircheck + "/"); 454 je.setMethod(ZipEntry.STORED); 455 je.setSize(0); 456 je.setCompressedSize(0); 457 je.setCrc(0); 458 jos.putNextEntry(je); 459 } 460 JarEntry je = new JarEntry(filename); 461 je.setMethod(ZipEntry.DEFLATED); 462 jos.putNextEntry(je); 464 jos.write(filebytes); 465 } 466 jos.close(); 467 } finally { 468 os.close(); 469 } 470 } 471 } 472 473 public void testInitModuleSystem() throws Exception { 474 int count = getIterationCount(); 475 for (int i = 0; i < count; i++) { 476 runNB(homedir, userdir, true, (Map)getArgument()); 477 } 478 } 479 480 private String cp = refinecp(System.getProperty("java.class.path")); 481 482 private void runNB(File homedir, File userdir, boolean log, Map params) throws IOException { 483 List cmd = new ArrayList(Arrays.asList(new String [] { 484 "java", 485 "-Xms24m", 486 "-Xmx96m", 487 "-Dorg.openide.TopManager=org.netbeans.core.NonGuiMain", 488 "-Dnetbeans.security.nocheck=true", 489 "-Dnetbeans.home=" + homedir.getAbsolutePath(), 490 "-Dnetbeans.user=" + userdir.getAbsolutePath(), 491 "-Dnetbeans.modules.quiet=true", 492 "-Dlog=" + log, 493 "-classpath", 494 cp, 495 })); 496 Iterator it = params.entrySet().iterator(); 497 while (it.hasNext()) { 498 Map.Entry e = (Map.Entry)it.next(); 499 String key = (String )e.getKey(); 500 if (key.startsWith("-D")) { 501 cmd.add(key + "=" + e.getValue()); 502 } 503 } 504 cmd.add("org.netbeans.core.modules.ModuleInitTest$Main"); 506 Process p = Runtime.getRuntime().exec((String [])cmd.toArray(new String [cmd.size()])); 508 new Copier(p.getInputStream(), System.out).start(); 509 new Copier(p.getErrorStream(), System.err).start(); 510 try { 511 int stat = p.waitFor(); 512 if (stat != 0) { 513 throw new IOException("Command failed (status " + stat + "): " + cmd); 514 } 515 } catch (InterruptedException ie) { 516 throw new IOException(ie.toString()); 517 } 518 } 519 520 522 private static String refinecp(String cp) { 523 StringBuffer b = new StringBuffer (cp.length()); 524 StringTokenizer t = new StringTokenizer(cp, File.pathSeparator); 525 while (t.hasMoreTokens()) { 526 File f = new File(t.nextToken()); 527 if (f.isDirectory()) { 528 if (new File(new File(new File(f, "org"), "openide"), "ErrorManagerTest.java").exists() || 529 new File(new File(new File(f, "org"), "openide"), "ErrorManagerTest.class").exists()) { 530 continue; 532 } 533 } 534 if (b.length() != 0) { 535 b.append(File.pathSeparatorChar); 536 } 537 b.append(f); 538 } 539 return b.toString(); 540 } 541 542 private static final class Copier extends Thread { 543 private final InputStream is; 544 private final PrintStream ps; 545 public Copier(InputStream is, PrintStream ps) { 546 this.is = is; 547 this.ps = ps; 548 } 549 public void run() { 550 try { 551 copyStream(is, ps); 552 } catch (IOException ioe) { 553 ioe.printStackTrace(); 554 } 555 } 556 } 557 558 public static final class Main { 559 public static void main(String [] x) { 560 NbTopManager.get(); 561 if (Boolean.getBoolean("log")) { 562 Runtime r = Runtime.getRuntime(); 563 r.gc(); 564 double megs = (r.totalMemory() - r.freeMemory()) / 1024.0 / 1024.0; 565 System.err.println("Used memory: " + new DecimalFormat ("0.00 Mb").format(megs)); 566 } 567 } 568 } 569 570 } 571 | Popular Tags |