Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/HotspotClassLoadingMBean/GetClassLoadingTime.java
38840 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 485852226* @summary Basic unit test of HotspotClassLoadingMBean.getClassLoadingTime()27* @author Steve Bohne28* @build ClassToLoad029* @run main/othervm -XX:+UsePerfData GetClassLoadingTime30*/3132/*33* This test is just a sanity check and does not check for the correct value.34*/3536import java.io.*;37import sun.management.*;3839public class GetClassLoadingTime {4041private static HotspotClassLoadingMBean mbean =42(HotspotClassLoadingMBean)ManagementFactoryHelper.getHotspotClassLoadingMBean();4344// Careful with these values.45private static final long MIN_TIME_FOR_PASS = 1;46private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;4748private static boolean trace = false;4950public static void main(String args[]) throws Exception {51if (args.length > 0 && args[0].equals("trace")) {52trace = true;53}5455long time = mbean.getClassLoadingTime();5657if (trace) {58System.out.println("Class loading time (ms): " + time);59}6061if (time < MIN_TIME_FOR_PASS || time > MAX_TIME_FOR_PASS) {62throw new RuntimeException("Class loading time " +63"illegal value: " + time + " ms " +64"(MIN = " + MIN_TIME_FOR_PASS + "; " +65"MAX = " + MAX_TIME_FOR_PASS + ")");66}6768// Load some classes to increase the time69for (int i = 0; i < 1000; i++) {70Class.forName("ClassToLoad0", true, new KlassLoader());71}7273long time2 = mbean.getClassLoadingTime();7475if (trace) {76System.out.println("Class loading time2 (ms): " + time2);77}7879if (time2 <= time) {80throw new RuntimeException("Class loading time " +81"did not increase when class loaded" +82"(time = " + time + "; " +83"time2 = " + time2 + ")");84}8586System.out.println("Test passed.");87}88}8990// KlassLoader exists to load classes without a parent classloader,91// so we can avoid delegation and spend lots of time loading the92// same class over and over, to test the class loading timer.93class KlassLoader extends ClassLoader {9495public KlassLoader() {96super(null);97}9899protected synchronized Class findClass(String name)100throws ClassNotFoundException {101String cname =102name.replace('.', '/')103+".class";104105FileInputStream in;106try {107in = new FileInputStream(new File(System.getProperty("test.classes", "."), cname));108if (in == null) {109throw new ClassNotFoundException("getResourceAsStream("110+cname+")");111}112} catch(java.io.FileNotFoundException e ) {113throw new ClassNotFoundException("getResourceAsStream("114+cname+") : "115+e);116}117118int len;119byte data[];120try {121len = in.available();122data = new byte[len];123for (int total = 0; total < data.length; ) {124total += in.read(data, total, data.length - total);125}126} catch (IOException e) {127throw new ClassNotFoundException(cname, e);128} finally {129try {130in.close();131} catch (IOException e) {132}133}134135return defineClass(name, data, 0, data.length);136}137}138139140