如何使用python为GDI和用户对象收集性能指标

认为这是我在这里问的第一个问题,通常find我需要的所有答案(所以提前感谢)

好的我的问题我已经写了一个Python程序,将在线程监视进程,并输出结果到CSV文件供以后。 此代码工作得很好我正在使用win32pdhutil计数器和WMI,Win32_PerfRawData_PerfProc_Process CPU%时间。 我现在被要求监视WPF应用程序,特别是监视用户对象和GDI对象。

这是我有一个问题,这是我似乎无法find任何python支持收集这两个计数器的指标。 这两个计数器很容易在任务pipe理器中使用,我觉得很奇怪,这两个计数器的信息很less。 我特意看着收集这些信息,看看是否有内存泄漏,我不想在已经安装的python以外的其他系统上安装任何东西。 请你可以帮忙find解决办法。

我使用Python 3.3.1,这将运行在Windows平台(主要是win7和win8)这是我用来收集数据

def gatherIt(self,whoIt,whatIt,type,wiggle,process_info2): #this is the data gathering function thing data=0.0 data1="wobble" if type=="counter": #gather data according to the attibutes try: data = win32pdhutil.FindPerformanceAttributesByName(whoIt, counter=whatIt) except: #a problem occoured with process not being there not being there.... data1="N/A" elif type=="cpu": try: process_info={}#used in the gather CPU bassed on service for x in range(2): for procP in wiggle.Win32_PerfRawData_PerfProc_Process(name=whoIt): n1 = int(procP.PercentProcessorTime) d1 = int(procP.Timestamp_Sys100NS) #need to get the process id to change per cpu look... n0, d0 = process_info.get (whoIt, (0, 0)) try: percent_processor_time = (float (n1 - n0) / float (d1 - d0)) *100.0 #print whoIt, percent_processor_time except ZeroDivisionError: percent_processor_time = 0.0 # pass back the n0 and d0 process_info[whoIt] = (n1, d1) #end for loop (this should take into account multiple cpu's) # end for range to allow for a current cpu time rather that cpu percent over sampleint if percent_processor_time==0.0: data=0.0 else: data=percent_processor_time except: data1="N/A" else: #we have done something wrong so data =0 data1="N/A" #endif if data == "[]": data=0.0 data1="N/A" if data == "" : data=0.0 data1="N/A" if data == " ": data=0.0 data1="N/A" if data1!="wobble" and data==0.0: #we have not got the result we were expecting so add an/a data=data1 return data 

干杯

编辑正确的CPU时间问题,如果有人试图运行它:D

所以经过漫长的搜索后,我能够把东西混合在一起,这就得到了我需要的信息。

 import time from ctypes import * from ctypes.wintypes import * import win32pdh # with help from here http://coding.derkeiler.com/Archive/Python/comp.lang.python/2007-10/msg00717.html # the following has been mashed together to get the info needed def GetProcessID(name): object = "Process" items, instances = win32pdh.EnumObjectItems(None, None, object, win32pdh.PERF_DETAIL_WIZARD) val = None if name in instances : tenQuery = win32pdh.OpenQuery() tenarray = [ ] item = "ID Process" path = win32pdh.MakeCounterPath( ( None, object, name, None, 0, item ) ) tenarray.append( win32pdh.AddCounter( tenQuery, path ) ) win32pdh.CollectQueryData( tenQuery ) time.sleep( 0.01 ) win32pdh.CollectQueryData( tenQuery ) for tencounter in tenarray: type, val = win32pdh.GetFormattedCounterValue( tencounter, win32pdh.PDH_FMT_LONG ) win32pdh.RemoveCounter( tencounter ) win32pdh.CloseQuery( tenQuery ) return val processIDs = GetProcessID('OUTLOOK') # Remember this is case sensitive PQI = 0x400 #open a handle on to the process so that we can query it OpenProcessHandle = windll.kernel32.OpenProcess(PQI, 0, processIDs) # OK so now we have opened the process now we want to query it GR_GDIOBJECTS, GR_USEROBJECTS = 0, 1 print(windll.user32.GetGuiResources(OpenProcessHandle, GR_GDIOBJECTS)) print(windll.user32.GetGuiResources(OpenProcessHandle, GR_USEROBJECTS)) #so we have what we want we now close the process handle windll.kernel32.CloseHandle(OpenProcessHandle) 

希望有所帮助