• WMI获取进程CPU占用率


    Monitor 

    % Process CPU Usage (specific process)
    http://www.tek-tips.com/viewthread.cfm?qid=395765
     
    for each Process in GetObject("winmgmts:{impersonationLevel=impersonate}//localhost").ExecQuery("Select PercentProcessorTime,IDProcess from Win32_PerfFormattedData_PerfProc_Process where IDProcess=4092")
       
       WScript.Echo("================================")
       For Each oProperty In Process.Properties_
            WScript.stdout.write vbtab & oProperty.Name & "="
            If IsArray(oProperty) Then
                For iCount = 0 To UBound(oProperty)
                    WScript.stdout.write  oProperty.Value(iCount) & "," 
                Next
                WScript.StdOut.WriteLine 
            ElseIf IsNull(oProperty) Then
                WScript.stdout.writeline "Property not set"
            Else
                WScript.stdout.writeline oProperty.Value 
            End If 
        Next
        WScript.Echo(Process.PercentProcessorTime)
      WScript.quit  
    next
    ====================================================================================
    for each Process in GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
       WScript.echo Process.name & " " & CPUUSage(Process.Handle) & " %"   
    Next

    Function CPUUSage( ProcID )
     On Error Resume Next
        
     Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!RootCimv2")
        
     For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
      N1 = objInstance1.PercentProcessorTime
      D1 = objInstance1.TimeStamp_Sys100NS
      Exit For
     Next
     'WScript.Sleep(2000)
     
     For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
      N2 = perf_instance2.PercentProcessorTime
      D2 = perf_instance2.TimeStamp_Sys100NS
      Exit For
     Next
     ' CounterType - PERF_100NSEC_TIMER_INV
     ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
     Nd = (N2 - N1)
     Dd = (D2-D1)
     PercentProcessorTime = ( (Nd/Dd))  * 100
           
     CPUUSage = Round(PercentProcessorTime ,0)
    End Function
  • 相关阅读:
    BZOJ3672/UOJ7 [Noi2014]购票
    POJ3718 Facer's Chocolate Dream
    BZOJ1453:[WC]Dface双面棋盘
    BZOJ2957:楼房重建
    AtCoder Grand Contest 009 D:Uninity
    BZOJ2877:[NOI2012]魔幻棋盘
    BZOJ3065:带插入区间K小值
    BZOJ3489:A simple rmq problem
    浅谈主席树
    AtCoder Regular Contest 080 E:Young Maids
  • 原文地址:https://www.cnblogs.com/diyunpeng/p/6836150.html
Copyright © 2020-2023  润新知