1 /*
   2  * Copyright (c) 2003, 2015, 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  * @modules java.management/sun.management
  30  * @build ClassToLoad0
  31  * @run main/othervm -XX:+UsePerfData GetClassLoadingTime
  32  */
  33 
  34 /*
  35  * This test is just a sanity check and does not check for the correct value.
  36  */
  37 
  38 import java.io.*;
  39 import sun.management.*;
  40 
  41 public class GetClassLoadingTime {
  42 
  43     private static HotspotClassLoadingMBean mbean =
  44         (HotspotClassLoadingMBean)ManagementFactoryHelper.getHotspotClassLoadingMBean();
  45 
  46     // Careful with these values.
  47     private static final long MIN_TIME_FOR_PASS = 1;
  48     private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;
  49 
  50     private static boolean trace = false;
  51 
  52     public static void main(String args[]) throws Exception {
  53         if (args.length > 0 && args[0].equals("trace")) {
  54             trace = true;
  55         }
  56 
  57         long time = mbean.getClassLoadingTime();
  58 
  59         if (trace) {
  60             System.out.println("Class loading time (ms): " + time);
  61         }
  62 
  63         if (time < MIN_TIME_FOR_PASS || time > MAX_TIME_FOR_PASS) {
  64             throw new RuntimeException("Class loading time " +
  65                                        "illegal value: " + time + " ms " +
  66                                        "(MIN = " + MIN_TIME_FOR_PASS + "; " +
  67                                        "MAX = " + MAX_TIME_FOR_PASS + ")");
  68         }
  69 
  70         // Load some classes to increase the time
  71         for (int i = 0; i < 1000; i++) {
  72             Class.forName("ClassToLoad0", true, new KlassLoader());
  73         }
  74 
  75         long time2 = mbean.getClassLoadingTime();
  76 
  77         if (trace) {
  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 
  97   public KlassLoader() {
  98       super(null);
  99   }
 100 
 101   protected synchronized Class findClass(String name)
 102                         throws ClassNotFoundException {
 103         String cname =
 104             name.replace('.', '/')
 105             +".class";
 106 
 107         FileInputStream in;
 108         try {
 109                 in = new FileInputStream(new File(System.getProperty("test.classes", "."), cname));
 110                 if (in == null) {
 111                         throw new ClassNotFoundException("getResourceAsStream("
 112                                 +cname+")");
 113                 }
 114         } catch(java.io.FileNotFoundException e ) {
 115                 throw new ClassNotFoundException("getResourceAsStream("
 116                         +cname+") : "
 117                         +e);
 118         }
 119 
 120         int len;
 121         byte data[];
 122         try {
 123                 len = in.available();
 124                 data = new byte[len];
 125                 for (int total = 0; total < data.length; ) {
 126                         total += in.read(data, total, data.length - total);
 127                 }
 128         } catch (IOException e) {
 129                 throw new ClassNotFoundException(cname, e);
 130         } finally {
 131                 try {
 132                         in.close();
 133                 } catch (IOException e) {
 134                 }
 135         }
 136 
 137         return defineClass(name, data, 0, data.length);
 138   }
 139 }