blob: d9a017b6897a3364b8bec6e60c5c8e76ba8107e9 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 *
26 *
27 * @bug 6336608 6511738
28 * @summary Basic unit test of OperatingSystemMXBean.getSystemLoadAverage()
29 * @author Mandy Chung
30 */
31
32/*
33 * This test tests the load average on linux and solaris. On Windows,
34 * getSystemLoadAverage() returns -1.
35 *
36 * Usage: GetSystemLoadAverage ["-1.0"]
37 * Arguments:
38 * o If no argument is specified, the test will verify the system load
39 * average with the /usr/bin/uptime command.
40 * o Otherwise, the input argument must be "-1.0" indicating the
41 * expected system load average. This would only be the case when
42 * running on Windows.
43 */
44
45import java.lang.management.*;
46import java.io.*;
47
48public class GetSystemLoadAverage {
49
50 private static OperatingSystemMXBean mbean =
51 ManagementFactory.getOperatingSystemMXBean();
52
53 // The system load average may be changing due to other jobs running.
54 // Allow some delta.
55 private static double DELTA = 0.05;
56
57 public static void main(String args[]) throws Exception {
58 if (args.length > 1) {
59 throw new IllegalArgumentException("Unexpected number of args " + args.length);
60 }
61
62 if (args.length == 0) {
63 // On Linux or Solaris
64 checkLoadAvg();
65 } else {
66 // On Windows, the system load average is expected to be -1.0
67 if (!args[0].equals("-1.0")) {
68 throw new IllegalArgumentException("Invalid argument: " + args[0]);
69 } else {
70 double loadavg = mbean.getSystemLoadAverage();
71 if (loadavg != -1.0) {
72 throw new RuntimeException("Expected load average : -1.0" +
73 " but getSystemLoadAverage returned: " +
74 loadavg);
75 }
76 }
77 }
78
79 System.out.println("Test passed.");
80 }
81
82 private static String LOAD_AVERAGE_TEXT = "load average:";
83 private static void checkLoadAvg() throws Exception {
84 // Obtain load average from OS command
85 ProcessBuilder pb = new ProcessBuilder("/usr/bin/uptime");
86 Process p = pb.start();
87 String output = commandOutput(p);
88
89 // obtain load average from OperatingSystemMXBean
90 double loadavg = mbean.getSystemLoadAverage();
91
92 // verify if two values are close
93 output = output.substring(output.lastIndexOf(LOAD_AVERAGE_TEXT) +
94 LOAD_AVERAGE_TEXT.length());
95 System.out.println("Load average returned from uptime = " + output);
96 System.out.println("getSystemLoadAverage() returned " + loadavg);
97
98 String[] lavg = output.split(",");
99 double expected = Double.parseDouble(lavg[0]);
100 double lowRange = expected * (1 - DELTA);
101 double highRange = expected * (1 + DELTA);
102
103 if (loadavg < lowRange || loadavg > highRange) {
104 throw new RuntimeException("Expected load average : " +
105 expected +
106 " but getSystemLoadAverage returned: " +
107 loadavg);
108 }
109 }
110
111 private static String commandOutput(Reader r) throws Exception {
112 StringBuilder sb = new StringBuilder();
113 int c;
114 while ((c = r.read()) > 0) {
115 if (c != '\r') {
116 sb.append((char) c);
117 }
118 }
119 return sb.toString();
120 }
121
122 private static String commandOutput(Process p) throws Exception {
123 Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
124 String output = commandOutput(r);
125 p.waitFor();
126 p.exitValue();
127 return output;
128 }
129
130}