1 /*
2  * Copyright (c) 2015, 2018, 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 import java.text.NumberFormat;
27 import java.text.ParseException;
28 
29 
30 /**
31  * Results of running the jstat tool Concrete subclasses will detail the jstat
32  * tool options
33  */
34 abstract public class JstatResults extends ToolResults {
35 
36     private static final float FLOAT_COMPARISON_TOLERANCE = 0.0011f;
37 
JstatResults(ToolResults rawResults)38     public JstatResults(ToolResults rawResults) {
39         super(rawResults);
40     }
41 
42     /**
43      * Gets a string result from the column labeled 'name'
44      *
45      * @param name - name of the column
46      * @return the result
47      */
getStringValue(String name)48     public String getStringValue(String name) {
49         int valueNdx = new StringOfValues(getStdoutLine(0)).getIndex(name);
50         return new StringOfValues(getStdoutLine(1)).getValue(valueNdx);
51     }
52 
53     /**
54      * Gets a float result from the column labeled 'name'
55      *
56      * @param name - name of the column
57      * @return the result
58      */
getFloatValue(String name)59     public float getFloatValue(String name) {
60         int valueNdx = new StringOfValues(getStdoutLine(0)).getIndex(name);
61         // Let the parsing use the current locale format.
62         try {
63             return NumberFormat.getInstance().parse(new StringOfValues(getStdoutLine(1)).getValue(valueNdx)).floatValue();
64         } catch (ParseException e) {
65             throw new NumberFormatException(e.getMessage());
66         }
67 
68     }
69 
70     /**
71      * Gets an integer result from the column labeled 'name'
72      *
73      * @param name - name of the column
74      * @return the result
75      */
getIntValue(String name)76     public int getIntValue(String name) {
77         int valueNdx = new StringOfValues(getStdoutLine(0)).getIndex(name);
78         try {
79             return NumberFormat.getInstance().parse(new StringOfValues(getStdoutLine(1)).getValue(valueNdx)).intValue();
80         } catch (ParseException e) {
81             throw new NumberFormatException(e.getMessage());
82         }
83     }
84 
85     /**
86      * Checks if a column with a given name exists
87      *
88      * @param name - name of the column
89      * @return true if the column exist, false otherwise
90      */
valueExists(String name)91     public boolean valueExists(String name) {
92         return new StringOfValues(getStdoutLine(0)).getIndex(name) != -1;
93     }
94 
95     /**
96      * Helper function to assert the increase of the GC events between 2
97      * measurements
98      *
99      * @param measurement1 -first measurement
100      * @param measurement2 -first measurement
101      */
assertGCEventsIncreased(JstatResults measurement1, JstatResults measurement2)102     public static void assertGCEventsIncreased(JstatResults measurement1, JstatResults measurement2) {
103         assertThat(measurement2.getFloatValue("YGC") > measurement1.getFloatValue("YGC"), "YGC didn't increase between measurements 1 and 2");
104         assertThat(measurement2.getFloatValue("FGC") > measurement1.getFloatValue("FGC"), "FGC didn't increase between measurements 2 and 3");
105     }
106 
107     /**
108      * Helper function to assert the increase of the GC time between 2
109      * measurements
110      *
111      * @param measurement1 -first measurement
112      * @param measurement2 -second measurement
113      */
assertGCTimeIncreased(JstatResults measurement1, JstatResults measurement2)114     public static void assertGCTimeIncreased(JstatResults measurement1, JstatResults measurement2) {
115         assertThat(measurement2.getFloatValue("YGCT") >= measurement1.getFloatValue("YGCT"), "YGCT time rewinded between measurements 1 and 2");
116         assertThat(measurement2.getFloatValue("FGCT") >= measurement1.getFloatValue("FGCT"), "FGCT time rewinded between measurements 1 and 2");
117         assertThat(measurement2.getFloatValue("GCT") >= measurement1.getFloatValue("GCT"), "GCT time rewinded between measurements 1 and 2");
118     }
119 
120     /**
121      * Helper function to assert the utilization of the space
122      *
123      * @param measurement - measurement results to analyze
124      * @param targetMemoryUsagePercent -assert that not less than this amount of
125      * space has been utilized
126      */
assertSpaceUtilization(JstatResults measurement, float targetMemoryUsagePercent)127     public static void assertSpaceUtilization(JstatResults measurement, float targetMemoryUsagePercent) {
128         assertSpaceUtilization(measurement, targetMemoryUsagePercent, targetMemoryUsagePercent);
129     }
130 
131     /**
132      * Helper function to assert the utilization of the space
133      *
134      * @param measurement - measurement results to analyze
135      * @param targetMetaspaceUsagePercent -assert that not less than this amount
136      * of metaspace has been utilized
137      * @param targetOldSpaceUsagePercent -assert that not less than this amount
138      * of old space has been utilized
139      */
assertSpaceUtilization(JstatResults measurement, float targetMetaspaceUsagePercent, float targetOldSpaceUsagePercent)140     public static void assertSpaceUtilization(JstatResults measurement, float targetMetaspaceUsagePercent,
141             float targetOldSpaceUsagePercent) {
142 
143         if (measurement.valueExists("OU")) {
144             float OC = measurement.getFloatValue("OC");
145             float OU = measurement.getFloatValue("OU");
146             assertThat((OU / OC) > targetOldSpaceUsagePercent, "Old space utilization should be > "
147                     + (targetOldSpaceUsagePercent * 100) + "%, actually OU / OC = " + (OU / OC));
148         }
149 
150         if (measurement.valueExists("MU")) {
151             float MC = measurement.getFloatValue("MC");
152             float MU = measurement.getFloatValue("MU");
153             assertThat((MU / MC) > targetMetaspaceUsagePercent, "Metaspace utilization should be > "
154                     + (targetMetaspaceUsagePercent * 100) + "%, actually MU / MC = " + (MU / MC));
155         }
156 
157         if (measurement.valueExists("O")) {
158             float O = measurement.getFloatValue("O");
159             assertThat(O > targetOldSpaceUsagePercent * 100, "Old space utilization should be > "
160                     + (targetOldSpaceUsagePercent * 100) + "%, actually O = " + O);
161         }
162 
163         if (measurement.valueExists("M")) {
164             float M = measurement.getFloatValue("M");
165             assertThat(M > targetMetaspaceUsagePercent * 100, "Metaspace utilization should be > "
166                     + (targetMetaspaceUsagePercent * 100) + "%, actually M = " + M);
167         }
168     }
169 
assertThat(boolean result, String message)170     public static void assertThat(boolean result, String message) {
171         if (!result) {
172             throw new RuntimeException(message);
173         }
174     }
175 
checkFloatIsSum(float sum, float... floats)176     public static boolean checkFloatIsSum(float sum, float... floats) {
177         for (float f : floats) {
178             sum -= f;
179         }
180 
181         return Math.abs(sum) <= FLOAT_COMPARISON_TOLERANCE;
182     }
183 
assertConsistency()184     abstract public void assertConsistency();
185 }
186