Forum Discussion

culisse's avatar
culisse
Contributor
13 years ago

Capture Memory Usage on remote Server

Hello,



I'm running some Memory Leak tests on my client machine for our web site.  I used the TestComplete training video as a guideline and that has been functioning great as I use the Sys.MemUsage command to capture the memory on my client machine as the script hits various pages over and over.  Now IT has asked if I could capture the Memory Usage on the web server as well.  Now I know this is possible with the server monitors in LoadComplete but we only have TestComplete.



So, in summary, I was wondering how to basically execute a "Sys.MemUsage" command in TestComplete to capture data on another machine if possible?



Thanks.



~Chris

6 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Chris,



    You can use WMI scripting classes to get memory usage on a remote computer. Specifically:

    * get total physical memory using the Win32_ComputerSystem.TotalPhysicalMemory property,

    * get free physical memory using the Win32_OperatingSystem.FreePhysicalMemory property,

    * calculate memory usage as (total_memory - free_memory) / total_memory.



    Here's an example:

    Sub Main

      Dim strComputer, strLogin, strPassword, iMemUsage



      strComputer = "computername" ' Use "." for local computer

      strLogin    = "domain\user"  ' An empty string ("") means the currently used account on the local computer

      strPassword = "password"     ' An empty string ("") means the password of the currently used account on the local computer



      iMemUsage = GetMemUsage(strComputer, strLogin, strPassword)

      Log.Message "Memory usage on computer " & strComputer & ": " & iMemUsage

    End Sub



    ' Returns % used physical memory on the specified computer

    Function GetMemUsage(Computer, Login, Password)

      Const FLAGS = 48 ' wbemFlagForwardOnly + wbemFlagReturnImmediately

      Dim oLocator, oWMI, colItems, oItem, nTotalMem, nFreeMem



      Set oLocator = CreateObject("WbemScripting.SWbemLocator")

      Set oWMI = oLocator.ConnectServer(Computer, "root\cimv2", Login, Password)

      oWMI.Security_.ImpersonationLevel = 3 ' wbemImpersonate



      ' Get total physical memory, in kbytes

      Set colItems = oWMI.ExecQuery("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem", , FLAGS)

      For Each oItem in colItems

        nTotalMem = CDbl(oItem.TotalPhysicalMemory) / 1024

      Next



      ' Get free physical memory, in kbytes

      Set colItems = oWMI.ExecQuery("SELECT FreePhysicalMemory FROM Win32_OperatingSystem", , FLAGS)

      For Each oItem in colItems

        nFreeMem = CDbl(oItem.FreePhysicalMemory)

      Next



      ' Memory Usage = (Total - Free) / Total

       GetMemUsage = Round((nTotalMem - nFreeMem) / nTotalMem * 100)

    End Function




    Note that for the script to work, you may need to change security settings on the local and remote computers and allow WMI connections through the firewall as described in the following articles:

    Working With WMI Objects in Scripts (TestComplete help)

    Connecting to WMI on a Remote Computer

    Connecting to WMI Remotely Starting with Windows Vista

    Connecting Through Windows Firewall
  • JNugent's avatar
    JNugent
    Occasional Contributor
    Hi Helen,



    I would love to use your script but I have run in to some probelsm. i am doing this on my local machine of which I have full admin rights.

    But I get the following error:



    Object doesn't support this property or method: 'oItem.TotalPhysicalMemory



    Is there something else specifically I need to set up on my machine?'





    Because this is my local machine, i have set the computer name to ., and left the username and password blank.

    Thanks,

    Joanne

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Joanne,



    Sorry, my above script had a typo. Instead of:

    nFreeMem = CDbl(oItem.TotalPhysicalMemory)
    it should be

    nFreeMem = CDbl(oItem.FreePhysicalMemory)


    I've fixed the example, so it should work fine now.



    Thanks for catching the error!
  • avinashs15's avatar
    avinashs15
    Occasional Contributor
    Hi,



    Im trying to get memory consumed by a process 

    We are mainly using C# scripit for our project. we were able to get vbscripot of test complete but not with c# script.



    This is the method




     


    function Test7(system,login,pwd) 


    { var i,colItems,sWMI; 


      //var asd = Sys["OleObject"]("WbemScripting.SWbemLocator"); 


     


      sWMI=Sys["OleObject"]("WbemScripting.SWbemLocator") 


      wmiserv= Sys["OleObject"]("WbemScripting.SWbemLocator")["ConnectServer"](system,"root\\cimv2",login,pwd);//,"", "",0,"") 


      Sys["OleObject"]("WbemScripting.SWbemLocator")["Security_"]["ImpersonationLevel"] = 3; 


     


      var colItems= wmiserv["ExecQuery"]("SELECT * FROM Win32_Process") 


      Log["Message"](colItems["Count"]); 


      for (i=1;i<=colItems["Count"];i++) 


      { 


     


           Log["Message"]( colItems["Item"](i)["Name"]) 


     


      } 


     


     


    }



    colItems["Count"] returns proper count (number of process running on the server. However, when i use colItems["Item"](i)["Name"], it gives me general error.



    Appritiate the help. We need exuivalent c# script