Create custom Perfmon counters and update them using PowerShell

This post has been republished via RSS; it originally appeared at: TestingSpot Blog articles.

First published on MSDN on Jun 14, 2017

Authored by Edwin Hernandez


Adding Perfmon counters to a Visual Studio load test is useful to monitor remote host’s metrics such a as usage of Processor, Memory or Local Disks. These metrics can be added to a graph and overlay against Load Test metrics such as throughput or response times for analysis.

In certain cases there are metrics that you wish you could visualize in a Load Test Graph as a counter. These are metrics that are not necessarily related to resource utilization. Below are some examples of this situation:
- Imagine that there is a back-end Batch process, such as a .exe program processing reports, estimates or invoices, and that   such program is logging information of interest for performance testing, such as # of items being processed ,avg. elapsed time per item , # of errors, etc. It would be useful to overlay that information against e.g. CPU utilization?
- Another example, a separate test exercising another feature of the same system, such as a UI test, or an automated functional test of a Desktop application and you want to include the timing results in the Load Test Charts. If the test is producing any logs, these logs can be integrated into a Load Test Graph.
- Yet another example: perhaps it would be helpful to monitor the size of a folder (not the whole disk) or the number of items in a folder path so it can visualized it in a Chart.

For all the situations above, if the information that is needed is being logged into a log (.txt, .csv., etc.), this article describes a way to create a custom Perfmon counter and feed it with information from the log using a PowerShell script. Once it's published it can added to a Load Test Chart and do analysis of it.
To draw a better picture of all this, please image the following example: let's assume a Load Test is being planned and there is some statistical information being logged into a .log file named WikieStats.csv that you want to include in your Load Test Charts. To simulate this, a program was created that pulls statistical information from Wikipedia, it then writes it to a log file every 2 seconds:



Now, to create the custom Perfmon counters: one of the easiest ways is described in this article by Michael Repperger: article. Using Michael’s approach and the import-csv cmdlet, the information can be consumed from the log and updated to the custom Perfmon counters, every 2 seconds with a PowerShell script:

$categoryName = “WikiStats”
$categoryHelp = “Statistics from Wikipedia”
$categoryType = [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance
$categoryExists = [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName)

If (-Not $categoryExists)
{
$objCCDC = New-Object System.Diagnostics.CounterCreationDataCollection
$objCCD1 = New-Object System.Diagnostics.CounterCreationData
$objCCD1.CounterName = “WikiStats”
$objCCD1.CounterType = “NumberOfItems32”
$objCCD1.CounterHelp = “Statistics from Wikipedia”
$objCCDC.Add($objCCD1) | Out-Null
[System.Diagnostics.PerformanceCounterCategory]::Create($categoryName, $categoryHelp, $categoryType, $objCCDC)|Out-Null
}

$perfInst1a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Pages”, $false)
$perfInst2a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Articles”, $false)
$perfInst3a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Edits”, $false)
$perfInst4a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Images”, $false)
$perfInst5a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Users”, $false)
$perfInst6a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Active Users”, $false)
$perfInst7a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStats”, “Admins”, $false)
$perfInst8a = New-Object System.Diagnostics.PerformanceCounter($categoryName, “WikiStatss”, “Jobs”, $false)

for ($i = 1; $i -lt 100000; $i++){
$timings = import-csv “C:\temp\PS1Example\wikiStats.csv”
$perfInst1a.RawValue = $timings[$timings.Length-1].'Pages' -as[int]
$perfInst2a.RawValue = $timings[$timings.Length-1].’Articles' -as[int]
$perfInst3a.RawValue = $timings[$timings.Length-1].’Edits' -as[int]
$perfInst4a.RawValue = $timings[$timings.Length-1].’Images' -as[int]
$perfInst5a.RawValue = $timings[$timings.Length-1].’Users' -as[int]
$perfInst6a.RawValue = $timings[$timings.Length-1].’Active Users' -as[int]
$perfInst7a.RawValue = $timings[$timings.Length-1].’Admins' -as[int]
$perfInst8a.RawValue = $timings[$timings.Length-1].’Jobs' -as[int]

#Start-Sleep -s 2
}

Read-Host

The PowerShell program has to be running during the Load Test for the counters to update. Once it is setup it can be simply added to the customer Counters in a "Counter Set" in Visual Studio and once the Load Test is running, these metrics would be collected and could be used for analysis.

Here is how it would look:



In the zip file attached to this article you can find the following: 1. PowerShell script, 2. the Load Test Solution with the custom Perfmon counters as a reference, this solution also included a Coded Webtest that is pulling the statistical information from Wikipedia. 3. The sample wikiStatis.csv mentioned above.

attachments

One Reply to “Create custom Perfmon counters and update them using PowerShell”

  1. Is there any way without running this script in background. and update value of metric as per need and using some other script.

    Here i can see until script is running its showing counter and metrics on perfmon once script is terminate we are not seeing this counters.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.