private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
public static void main(String[] args) {
WindowsUtils wu=new WindowsUtils();
System.out.println(wu.findReg());
System.out.println(wu.findTaskKill());
System.out.println(wu.findWMIC());
System.out.println(wu.getProgramFilesPath());
String cmd="cmd /c taskkill /pid 1400 /t";
try {
Process p=Runtime.getRuntime().exec(cmd);
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare=(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}
//获取文件系统使用率
public static List<String> getDisk() {
// 操作系统
List<String> list=new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if(win.exists()){
long total=win.getTotalSpace();
long free=win.getFreeSpace();
Double compare=(1-free*1.0/total)*100;
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}
//获得cpu使用率
public static String getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir") + "//system32//wbem//wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,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 "CPU使用率:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%";
} else {
return "CPU使用率:"+0+"%";
}
} catch (Exception ex) {
ex.printStackTrace();
return "CPU使用率:"+0+"%";
}
}
//读取cpu相关信息
private static long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
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,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption =substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() > 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
usertime += Long.valueOf(s2).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;
}
/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
* @param src 要截取的字符串
* @param start_idx 开始坐标(包括该坐标)
* @param end_idx 截止坐标(包括该坐标)
* @return
*/
private static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}
- 浏览: 13034 次
- 性别:
- 来自: 上海
相关推荐
python selenium page 类封装
selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium
selenium python API分类总结.pdf
UI自动化selenium封装常用工具类
Selenium_v2.5 Selenium_v2.5 Selenium_v2.5 Selenium_v2.5Selenium_v2.5 Selenium_v2.5
selenium RC与selenium webdriver的区别
selenium2 java doc的chm格式。
但是如果使用 Python 或 Ruby 之类的高级动 态编程语言,那么这种复杂性方面的差异就很小。 2、两种模式之间最大的不同点在于: 如果使用 driven 脚本,测试有一部分在浏览器之外运行,而 如果使用 test runner 脚本...
1.selenium的python实现 2.WEB测试自动化
selenium selenium selenium
由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。 (6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。 (7)....
Selenium 服务器(selenium-server-4.1.1.jar)
java+selenium实现web自动化测试需要用到的jar包。不过某些情况下Selenium-server-standalone包即可,Selenium-java没有使用也可以运行
当然,对那些执行压力测试的工具来说,这类模拟已经足够。 Selenium 的脚本录制工具是通过监听用户对html 页面的操作来录制脚本的。Selenium 是真正能够监听用户对html 页面的操作的录制工具。Selenium 完全了解用户...
Selenium IDE 插件
Selenium 官方中文文档
1. Download the latest version of Selenium RC 2. Unzip the file save it C:\selenium-remote-control-1.0.3 3. command prompt>java -version –> Version should grater than 1.6.0_5 4. command prompt go to ...
selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。selenium 是...
昨天群里有朋友问我selenium入门例子,我今天抽了点时间写了一段简单的代码,此代码适合刚刚学习selenium的人员参考,此代码是selenium2 RC 调用chrome driver访问百度,输入Jack_test 搜索. 代码里面含资源包:selenium...