Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python收集GDI和用户对象的性能指标_Python_Windows_Wmi_Performance Testing_Metrics - Fatal编程技术网

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

如何使用python收集GDI和用户对象的性能指标,python,windows,wmi,performance-testing,metrics,Python,Windows,Wmi,Performance Testing,Metrics,我想这是我在这里问的第一个问题,通常会找到我需要的所有答案(所以提前谢谢) 好的,我的问题是,我已经编写了一个python程序,它将在线程中监视一个进程,并将结果输出到一个csv文件,供以后使用。这段代码运行得很好,我使用Win32 PDHUTIL作为计数器,使用WMI、Win32_PerfRawData_PerfProc_进程作为CPU%的时间。我现在被要求监视WPF应用程序,特别是监视用户对象和GDI对象 这就是我的问题所在,我似乎找不到任何python支持在这两个计数器上收集度量。这两个计

我想这是我在这里问的第一个问题,通常会找到我需要的所有答案(所以提前谢谢)

好的,我的问题是,我已经编写了一个python程序,它将在线程中监视一个进程,并将结果输出到一个csv文件,供以后使用。这段代码运行得很好,我使用Win32 PDHUTIL作为计数器,使用WMI、Win32_PerfRawData_PerfProc_进程作为CPU%的时间。我现在被要求监视WPF应用程序,特别是监视用户对象和GDI对象

这就是我的问题所在,我似乎找不到任何python支持在这两个计数器上收集度量。这两个计数器在任务管理器中很容易找到。我觉得奇怪的是,关于这两个计数器的信息很少。我特别关注收集这些数据以查看是否存在内存泄漏,除了已经安装的python之外,我不想在系统上安装其他任何东西。请你帮我找到解决办法

我使用的是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 a n/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)

希望这能有所帮助

因此,经过长时间的搜索,我能够将一些东西组合在一起,从而获得所需的信息

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)

希望这对GDI计数有所帮助,我认为一个更简单、更干净的监控脚本如下:

import time, psutil
from ctypes import *

def getPID(processName):
    for proc in psutil.process_iter():
        try:
            if processName.lower() in proc.name().lower():
                return proc.pid
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return None;

def getGDIcount(PID):
    PH = windll.kernel32.OpenProcess(0x400, 0, PID)
    GDIcount = windll.user32.GetGuiResources(PH, 0)
    windll.kernel32.CloseHandle(PH)
    return GDIcount

PID = getPID('Outlook')

while True:
    GDIcount = getGDIcount(PID)
    print(f"{time.ctime()}, {GDIcount}")
    time.sleep(1)

对于GDI计数,我认为一个更简单、更干净的监视脚本如下所示:

import time, psutil
from ctypes import *

def getPID(processName):
    for proc in psutil.process_iter():
        try:
            if processName.lower() in proc.name().lower():
                return proc.pid
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return None;

def getGDIcount(PID):
    PH = windll.kernel32.OpenProcess(0x400, 0, PID)
    GDIcount = windll.user32.GetGuiResources(PH, 0)
    windll.kernel32.CloseHandle(PH)
    return GDIcount

PID = getPID('Outlook')

while True:
    GDIcount = getGDIcount(PID)
    print(f"{time.ctime()}, {GDIcount}")
    time.sleep(1)

哇,没人能解决它,比我想象的更难…哇,没人能解决它,比我想象的更难。。。