1000 percent speed up for Get-vCOpsResourceMetric

Currently VMware does not provide a PowerCLI snapin to access the vCenter Operations API (the HttpPostAdapter). Clint Kitson and Alan Renouf have developed a Powershell Module to fill that gap.

The Module provides the CMDlets Get-vCOpsResourceAttributes, Get-vCOpsDBQuery and Get-vCOpsResourceMetric. I use Get-vCOpsResourceMetric in a customer project to fetch the “Active Memory” of virtual machines to calculate new RAM reservation values. The Module has saved me a lot of development time, but the runtime is too high if you have more than a few VM’s.

The Get-vCOpsResourceMetric CMDlet uses several Arrays to process the metric data that has been fetched from vCenter Operations. I have replaced that arrays with LinkedLists, because if you have many entries in the arrays they are really slow in Powershell/.net. Since Luc Dekens has provided that hint in his PowerCLI session during VMWorld 2011, i use it in many scripts to raise the performance of my scripts. You can find some additional infos on James Brundage’s page start-automating.com. I also moved the creation of PSobject that contains the final data structure out of the inner foreach loop and i have removed the date conversion from the CMDlet. Especially the date conversion from Unix time stamp to a local date format is very time consuming. The PSoject still includes the timestamp and you can make the conversion in a later step if you need that for some metrics. You can download the updated version of the ps_vcops.psm1 Module. Maybe Clint and Alan can update the original version to include the changes and provide a option to enable the date conversion.

original scriptoptimized script with date conversionoptimized script without date conversion
CMDletGet-vCOpsResourceMetricGet-vCOpsResourceMetricoptimizeddateincludedGet-vCOpsResourceMetricoptimized
Metricmem|active_averagemem|active_averagemem|active_average
Timeframe180 days180 days180 days
# Values305573055530555
Runtime0h 4m 54s0h 3m 15s0h 0m 28s
Runtime per Value0.009631s0.006371s0.000916s
Speed up151,16%1051,41%

I have used that calls to run the CMDlets and they provide the data shown in the screenshots:

  • $VM | Get-vCOpsResourceMetric -metricKey “mem|active_average” -startDate (Get-Date).AddHours(-4320) -includeDt:$false -includeSmooth:$false

default-Get-vCOpsResourceMetric-150x60

  • $VM | Get-vCOpsResourceMetricoptimizeddateincluded -metricKey “mem|active_average” -startDate (Get-Date).AddHours(-4320) -includeDt:$false -includeSmooth:$false

optimizeddateincluded-Get-vCOpsResourceMetric-150x59

  • $VM | Get-vCOpsResourceMetricoptimized -metricKey “mem|active_average” -startDate (Get-Date).AddHours(-4320) -includeDt:$false -includeSmooth:$false

optimized-Get-vCOpsResourceMetric-150x61

If you have more than a few VM’s in your environment or you want to use a big timeframe for your vCOps metrics this optimization gives you big time savings. For more information about the usage of the HttpPostAdapter open the documentation url [https://vcopshost/HttpPostAdapter] of your vCOps installation.

Thanks to Clint and Alan for developing that Module!