Reply
Frequent Contributor

VBScript - Get CPU Usage

Hi all,
I want to get CPU Usage from VBScript. I can't use TC API that's why I am getting like this. Here is my code.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set AllItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48)
for each itm in AllItems
WScript.Echo itm.PercentProcessorTime & "%"
next

Here PercentProcessorTime gives CPU Time not CPU Usage. I want to get CPU Usage. Can anybody help me?
Refer the screenshot also. Also refer http://msdn.microsoft.com/en-us/library/aa394372(v=VS.85).aspx#methods

Thanks...
Give Kudos or Accept as a Solution to help others if my input helped you !!!
http://shanmugavelc.blogspot.in
vex
Contributor

RE: VBScript - Get CPU Usage

I use this in my scripts all the time.  This should work.



Function sdGetCpu(proc)



  proc = aqString.Replace(proc, ".exe", "")

  proc = lcase(proc)

 

  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

  set objRefresher = CreateObject("WbemScripting.SWbemRefresher")

  Set colItems = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet

  objRefresher.Refresh



  For Each objItem in colItems

    If lcase(objItem.Name) = proc Then

      objRefresher.Refresh

      sdGetCpu = objItem.PercentProcessorTime

    End If

  Next

 

End Function
Frequent Contributor

RE: VBScript - Get CPU Usage

Thanks Vince.



Is the PercentProcessorTime gives, CPU Usage?

I think, its giving CPU Time. See the Task Manager screenshot attached in this thread.

Correct me if I am wrong here.



Thanks...
Give Kudos or Accept as a Solution to help others if my input helped you !!!
http://shanmugavelc.blogspot.in
vex
Contributor

RE: VBScript - Get CPU Usage

Are you looking for the total CPU Usage across all processes?  I don't think there is a Win32 API that can get that.  You'll want to read something along these lines:



http://www.codeproject.com/Articles/3413/How-to-get-CPU-usage-by-performance-counters-witho



My example was mainly for a single process.
Moderator

RE: VBScript - Get CPU Usage

Hi Shanmugavel,



Win32_PerfFormattedData_PerfOS_Processor.PercentProcessorTime in the script that you posted is the CPU usage -- per processors/cores indeed. I'm afraid, it's not clear from your post what issues you're having with this code.



Could you please clarify which exactly CPU usage do you need? --

- A list of CPU usage per each process (application) (marked 1 in the attached screenshot)

- CPU usage for a specific process (application)

- Total CPU usage (marked 2 on the attached screenshot)

- Something else



Also, why don't TestComplete's Sys.CPUUsage and Sys.Process(...).CPUUsage suit your needs? Is it because you are planning to run this code using wscript/cscript outside of TestComplete?
Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.
Frequent Contributor

RE: VBScript - Get CPU Usage

Yes, You are Correct Helen. I want to run the script outside the TC environment.



Here I want to know the CPU Usage for a particular process like you listed as point No.2 in the list. i.e. Marked as 1 your screenshot but for a particular process.

Here I am bit confusion like Is PercentProcessorTime is the CPU Usage for a particular process? If so, then I am done.



Thanks...
Give Kudos or Accept as a Solution to help others if my input helped you !!!
http://shanmugavelc.blogspot.in
Frequent Contributor

RE: VBScript - Get CPU Usage

Yes, Exactly I want Sys.Process("process").CPUUsage value using other scripts externally.
Give Kudos or Accept as a Solution to help others if my input helped you !!!
http://shanmugavelc.blogspot.in
Moderator

RE: VBScript - Get CPU Usage

Hi Shanmugavel,



Yes, Exactly I want Sys.Process("process").CPUUsage value using other scripts externally.
That's exactly what Vince's example does.



Here I am bit confusion like Is PercentProcessorTime is the CPU Usage for a particular process?
Yes, it's CPU usage.





By the way, as for general scripting and programming questions that are not directly related to TestComplete and automated testing, you will probably find better answers in specialized forums, such as Scripting Guys Forum or Stack Overflow.
Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.
Frequent Contributor

RE: VBScript - Get CPU Usage

Thanks Helen for your info.
Give Kudos or Accept as a Solution to help others if my input helped you !!!
http://shanmugavelc.blogspot.in