Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/HotspotClassLoadingMBean/GetClassInitializationTime.java
38840 views
/*1* Copyright (c) 2003, 2008, 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.getClassInitializationTime()27* @author Steve Bohne28*/2930/*31* This test is just a sanity check and does not check for the correct value.32*/3334import sun.management.*;3536public class GetClassInitializationTime {3738private static HotspotClassLoadingMBean mbean =39(HotspotClassLoadingMBean)ManagementFactoryHelper.getHotspotClassLoadingMBean();4041// Careful with these values.42private static final long MIN_TIME_FOR_PASS = 1;43private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;4445private static boolean trace = false;4647public static void main(String args[]) throws Exception {48if (args.length > 0 && args[0].equals("trace")) {49trace = true;50}5152long time = mbean.getClassInitializationTime();5354if (trace) {55System.out.println("Class initialization time (ms): " + time);56}5758if (time < MIN_TIME_FOR_PASS || time > MAX_TIME_FOR_PASS) {59throw new RuntimeException("Class initialization time " +60"illegal value: " + time + " ms " +61"(MIN = " + MIN_TIME_FOR_PASS + "; " +62"MAX = " + MAX_TIME_FOR_PASS + ")");63}6465// Load a class and make sure the time increases66Class.forName("ClassToInitialize");6768long time2 = mbean.getClassInitializationTime();6970if (trace) {71System.out.println("Class initialization time2 (ms): " + time2);72}7374if (time2 <= time) {75throw new RuntimeException("Class initialization time " +76"did not increase when class loaded " +77"(time = " + time + "; " +78"time2 = " + time2 + ")");79}8081System.out.println("Test passed.");82}83}8485// This class should just cause the initialization timer to run86class ClassToInitialize {87static {88try {89Thread.sleep(1000);90} catch (InterruptedException ie) {91// do nothing92}93}94}959697