Path: blob/master/test/jdk/java/lang/management/OperatingSystemMXBean/GetSystemLoadAverage.java
41153 views
/*1* Copyright (c) 2005, 2012, 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*/2223/*24*25*26* @bug 6336608 651173827* @summary Basic unit test of OperatingSystemMXBean.getSystemLoadAverage()28* @author Mandy Chung29*/3031/*32* This test tests the load average on linux and solaris. On Windows,33* getSystemLoadAverage() returns -1.34*35* Usage: GetSystemLoadAverage ["-1.0"]36* Arguments:37* o If no argument is specified, the test will verify the system load38* average with the /usr/bin/uptime command.39* o Otherwise, the input argument must be "-1.0" indicating the40* expected system load average. This would only be the case when41* running on Windows.42*/4344import java.lang.management.*;45import java.io.*;4647public class GetSystemLoadAverage {4849private static OperatingSystemMXBean mbean =50ManagementFactory.getOperatingSystemMXBean();5152// The system load average may be changing due to other jobs running.53// Allow some delta.54private static double DELTA = 0.05;5556public static void main(String args[]) throws Exception {57if (args.length > 1) {58throw new IllegalArgumentException("Unexpected number of args " + args.length);59}6061if (args.length == 0) {62// On Linux or Solaris63checkLoadAvg();64} else {65// On Windows, the system load average is expected to be -1.066if (!args[0].equals("-1.0")) {67throw new IllegalArgumentException("Invalid argument: " + args[0]);68} else {69double loadavg = mbean.getSystemLoadAverage();70if (loadavg != -1.0) {71throw new RuntimeException("Expected load average : -1.0" +72" but getSystemLoadAverage returned: " +73loadavg);74}75}76}7778System.out.println("Test passed.");79}8081private static String LOAD_AVERAGE_TEXT82= System.getProperty("os.name").contains("OS X")83? "load averages:"84: "load average:";8586private static void checkLoadAvg() throws Exception {87// Obtain load average from OS command88ProcessBuilder pb = new ProcessBuilder("/usr/bin/uptime");89Process p = pb.start();90String output = commandOutput(p);9192// obtain load average from OperatingSystemMXBean93double loadavg = mbean.getSystemLoadAverage();9495// verify if two values are close96output = output.substring(output.lastIndexOf(LOAD_AVERAGE_TEXT) +97LOAD_AVERAGE_TEXT.length() + 1);98System.out.println("Load average returned from uptime = " + output);99System.out.println("getSystemLoadAverage() returned " + loadavg);100101String[] lavg = System.getProperty("os.name").contains("OS X")102? output.split(" ")103: output.split(",");104double expected = Double.parseDouble(lavg[0]);105double lowRange = expected * (1 - DELTA);106double highRange = expected * (1 + DELTA);107108if (loadavg < lowRange || loadavg > highRange) {109throw new RuntimeException("Expected load average : " +110expected +111" but getSystemLoadAverage returned: " +112loadavg);113}114}115116private static String commandOutput(Reader r) throws Exception {117StringBuilder sb = new StringBuilder();118int c;119while ((c = r.read()) > 0) {120if (c != '\r') {121sb.append((char) c);122}123}124return sb.toString();125}126127private static String commandOutput(Process p) throws Exception {128Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");129String output = commandOutput(r);130p.waitFor();131p.exitValue();132return output;133}134135}136137138