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