content: /*
   * Copyright 1999-2018 Alibaba Group Holding Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alibaba.druid;
  
  import com.alibaba.druid.util.Utils;
  
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import java.lang.management.ManagementFactory;
  
  public class TestUtil {
      public static long getYoungGC() {
          try {
              MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
              ObjectName objectName;
              if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=ParNew"))) {
                  objectName = new ObjectName("java.lang:type=GarbageCollector,name=ParNew");
              } else if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=Copy"))) {
                  objectName = new ObjectName("java.lang:type=GarbageCollector,name=Copy");
              } else {
                  objectName = new ObjectName("java.lang:type=GarbageCollector,name=PS Scavenge");
              }
  
              return (Long) mbeanServer.getAttribute(objectName, "CollectionCount");
          } catch (Exception e) {
              throw new RuntimeException("error");
          }
      }
  
      public static long getFullGC() {
          try {
              MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
              ObjectName objectName;
  
              if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"))) {
                  objectName = new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep");
              } else if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=MarkSweepCompact"))) {
                  objectName = new ObjectName("java.lang:type=GarbageCollector,name=MarkSweepCompact");
              } else {
                  objectName = new ObjectName("java.lang:type=GarbageCollector,name=PS MarkSweep");
              }
  
              return (Long) mbeanServer.getAttribute(objectName, "CollectionCount");
          } catch (Exception e) {
              throw new RuntimeException("error");
          }
      }
  
      public static String getResource(String path) {
          ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
          try (Reader reader = new InputStreamReader(
                  contextClassLoader
                          .getResourceAsStream(path), "UTF-8")
          ) {
              return Utils.read(reader);
          } catch (IOException ignored) {
              return null;
          }
      }
  }
  