Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/instrument/ManyMethodsBenchmarkApp.java
41152 views
1
/*
2
* Copyright 2015 Google Inc. All Rights Reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.File;
25
import java.io.FileWriter;
26
import java.io.PrintStream;
27
import java.lang.reflect.InvocationTargetException;
28
import java.lang.reflect.Method;
29
import java.lang.reflect.Type;
30
import java.net.URL;
31
import java.net.URLClassLoader;
32
import java.util.Arrays;
33
import java.util.List;
34
import javax.tools.JavaCompiler;
35
import javax.tools.StandardJavaFileManager;
36
import javax.tools.ToolProvider;
37
38
/**
39
* A manual benchmark of the JVMTI RedefineClasses when a
40
* single class (and its parent) contains many methods.
41
*/
42
public class ManyMethodsBenchmarkApp {
43
// Limit is 64k but we can not put such many as the CP limit is 32k.
44
// In practice, it means a real limit is much lower (less than 22000).
45
static final int METHOD_COUNT = 20000;
46
47
static Class<?> loadClassInNewClassLoader(String className) throws Exception {
48
URL[] urls = { new File(".").toURI().toURL() };
49
URLClassLoader ucl = new URLClassLoader(urls, null);
50
Class<?> klazz = Class.forName(className, true, ucl);
51
return klazz;
52
}
53
54
static void benchmarkClassOperations(String className) throws Exception {
55
Class<?> klazz = loadClassInNewClassLoader(className);
56
57
Method[] methods = klazz.getDeclaredMethods();
58
if (methods.length != METHOD_COUNT) {
59
throw new AssertionError("unexpected method count: " + methods.length +
60
" expected: " + METHOD_COUNT);
61
}
62
63
methods = klazz.getMethods();
64
// returned methods includes those inherited from Object
65
int objectMethodSlop = 100;
66
if (methods.length <= METHOD_COUNT ||
67
methods.length >= METHOD_COUNT + objectMethodSlop) {
68
throw new AssertionError("unexpected method count: " + methods.length);
69
}
70
71
// Invoke methods to make them appear in the constant pool cache
72
Object obj = klazz.newInstance();
73
Object[] args = new Object[0];
74
for (Method m: methods) {
75
try {
76
Class<?>[] types = m.getParameterTypes();
77
String name = m.getName();
78
// System.out.println("method: " + name + "; argno: " + types.length);
79
if (types.length == 0 && name.length() == 2 && name.startsWith("f")) {
80
m.invoke(obj, args);
81
}
82
} catch (InvocationTargetException ex) {
83
ex.printStackTrace();
84
}
85
}
86
}
87
88
public static void main(String[] args) throws Exception {
89
System.out.println("test started: ManyMethodsBenchmarkApp");
90
91
// Create source files with many methods
92
File base = new File("Base.java");
93
try (FileWriter fw = new FileWriter(base)) {
94
fw.write("public class Base {\n");
95
final int L = 10;
96
// Each of the first L methods makes calls to its own chunk of METHOD_COUNT/L methods
97
for (int k = 0; k < L; k++) {
98
fw.write(" public void f" + k + "() {\n");
99
int shift = (k == 0) ? L : 0;
100
for (int i = (k * (METHOD_COUNT/L)) + shift; i < (k + 1) * METHOD_COUNT/L; i++) {
101
fw.write(" f" + i + "();\n");
102
}
103
fw.write(" }\n");
104
}
105
106
// The rest of (METHOD_COUNT - L) methods have empty body
107
for (int i = L; i < METHOD_COUNT; i++) {
108
fw.write(" public static void f" + i + "() {}\n");
109
}
110
fw.write("}\n");
111
}
112
113
// Compile the generated source files.
114
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
115
List<File> files = Arrays.asList(new File[] { base });
116
try (StandardJavaFileManager fileManager =
117
compiler.getStandardFileManager(null, null, null)) {
118
compiler.getTask(null, fileManager, null, null, null,
119
fileManager.getJavaFileObjectsFromFiles(files))
120
.call();
121
}
122
123
benchmarkClassOperations("Base");
124
125
ManyMethodsBenchmarkAgent.instr();
126
127
// Cleanup
128
base.delete();
129
new File("Base.class").delete();
130
if (!ManyMethodsBenchmarkAgent.completed) {
131
throw new Exception("ERROR: ManyMethodsBenchmarkAgent did not complete.");
132
}
133
134
if (ManyMethodsBenchmarkAgent.fail) {
135
throw new Exception("ERROR: ManyMethodsBenchmarkAgent failed.");
136
} else {
137
System.out.println("ManyMethodsBenchmarkAgent succeeded.");
138
}
139
System.out.println("test finished: ManyMethodsBenchmarkApp");
140
}
141
}
142
143