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