1 /*
   2  * Copyright (c) 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 package utils;
  24 
  25 import common.ToolResults;
  26 
  27 /**
  28  * Results of running the jstat tool Concrete subclasses will detail the jstat
  29  * tool options
  30  */
  31 abstract public class JstatResults extends ToolResults {
  32 
  33     public JstatResults(ToolResults rawResults) {
  34         super(rawResults);
  35     }
  36 
  37     /**
  38      * Gets a string result from the column labeled 'name'
  39      *
  40      * @param name - name of the column
  41      * @return the result
  42      */
  43     public String getStringValue(String name) {
  44         int valueNdx = new StringOfValues(getStdoutLine(0)).getIndex(name);
  45         return new StringOfValues(getStdoutLine(1)).getValue(valueNdx);
  46     }
  47 
  48     /**
  49      * Gets a float result from the column labeled 'name'
  50      *
  51      * @param name - name of the column
  52      * @return the result
  53      */
  54     public float getFloatValue(String name) {
  55         int valueNdx = new StringOfValues(getStdoutLine(0)).getIndex(name);
  56         return Float.valueOf(new StringOfValues(getStdoutLine(1)).getValue(valueNdx));
  57     }
  58 
  59     /**
  60      * Gets an integer result from the column labeled 'name'
  61      *
  62      * @param name - name of the column
  63      * @return the result
  64      */
  65     public int getIntValue(String name) {
  66         int valueNdx = new StringOfValues(getStdoutLine(0)).getIndex(name);
  67         return Integer.valueOf(new StringOfValues(getStdoutLine(1)).getValue(valueNdx));
  68     }
  69 
  70     /**
  71      * Checks if a column with a given name exists
  72      *
  73      * @param name - name of the column
  74      * @return true if the column exist, false otherwise
  75      */
  76     public boolean valueExists(String name) {
  77         return new StringOfValues(getStdoutLine(0)).getIndex(name) != -1;
  78     }
  79 
  80     /**
  81      * Helper function to assert the increase of the GC events between 2
  82      * measurements
  83      *
  84      * @param measurement1 -first measurement
  85      * @param measurement2 -first measurement
  86      */
  87     public static void assertGCEventsIncreased(JstatResults measurement1, JstatResults measurement2) {
  88         assertThat(measurement2.getFloatValue("YGC") > measurement1.getFloatValue("YGC"), "YGC didn't increase between measurements 1 and 2");
  89         boolean increasedFGC =
  90           measurement2.getFloatValue("FGC") > measurement1.getFloatValue("FGC");
  91         boolean increasedCGC =
  92           measurement2.getFloatValue("CGC") > measurement1.getFloatValue("CGC");
  93         assertThat(increasedFGC || increasedCGC, "FGC and CGC both don't increase between measurents 2 and 3");
  94     }
  95 
  96     /**
  97      * Helper function to assert the increase of the GC time between 2
  98      * measurements
  99      *
 100      * @param measurement1 -first measurement
 101      * @param measurement2 -first measurement
 102      */
 103     public static void assertGCTimeIncreased(JstatResults measurement1, JstatResults measurement2) {
 104         assertThat(measurement2.getFloatValue("YGCT") > measurement1.getFloatValue("YGCT"), "YGCT time didn't increase between measurements 1 and 2");
 105         boolean increasedFGCT =
 106         measurement2.getFloatValue("FGCT") > measurement1.getFloatValue("FGCT");
 107         boolean increasedCGCT =
 108         measurement2.getFloatValue("CGCT") > measurement1.getFloatValue("CGCT");
 109         assertThat(increasedFGCT || increasedCGCT, "FGCT or CGCT didn't increase between measurements 2 and 3");
 110         assertThat(measurement2.getFloatValue("GCT") > measurement1.getFloatValue("GCT"), "GCT time didn't increase between measurements 1 and 2");
 111     }
 112 
 113     /**
 114      * Helper function to assert the utilization of the space
 115      *
 116      * @param measurement - measurement results to analyze
 117      * @param targetMemoryUsagePercent -assert that not less than this amount of
 118      * space has been utilized
 119      */
 120     public static void assertSpaceUtilization(JstatResults measurement, float targetMemoryUsagePercent) {
 121 
 122         if (measurement.valueExists("OU")) {
 123             float OC = measurement.getFloatValue("OC");
 124             float OU = measurement.getFloatValue("OU");
 125             assertThat((OU / OC) > targetMemoryUsagePercent, "Old space utilization should be > "
 126                     + (targetMemoryUsagePercent * 100) + "%, actually OU / OC = " + (OU / OC));
 127         }
 128 
 129         if (measurement.valueExists("MU")) {
 130             float MC = measurement.getFloatValue("MC");
 131             float MU = measurement.getFloatValue("MU");
 132             assertThat((MU / MC) > targetMemoryUsagePercent, "Metaspace utilization should be > "
 133                     + (targetMemoryUsagePercent * 100) + "%, actually MU / MC = " + (MU / MC));
 134         }
 135 
 136         if (measurement.valueExists("O")) {
 137             float O = measurement.getFloatValue("O");
 138             assertThat(O > targetMemoryUsagePercent * 100, "Old space utilization should be > "
 139                     + (targetMemoryUsagePercent * 100) + "%, actually O = " + O);
 140         }
 141 
 142         if (measurement.valueExists("M")) {
 143             float M = measurement.getFloatValue("M");
 144             assertThat(M > targetMemoryUsagePercent * 100, "Metaspace utilization should be > "
 145                     + (targetMemoryUsagePercent * 100) + "%, actually M = " + M);
 146         }
 147     }
 148 
 149     private static void assertThat(boolean result, String message) {
 150         if (!result) {
 151             throw new RuntimeException(message);
 152         }
 153     }
 154 
 155 }