|
| 1 | +/* |
| 2 | + * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package net.sdo; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.MalformedURLException; |
| 10 | +import java.net.URL; |
| 11 | +import java.net.URLClassLoader; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Enumeration; |
| 14 | +import java.util.concurrent.LinkedBlockingQueue; |
| 15 | +import java.util.concurrent.atomic.AtomicLong; |
| 16 | +import java.util.zip.ZipEntry; |
| 17 | +import java.util.zip.ZipException; |
| 18 | +import java.util.zip.ZipFile; |
| 19 | + |
| 20 | +public class ClassLoaderTest implements Runnable { |
| 21 | + |
| 22 | + private static String[] classNames; |
| 23 | + private static AtomicLong time = new AtomicLong(); |
| 24 | + private static URL[] urls; |
| 25 | + private static URLClassLoader ucl; |
| 26 | + |
| 27 | + private static boolean increaseStackDepth = false; |
| 28 | + |
| 29 | + private static void loadClasses(String[] classNames, int count, ClassLoader cl) throws ClassNotFoundException { |
| 30 | + for (int i = 0; i < count; i++) { |
| 31 | + String name = classNames[i]; |
| 32 | + cl.loadClass(name); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static void initializeFromJar(String fileName) throws ZipException, IOException { |
| 37 | + ZipFile zf = new ZipFile(new File(fileName)); |
| 38 | + Enumeration e = zf.entries(); |
| 39 | + ArrayList<String> al = new ArrayList<String>(); |
| 40 | + while (e.hasMoreElements()) { |
| 41 | + ZipEntry ze = (ZipEntry) e.nextElement(); |
| 42 | + String s = ze.getName(); |
| 43 | + if (s.endsWith(".class")) { |
| 44 | + al.add(s); |
| 45 | + } |
| 46 | + } |
| 47 | + String[] names = al.toArray(new String[al.size()]); |
| 48 | + classNames = new String[names.length]; |
| 49 | + for (int i = 0; i < names.length; i++) { |
| 50 | + classNames[i] = names[i].substring(0, names[i].length() - 6).replaceAll("/", "."); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void a1() { a2(); } |
| 55 | + public void a2() { a3(); } |
| 56 | + public void a3() { a4(); } |
| 57 | + public void a4() { a5(); } |
| 58 | + public void a5() { a6(); } |
| 59 | + public void a6() { a7(); } |
| 60 | + public void a7() { a8(); } |
| 61 | + public void a8() { a9(); } |
| 62 | + public void a9() { a10(); } |
| 63 | + public void a10() { a11(); } |
| 64 | + public void a11() { a12(); } |
| 65 | + public void a12() { a13(); } |
| 66 | + public void a13() { a14(); } |
| 67 | + public void a14() { a15(); } |
| 68 | + public void a15() { a16(); } |
| 69 | + public void a16() { a17(); } |
| 70 | + public void a17() { a18(); } |
| 71 | + public void a18() { a19(); } |
| 72 | + public void a19() { a20(); } |
| 73 | + public void a20() { a21(); } |
| 74 | + public void a21() { a22(); } |
| 75 | + public void a22() { a23(); } |
| 76 | + public void a23() { a24(); } |
| 77 | + public void a24() { a25(); } |
| 78 | + public void a25() { a26(); } |
| 79 | + public void a26() { a27(); } |
| 80 | + public void a27() { a28(); } |
| 81 | + public void a28() { a29(); } |
| 82 | + public void a29() { a30(); } |
| 83 | + public void a30() { a31(); } |
| 84 | + public void a31() { a32(); } |
| 85 | + public void a32() { a33(); } |
| 86 | + public void a33() { a34(); } |
| 87 | + public void a34() { a35(); } |
| 88 | + public void a35() { a36(); } |
| 89 | + public void a36() { a37(); } |
| 90 | + public void a37() { a38(); } |
| 91 | + public void a38() { a39(); } |
| 92 | + public void a39() { a40(); } |
| 93 | + public void a40() { |
| 94 | + try { |
| 95 | + loadClasses(classNames, classNames.length, new URLClassLoader(urls)); |
| 96 | + } catch (ClassNotFoundException ex) { |
| 97 | + System.err.println("Can't define class "); |
| 98 | + ex.printStackTrace(); |
| 99 | + System.exit(-1); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void run() { |
| 104 | + try { |
| 105 | + if (increaseStackDepth) { |
| 106 | + a1(); |
| 107 | + } |
| 108 | + else { |
| 109 | + loadClasses(classNames, classNames.length, new URLClassLoader(urls)); |
| 110 | + } |
| 111 | + } catch (Exception e) { |
| 112 | + System.err.println("Can't define class "); |
| 113 | + e.printStackTrace(); |
| 114 | + System.exit(-1); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, ZipException, IOException { |
| 119 | + initializeFromJar(args[0]); |
| 120 | + int nThreads = Integer.parseInt(args[1]); |
| 121 | + increaseStackDepth = Boolean.parseBoolean(args[2]); |
| 122 | + urls = new URL[]{new File(args[0]).toURL()}; |
| 123 | + for (int i = 0; i < 10; i++) { |
| 124 | + ucl = new URLClassLoader(urls); |
| 125 | + loadClasses(classNames, classNames.length, ucl); |
| 126 | + } |
| 127 | + LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); |
| 128 | + PausingThreadPoolExecutor tpe = new PausingThreadPoolExecutor(nThreads, queue); |
| 129 | + tpe.prestartAllCoreThreads(); |
| 130 | + for (int j = 0; j < 100; j++) { |
| 131 | + tpe.pause(); |
| 132 | + ucl = new URLClassLoader(urls); |
| 133 | + for (int i = 0; i < nThreads; i++) { |
| 134 | + tpe.addTask(new ClassLoaderTest()); |
| 135 | + } |
| 136 | + long then = System.currentTimeMillis(); |
| 137 | + tpe.resume(); |
| 138 | + long now = System.currentTimeMillis(); |
| 139 | + time.getAndAdd(now - then); |
| 140 | + } |
| 141 | + tpe.shutdown(); |
| 142 | + System.out.println("Threads: " + nThreads + ", Classes: " + classNames.length + ", Time: " + time); |
| 143 | + } |
| 144 | +} |
0 commit comments