Forum Discussion

Jim_Taylor_3rd's avatar
Jim_Taylor_3rd
Occasional Contributor
13 years ago

Writing Perfmon Counters to a csv file

I am currently writing the time and memory usage to a csv file using aqfile and the following code statement:


     aqDateTime.Time() & "," & Sys.Process("MfgSys").MemUsage & vbCrLf






As I am monitoring for memory issues caused by improper garbage collection, I would also like to display the .Net CLR memory counters that are available in Perfmon.  In particulary, the size of the Gen 2 heap would be most useful -

\.net CLR Memory (_Global_)\Gen 2 heap size




I have not been able to find a way to access this counter so that I can write it to the same csv file.  I have tried running as administrator and still am not able to find a way to get at this counter. 



Any suggestions?



2 Replies

  • AlexeyK's avatar
    AlexeyK
    SmartBear Alumni (Retired)

    Jim,


    The counter you are looking for is available via WMI. Use the Win32_PerfFormattedData_NETFramework_NETCLRMemory class. Here is sample code:

    Sub Get_NETCLRMemory_Counter

      ' My process name

      myProcess = "_Global_" ' You can specify your executable's name here, e.g. Orders

      computerName = "."



      ' Connect to WMI via COM

      Set objWMIService = GetObject("winmgmts:\\" & computerName & "\root\cimv2")

      Set PerfProcess = objWMIService.Get( _

            "Win32_PerfFormattedData_NETFramework_NETCLRMemory.Name='" & myProcess & "'")



      ' Get the Get 2 heap size value

      Log.Message "Gen 2 heap size: " & aqConvert.VarToStr(PerfProcess.Gen2heapsize)



      ' Output all the properties of the NETCLRMemory class

      ' Can be useful in the future ;-)

      s = ""

      For Each prop In PerfProcess.Properties_

        s = s & prop.Name & ": " & aqConvert.VarToStr(prop.Value) & vbNewLine

      Next

      Log.Message "Properties", s ' Post to the Additional Information panel of the log

    End Sub

  • Jim_Taylor_3rd's avatar
    Jim_Taylor_3rd
    Occasional Contributor

    Fantastic - Thanks!



    I got this working this morning.  One key for anyone else who wants to use this: I had to run it signed in as Administrator rather than my normal ID with Admin rights.  The script ran, but I got all zeros for values when I was not running as administrator.



    Also, I used your script to create two more so that I could write these out to a csv file for graphing and analysis.  It was a very simple matter, but I'm sharing them to save someone time if they are new to this or haven't scripted in a while, as I haven't. 



    One script writes the Counter Names (prop.Name) to the header row of a .csv file.  I execute it before entering my Data-Driven or For Loop.  The other one writes out the Counter Values (prop.Value) to the .csv file within  the loop.



    First

    Create csv File and Headers:



    Sub Write_CLRCountersToHeader


      ' My process name

      myProcess = "_Global_" ' You can specify your executable's name here, e.g. Orders

      computerName = ".' You can specify your computer name here



     ' Connect to WMI via COM


       Set objWMIService = GetObject("winmgmts:\\" & computerName & "\root\cimv2")

       Set PerfProcess = objWMIService.Get( _

             "Win32_PerfFormattedData_NETFramework_NETCLRMemory.Name='" & myProcess & "'")


     'Creates a new file. 

      Call aqfile.Create("FilePath\CLRCounters.csv")  'Change FilePath to the path to the directory where you want to put the file'


    ' Writes each property name to the CSV Header Line  . 

       s = ""

       For Each prop In PerfProcess.Properties_

         s = s & prop.Name & ","

       Next

       Call aqFile.WriteToTextFile("FilePath\CLRCounters.csv", s & vbCrLf, 20)   'Must be same path and filename as above

    End Sub





    Second Script

    Write CLR Counter Values to csv Within a Loop:



    Sub Write_CLRCounterValuesToCSV

      ' My process name

      myProcess = "_Global_ " ' You can specify your executable's name here, e.g. Orders

      computerName = "." ' You can specify your computer name here


     ' Connect to WMI via COM

      Set objWMIService = GetObject("winmgmts:\\" & computerName & "\root\cimv2")

      Set PerfProcess = objWMIService.Get( _

                "Win32_PerfFormattedData_NETFramework_NETCLRMemory.Name='" & myProcess & "'")



    ' Writes each property to the CSV Header Line  .

        s = ""

        For Each prop In PerfProcess.Properties_

           s = s & aqConvert.VarToStr(prop.Value) & ","

        Next

        Call aqFile.WriteToTextFile("FilePath\CLRCounters.csv",s & vbCrLf, 20)

    End Sub