Path: blob/master/test/jdk/sun/management/HotspotClassLoadingMBean/GetClassLoadingTime.java
41152 views
/*1* Copyright (c) 2003, 2015, 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*29* @build ClassToLoad030* @run main/othervm -XX:+UsePerfData GetClassLoadingTime31*/3233/*34* This test is just a sanity check and does not check for the correct value.35*/3637import java.io.*;38import sun.management.*;3940public class GetClassLoadingTime {4142private static HotspotClassLoadingMBean mbean =43(HotspotClassLoadingMBean)ManagementFactoryHelper.getHotspotClassLoadingMBean();4445// Careful with these values.46private static final long MIN_TIME_FOR_PASS = 1;47private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;4849private static boolean trace = false;5051public static void main(String args[]) throws Exception {52if (args.length > 0 && args[0].equals("trace")) {53trace = true;54}5556long time = mbean.getClassLoadingTime();5758if (trace) {59System.out.println("Class loading time (ms): " + time);60}6162if (time < MIN_TIME_FOR_PASS || time > MAX_TIME_FOR_PASS) {63throw new RuntimeException("Class loading time " +64"illegal value: " + time + " ms " +65"(MIN = " + MIN_TIME_FOR_PASS + "; " +66"MAX = " + MAX_TIME_FOR_PASS + ")");67}6869// Load some classes to increase the time70for (int i = 0; i < 1000; i++) {71Class.forName("ClassToLoad0", true, new KlassLoader());72}7374long time2 = mbean.getClassLoadingTime();7576if (trace) {77System.out.println("Class loading time2 (ms): " + time2);78}7980if (time2 <= time) {81throw new RuntimeException("Class loading time " +82"did not increase when class loaded" +83"(time = " + time + "; " +84"time2 = " + time2 + ")");85}8687System.out.println("Test passed.");88}89}9091// KlassLoader exists to load classes without a parent classloader,92// so we can avoid delegation and spend lots of time loading the93// same class over and over, to test the class loading timer.94class KlassLoader extends ClassLoader {9596public KlassLoader() {97super(null);98}99100protected synchronized Class findClass(String name)101throws ClassNotFoundException {102String cname =103name.replace('.', '/')104+".class";105106FileInputStream in;107try {108in = new FileInputStream(new File(System.getProperty("test.classes", "."), cname));109if (in == null) {110throw new ClassNotFoundException("getResourceAsStream("111+cname+")");112}113} catch(java.io.FileNotFoundException e ) {114throw new ClassNotFoundException("getResourceAsStream("115+cname+") : "116+e);117}118119int len;120byte data[];121try {122len = in.available();123data = new byte[len];124for (int total = 0; total < data.length; ) {125total += in.read(data, total, data.length - total);126}127} catch (IOException e) {128throw new ClassNotFoundException(cname, e);129} finally {130try {131in.close();132} catch (IOException e) {133}134}135136return defineClass(name, data, 0, data.length);137}138}139140141