我要检测服务器上的性能:现在要做个java的程序 希望 有代码! 和讲解 谢谢
private double getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,"
+ "KernelModeTime,ReadOperationCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
} else {
return 0.0;
}
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
}
}
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream(), "gb2312");
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int kmtidx = line.indexOf("KernelModeTime");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出现顺序:Caption,KernelModeTime,ReadOperationCount,
// UserModeTime,WriteOperation
String caption = substring(line, capidx, kmtidx - 1)
.trim();
// log.info("line="+line);
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
idletime += Long.valueOf(
substring(line, kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
substring(line, umtidx, wocidx - 1).trim())
.longValue();
continue;
} else if (caption.equals("wmic.exe"))
continue;
kneltime += Long.valueOf(
substring(line, kmtidx, rocidx - 1).trim())
.longValue();
usertime += Long.valueOf(
substring(line, umtidx, wocidx - 1).trim())
.longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
取cpu使用率
在控制台执行下
String procCmd = System.getenv("windir")
[code="java"]
public MonitorInfoModel getMonitorInfoModel() throws Exception {
// 构造返回对象
MonitorInfoModel infoModel = new MonitorInfoModel();
// 可使用内存
infoModel.setTotalJVMMemorySize(Runtime.getRuntime().totalMemory() / M);
// 剩余内存
infoModel.setFreeJVMMemorySize(Runtime.getRuntime().freeMemory() / M);
infoModel.setUsedJVMMemorySize(infoModel.getTotalJVMMemorySize() -
infoModel.getFreeJVMMemorySize());
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系统
String osName = System.getProperty("os.name");
infoModel.setOsName(osName);
// 总的物理内存
infoModel.setTotalPhysicalMemorySize(osmxb.getTotalPhysicalMemorySize()
/ M);
// 剩余的物理内存
infoModel.setFreePhysicalMemorySize(osmxb.getFreePhysicalMemorySize()
/ M);
// 已使用的物理内存
infoModel.setUsedPhysicalMemorySize(infoModel.getTotalPhysicalMemorySize()
- infoModel.getFreePhysicalMemorySize());
infoModel.setTotalSwapMemorySize(osmxb.getTotalSwapSpaceSize() / M);
infoModel.setFreeSwapMemorySize(osmxb.getFreeSwapSpaceSize() / M);
infoModel.setUsedSwapMemorySize(infoModel.getTotalSwapMemorySize()
- infoModel.getFreeSwapMemorySize());
double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = getCpuRatioForWindows();
} else {
cpuRatio = getCpuRateForLinux();
}
if (cpuRatio > 99.99)
cpuRatio = 100.0;
infoModel.setCPURatio(cpuRatio);
return infoModel;
}
public class MonitorInfoModel implements Serializable {
private static final long serialVersionUID = 1L;
/** JVM可使用内存 */
private long totalJVMMemorySize;
/** JVM剩余内存 */
private long freeJVMMemorySize;
/** JVM使用内存 */
private long usedJVMMemorySize;
/** 操作系统 */
private String osName;
/** 总的物理内存 */
private long totalPhysicalMemorySize;
/** 剩余的物理内存 */
private long freePhysicalMemorySize;
/** 已使用的物理内存 */
private long usedPhysicalMemorySize;
/** 总的缓存 */
private long totalSwapMemorySize;
/** 剩余的缓存 */
private long freeSwapMemorySize;
/** 已使用的缓存 */
private long usedSwapMemorySize;
/** cpu使用率 */
private double cpuRatio;
[/code]