Path: blob/master/test/jdk/sun/tools/jstatd/JstatGCUtilParser.java
41149 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Arrays;24import java.text.NumberFormat;2526import static jdk.test.lib.Asserts.*;27import jdk.test.lib.Utils;2829/**30* The helper class for parsing following output from command 'jstat -gcutil':31*32* S0 S1 E O M CCS YGC YGCT FGC FGCT CGC CGCT GCT33* 0.00 0.00 0.00 52.39 97.76 92.71 4 0.286 28 28.006 2 0.086 28.37834*35* It will be verified that numerical values have defined types and are reasonable,36* for example percentage should fit within 0-100 interval.37*/38public class JstatGCUtilParser {3940public enum GcStatisticsType {41INTEGER, DOUBLE, PERCENTAGE, PERCENTAGE_OR_DASH;42}4344public enum GcStatistics {45S0(GcStatisticsType.PERCENTAGE),46S1(GcStatisticsType.PERCENTAGE),47E(GcStatisticsType.PERCENTAGE),48O(GcStatisticsType.PERCENTAGE),49M(GcStatisticsType.PERCENTAGE_OR_DASH),50CCS(GcStatisticsType.PERCENTAGE_OR_DASH),51YGC(GcStatisticsType.INTEGER),52YGCT(GcStatisticsType.DOUBLE),53FGC(GcStatisticsType.INTEGER),54FGCT(GcStatisticsType.DOUBLE),55CGC(GcStatisticsType.INTEGER),56CGCT(GcStatisticsType.DOUBLE),57GCT(GcStatisticsType.DOUBLE);5859private final GcStatisticsType type;6061private GcStatistics(GcStatisticsType type) {62this.type = type;63}6465private GcStatisticsType getType() {66return type;67}6869public static boolean isHeadline(String... valueArray) {70if (valueArray.length != values().length) {71return false;72}73int headersCount = 0;74for (int i = 0; i < values().length; i++) {75if (valueArray[i].equals(values()[i].toString())) {76headersCount++;77}78}79if (headersCount != values().length) {80return false;81}82return true;83}8485private static void verifyLength(String... valueArray) throws Exception {86assertEquals(valueArray.length, values().length,87"Invalid number of data columns: " + Arrays.toString(valueArray));88}8990public static void verify(String... valueArray) throws Exception {91verifyLength(valueArray);92for (int i = 0; i < values().length; i++) {93GcStatisticsType type = values()[i].getType();94String value = valueArray[i].trim();95if (type.equals(GcStatisticsType.INTEGER)) {96NumberFormat.getInstance().parse(value).intValue();97break;98}99if (type.equals(GcStatisticsType.DOUBLE)) {100NumberFormat.getInstance().parse(value).doubleValue();101break;102}103if (type.equals(GcStatisticsType.PERCENTAGE_OR_DASH) &&104value.equals("-")) {105break;106}107double percentage = NumberFormat.getInstance().parse(value).doubleValue();108assertTrue(0 <= percentage && percentage <= 100,109"Not a percentage: " + value);110}111}112113}114115private final String output;116117public JstatGCUtilParser(String output) {118this.output = output;119}120121public String getOutput() {122return output;123}124125/**126* The function will discard any lines that come before the header line.127* This can happen if the JVM outputs a warning message for some reason128* before running jstat.129*/130public void parse(int samples) throws Exception {131boolean headlineFound = false;132int datalineCount = 0;133134String[] lines = output.split(Utils.NEW_LINE);135for (String line : lines) {136line = line.replaceAll("\\s+", " ").trim();137String[] valueArray = line.split(" ");138139if (!headlineFound) {140headlineFound = GcStatistics.isHeadline(valueArray);141continue;142}143144GcStatistics.verify(valueArray);145datalineCount++;146}147148assertTrue(headlineFound, "No or invalid headline found, expected: " +149Utils.NEW_LINE + Arrays.toString(GcStatistics.values()).replaceAll(",", " "));150assertEquals(samples, datalineCount,151"Expected " + samples + " samples, got " + datalineCount);152}153154}155156157