author | lana |
Thu, 10 Jun 2010 18:58:31 -0700 | |
changeset 5753 | e0ee3917e318 |
parent 5506 | 202f599c92aa |
child 12198 | 0665533a07d2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4858522 |
|
27 |
* @summary Basic unit test of HotspotClassLoadingMBean.getClassLoadingTime() |
|
28 |
* @author Steve Bohne |
|
29 |
* @build ClassToLoad0 |
|
30 |
*/ |
|
31 |
||
32 |
/* |
|
33 |
* This test is just a sanity check and does not check for the correct value. |
|
34 |
*/ |
|
35 |
||
36 |
import java.io.*; |
|
37 |
import sun.management.*; |
|
38 |
||
39 |
public class GetClassLoadingTime { |
|
40 |
||
41 |
private static HotspotClassLoadingMBean mbean = |
|
403
dc6f4ca08b81
6687508: Update test/sun/management jtreg tests due to sun.management.ManagementFactory class rename
mchung
parents:
2
diff
changeset
|
42 |
(HotspotClassLoadingMBean)ManagementFactoryHelper.getHotspotClassLoadingMBean(); |
2 | 43 |
|
44 |
// Careful with these values. |
|
45 |
private static final long MIN_TIME_FOR_PASS = 1; |
|
46 |
private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE; |
|
47 |
||
48 |
private static boolean trace = false; |
|
49 |
||
50 |
public static void main(String args[]) throws Exception { |
|
51 |
if (args.length > 0 && args[0].equals("trace")) { |
|
52 |
trace = true; |
|
53 |
} |
|
54 |
||
55 |
long time = mbean.getClassLoadingTime(); |
|
56 |
||
57 |
if (trace) { |
|
58 |
System.out.println("Class loading time (ms): " + time); |
|
59 |
} |
|
60 |
||
61 |
if (time < MIN_TIME_FOR_PASS || time > MAX_TIME_FOR_PASS) { |
|
62 |
throw new RuntimeException("Class loading time " + |
|
63 |
"illegal value: " + time + " ms " + |
|
64 |
"(MIN = " + MIN_TIME_FOR_PASS + "; " + |
|
65 |
"MAX = " + MAX_TIME_FOR_PASS + ")"); |
|
66 |
} |
|
67 |
||
68 |
// Load some classes to increase the time |
|
69 |
for (int i = 0; i < 1000; i++) { |
|
70 |
Class.forName("ClassToLoad0", true, new KlassLoader()); |
|
71 |
} |
|
72 |
||
73 |
long time2 = mbean.getClassLoadingTime(); |
|
74 |
long count = mbean.getLoadedClassCount(); |
|
75 |
||
76 |
if (trace) { |
|
77 |
System.out.println("(new count is " + count + ")"); |
|
78 |
System.out.println("Class loading time2 (ms): " + time2); |
|
79 |
} |
|
80 |
||
81 |
if (time2 <= time) { |
|
82 |
throw new RuntimeException("Class loading time " + |
|
83 |
"did not increase when class loaded" + |
|
84 |
"(time = " + time + "; " + |
|
85 |
"time2 = " + time2 + ")"); |
|
86 |
} |
|
87 |
||
88 |
System.out.println("Test passed."); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
// KlassLoader exists to load classes without a parent classloader, |
|
93 |
// so we can avoid delegation and spend lots of time loading the |
|
94 |
// same class over and over, to test the class loading timer. |
|
95 |
class KlassLoader extends ClassLoader { |
|
96 |
static String klassDir=""; |
|
97 |
static int index=0; |
|
98 |
||
99 |
public KlassLoader() { |
|
100 |
super(null); |
|
101 |
} |
|
102 |
||
103 |
protected synchronized Class findClass(String name) |
|
104 |
throws ClassNotFoundException { |
|
105 |
String cname = klassDir |
|
106 |
+ (klassDir == "" ? "" : "/") |
|
107 |
+name.replace('.', '/') |
|
108 |
+".class"; |
|
109 |
||
110 |
FileInputStream in; |
|
111 |
try { |
|
112 |
in=new FileInputStream(cname); |
|
113 |
if (in == null) { |
|
114 |
throw new ClassNotFoundException("getResourceAsStream(" |
|
115 |
+cname+")"); |
|
116 |
} |
|
117 |
} catch(java.io.FileNotFoundException e ) { |
|
118 |
throw new ClassNotFoundException("getResourceAsStream(" |
|
119 |
+cname+") : " |
|
120 |
+e); |
|
121 |
} |
|
122 |
||
123 |
int len; |
|
124 |
byte data[]; |
|
125 |
try { |
|
126 |
len = in.available(); |
|
127 |
data = new byte[len]; |
|
128 |
for (int total = 0; total < data.length; ) { |
|
129 |
total += in.read(data, total, data.length - total); |
|
130 |
} |
|
131 |
} catch (IOException e) { |
|
132 |
throw new ClassNotFoundException(cname, e); |
|
133 |
} finally { |
|
134 |
try { |
|
135 |
in.close(); |
|
136 |
} catch (IOException e) { |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
return defineClass(name, data, 0, data.length); |
|
141 |
} |
|
142 |
} |