Forum Discussion

jimmykilleen's avatar
jimmykilleen
New Contributor
10 years ago

Output the VBScript Unit Name to File

I am currently writing a VBScript  function that outputs the CPU% & Memory Usage of two applications that run simultaneously. I have achieved this & I get the data to in csv format.

I have a set of VB Scripts that run as part of my project & I have amended them to call the function above in order to output the CPU & Memory Usage.



What I want to achieve is that when my function is called, I can also output the Script Unit name (or even better the actual sub routine within the script that has been run).  This will then allow me to accurately monitor individual areas of my applications for any possible memory leaks etc. Any Ideas?? Below is my code so far.






Sub CPU_Mem_Usage

'This routine will monitor the CPU & the Mem Usage  & outputs the result to a CSV file with the build version as the file name.

  Dim sPath

  sPath = "\\Ulysses2008\TestComplete\FutaTill\TestProject_FT\Results\FutaTill_CPU_Mem_Stats_"&_

         Sys.Process("FutaTill").FileVersionInfo & ".csv"

  If Not aqFile.Exists(sPath) Then

    Call aqFile.Create(sPath)

    Call aqFile.WriteToTextFile(sPath, "CPU & Memory Usage Statistics for FutaTill Build Version " &_

          Sys.Process("FutaTill").FileVersionInfo  & vbCrlf , aqFile.ctANSI, False)

    Call aqFile.WriteToTextFile(sPath, "Test Date & Time: " & aqDateTime.Now & vbCrlf , aqFile.ctANSI, False)

    Call aqFile.WriteToTextFile(sPath, "FutaTill, CPU (%), Mem Usage (K),, WinEpos, CPU (%), Mem Usage (K)"   &_

vbCrlf , aqFile.ctANSI, False)

  End If

    Call aqFile.WriteToTextFile(sPath,  "," & Sys.Process("FutaTill").CPUUsage & "," & Sys.Process("FutaTill").MemUsage &_

          "," & ",," & Sys.Process("wineposv4").CPUUsage & "," & Sys.Process("wineposv4").MemUsage & vbCrlf , aqFile.ctANSI, False)

End Sub



1 Reply

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Jimmy,

     


    There are two ways you can consider:


    1. If you are running Test Items, you can extract the name from the caption of the test item element by using regular expressions:




    'VBScript


    Dim regEx


    Set regEx = New RegExp


    regEx.Pattern = "-\s(.+)"


    regEx.IgnoreCase = True


    Set Matches = regEx.Execute(Project.TestItems.Current.ElementToBeRun.Caption)


    Log.Message(Matches(0).SubMatches(0))




     


    2. In case of JScript, use methods and properties of the JScript Function object to get the code of the currently executed function. For example, the following method returns the call stack of the currently executed function:




    'JScript


    function GetStack()


    {


      var text, fname, parent;


      text = "";


      parent = GetStack["caller"];


      do {


        fname = parent["toString"]()["match"](/function (\w*)/)[1];


        text += fname + "\r\n";


        parent = parent["caller"];


      } while (parent);


      return aqString["Trim"](text);  


    }




     


    To get the name of the currently executed routine, call GetStack from it.