开发者

Slow Powershell function. How to Improve?

开发者 https://www.devze.com 2023-01-08 22:14 出处:网络
I have written the below Powershell function to call an F5 unit.It loops round for each server and gets each individual stat and then executes a SQL SMO call.

I have written the below Powershell function to call an F5 unit. It loops round for each server and gets each individual stat and then executes a SQL SMO call.

The code is running really slowly and i think i have discounted the SQL call as the cause.

Ho开发者_StackOverfloww can the powershell be improved?

function Print-VServerStats()
{

param($virtual_server);


$VirtualServerStatistics = (Get-F5.iControl).LocalLBVirtualServer.get_statistics( (, $virtual_server) );
$VirtualServerStatisticEntry = $VirtualServerStatistics.statistics[0];
$Statistics = $VirtualServerStatisticEntry.statistics | ? {$_.type -eq "STATISTIC_CLIENT_SIDE_CURRENT_CONNECTIONS" -or
                                                           $_.type -eq "STATISTIC_CLIENT_SIDE_MAXIMUM_CONNECTIONS" -or
                                                           $_.type -eq "STATISTIC_CLIENT_SIDE_TOTAL_CONNECTIONS"};

foreach ($Statistic in $Statistics)
{
  $val = Convert-To64Bit $Statistic.value.high $Statistic.value.low;

  switch ($Statistic.type) {
    "STATISTIC_CLIENT_SIDE_CURRENT_CONNECTIONS" {
     $label = "Current Connections";
    }
     "STATISTIC_CLIENT_SIDE_MAXIMUM_CONNECTIONS" {
     $label = "Max Connections";
    }
      "STATISTIC_CLIENT_SIDE_TOTAL_CONNECTIONS" {
     $label = "Total Connections";
    }
  }
    $profcmd.Parameters["@property"].Value = $SrceName
    $profcmd.Parameters["@propertyDesc"].Value = $label
    $profcmd.Parameters["@ValDim1"].Value = $virtual_server
    $profcmd.Parameters["@value"].Value = $val 
    $profcmd.Parameters["@Timestamp"].Value = $t
    [void]$profcmd.ExecuteNonQuery()
  }
}


We have an F5 BigIP and we've noticed that the UI on the device is really really slow. We haven't traced the cause, but it is very likely your delay is sourcing on the F5 device itself. Measure-Command on

$VirtualServerStatistics = (Get-F5.iControl).LocalLBVirtualServer.get_statistics( (, $virtual_server) );

Should show it if that's the case.


it's hard to test without having F5 but maybe you can shorten/improve the code by replacing:

$Statistics = $VirtualServerStatisticEntry.statistics | ? {$_.type -eq "STATISTIC_CLIENT_SIDE_CURRENT_CONNECTIONS" -or
                                                           $_.type -eq "STATISTIC_CLIENT_SIDE_MAXIMUM_CONNECTIONS" -or
                                                           $_.type -eq "STATISTIC_CLIENT_SIDE_TOTAL_CONNECTIONS"};

With

 $Statistics = $VirtualServerStatisticEntry.statistics | ? {$_.type -match '^STATISTIC_CLIENT_SIDE_(CURRENT|MAXIMUM|TOTAL)_CONNECTIONS?' }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号