#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
import datetime
import time
import psutil
from ctypes import *
"""
性能测试示例Python代码:
1、获取系统性能指标(内存、cpu、GDI、IO)
2、保存数据至日志文件 (./******.log)
3、可同时监控多个程序(xiadan:hexin)
"""
class PerfMonitor(object):
def __init__(self, pnames, monitor_time):
self.pnames = pnames
self.monitor_time = monitor_time
self.perflog = "./"
self.perfimg = "./"
def getGDIcount(self,PID):
PH = windll.kernel32.OpenProcess(5432, 0, PID)
GDIcount = windll.user32.GetGuiResources(PH, 0)
windll.kernel32.CloseHandle(PH)
return GDIcount
def time_wait(self,proc_name):
# 增加隐形等待时间
pid_list = []
now_data = time.time()
while time.time() < now_data + 35:
pid_data = list(filter(lambda x: x.name() == proc_name, psutil.process_iter()))
if pid_data != []:
pid_list.append(list(pid_data)[0].pid)
break
else:
time.sleep(0.1)
return pid_list
def get_pid(self,pnames):
# 获取进程号列表
pnames = map(lambda x : (x + ".exe"), pnames.split(":"))
pid_lists = []
for proc_name in pnames:
pid = self.time_wait(proc_name)
pid_lists.extend(pid)
return pid_lists
def write_logs(self,log_filepath,usagestr):
# 数据写入log日志
fp=open(log_filepath,"a")
fp.write(usagestr)
fp.close()
def get_nowtime(self):
# 获取现在时间
now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return now_time
def get_log_filepaths(self, p, monitor, types, check_times):
name = p.name()
names = name.split(".")[0]
current = check_times
usagestr = current + "\t" + str(monitor) + "\n"
log_filepath = self.perflog + types + "-" + names + "-" + str(p.pid) + ".log"
self.write_logs(log_filepath,usagestr)
def perfrun(self):
try:
monitorsecond = self.monitor_time * 60
begintime = int(time.time())
p_list = []
for pid in self.get_pid(self.pnames):
p_list.append((pid,psutil.Process(int(pid))))
while((int(time.time()) - begintime) <= monitorsecond):
for (pid, p) in p_list:
# 内存
mem_time = self.get_nowtime()
memusage = p.memory_info().rss/1024.0/1024.0
# CPU
cpu_time = self.get_nowtime()
cpuusage = p.cpu_percent()
# GDI
gdi_time = self.get_nowtime()
GDIcount = self.getGDIcount(pid)
# IO
iousage = p.io_counters()
read_count_time = self.get_nowtime()
read_count = iousage.read_count
write_count_time = self.get_nowtime()
write_count = iousage.write_count
read_bytes_time = self.get_nowtime()
read_bytes = iousage.read_bytes
write_bytes_time = self.get_nowtime()
write_bytes = iousage.write_bytes
# 写入日志
self.get_log_filepaths(p, memusage, "mem", mem_time)
self.get_log_filepaths(p, cpuusage, "cpu", cpu_time)
self.get_log_filepaths(p, GDIcount, "gdi", gdi_time)
self.get_log_filepaths(p, read_count, "io-read-count", read_count_time)
self.get_log_filepaths(p, write_count, "io-write-count", write_count_time )
self.get_log_filepaths(p, read_bytes, "io-read-bytes", read_bytes_time)
self.get_log_filepaths(p, write_bytes, "io-write-bytes", write_bytes_time)
time.sleep(2)
now = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S")
except Exception as e:
print(e)
if __name__ == '__main__':
if(len(sys.argv) == 3):
pnames=sys.argv[1]
monitor_time=int(sys.argv[2])
else:
print("""|====================================================================================|""")
print("""| Usage Instructions |""")
print("""|====================================================================================|""")
print("""| usage : python perfmonitor.py pnames monitor_time (minute) |""")
print("""| example : python perfmonitor.py xiadan:hexin 10 |""")
print("""|====================================================================================|""")
pnames = "xiadan"
monitor_time = 5
perfm = PerfMonitor(pnames, monitor_time)
perfm.perfrun()