简体   繁体   中英

Slow Powershell function. How to Improve?

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.

How 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?' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM