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         assertThat(measurement2.getFloatValue("FGC") > measurement1.getFloatValue("FGC"), "FGC didn't increase between measurements 2 and 3");
  90     }
  91 
  92     /**
  93      * Helper function to assert the increase of the GC time between 2
  94      * measurements
  95      *
  96      * @param measurement1 -first measurement
  97      * @param measurement2 -first measurement
  98      */
  99     public static void assertGCTimeIncreased(JstatResults measurement1, JstatResults measurement2) {
 100         assertThat(measurement2.getFloatValue("YGCT") > measurement1.getFloatValue("YGCT"), "YGCT time didn't increase between measurements 1 and 2");
 101         assertThat(measurement2.getFloatValue("FGCT") > measurement1.getFloatValue("FGCT"), "FGCT time didn't increase between measurements 1 and 2");
 102         assertThat(measurement2.getFloatValue("GCT") > measurement1.getFloatValue("GCT"), "GCT time didn't increase between measurements 1 and 2");
 103     }
 104 
 105     /**
 106      * Helper function to assert the utilization of the space
 107      *
 108      * @param measurement - measurement results to analyze
 109      * @param targetMemoryUsagePercent -assert that not less than this amount of
 110      * space has been utilized
 111      */
 112     public static void assertSpaceUtilization(JstatResults measurement, float targetMemoryUsagePercent) {
 113 
 114         if (measurement.valueExists("OU")) {
 115             float OC = measurement.getFloatValue("OC");
 116             float OU = measurement.getFloatValue("OU");
 117             assertThat((OU / OC) > targetMemoryUsagePercent, "Old space utilization should be > "
 118                     + (targetMemoryUsagePercent * 100) + "%, actually OU / OC = " + (OU / OC));
 119         }
 120 
 121         if (measurement.valueExists("MU")) {
 122             float MC = measurement.getFloatValue("MC");
 123             float MU = measurement.getFloatValue("MU");
 124             assertThat((MU / MC) > targetMemoryUsagePercent, "Metaspace utilization should be > "
 125                     + (targetMemoryUsagePercent * 100) + "%, actually MU / MC = " + (MU / MC));
 126         }
 127 
 128         if (measurement.valueExists("O")) {
 129             float O = measurement.getFloatValue("O");
 130             assertThat(O > targetMemoryUsagePercent * 100, "Old space utilization should be > "
 131                     + (targetMemoryUsagePercent * 100) + "%, actually O = " + O);
 132         }
 133 
 134         if (measurement.valueExists("M")) {
 135             float M = measurement.getFloatValue("M");
 136             assertThat(M > targetMemoryUsagePercent * 100, "Metaspace utilization should be > "
 137                     + (targetMemoryUsagePercent * 100) + "%, actually M = " + M);
 138         }
 139     }
 140 
 141     private static void assertThat(boolean result, String message) {
 142         if (!result) {
 143             throw new RuntimeException(message);
 144         }
 145     }
 146 
 147 }