Fixes code styling for New-IcingaPerformanceCounterArray

This commit is contained in:
Christian Stein 2020-08-04 15:37:35 +02:00
parent 3e5a97d52b
commit 9ece8d805f

View file

@ -12,60 +12,67 @@
'\Memory\% Committed Bytes In Use' '\Memory\% Committed Bytes In Use'
); );
#> #>
function New-IcingaPerformanceCounterArray() function New-IcingaPerformanceCounterArray()
{ {
param( param(
[array]$CounterArray = @() [array]$CounterArray = @()
) )
[hashtable]$CounterResult = @{}; [hashtable]$CounterResult = @{};
[bool]$RequireSleep = $TRUE; [bool]$RequireSleep = $TRUE;
foreach ($counter in $CounterArray) { foreach ($counter in $CounterArray) {
# We want to speed up things with loading, so we will check if a specified # We want to speed up things with loading, so we will check if a specified
# Counter is already cached within our hashtable. If it is not, we sleep # Counter is already cached within our hashtable. If it is not, we sleep
# at the end of the function the required 500ms and don't have to wait # at the end of the function the required 500ms and don't have to wait
# NumOfCounters * 500 milliseconds for the first runs. This will speed # NumOfCounters * 500 milliseconds for the first runs. This will speed
# up the general loading of counters and will not require some fancy # up the general loading of counters and will not require some fancy
# pre-caching / configuration handler # pre-caching / configuration handler
# TODO: Re-Implement caching for counters # TODO: Re-Implement caching for counters
#if ($Icinga2.Cache.PerformanceCounter -ne $null) { #if ($Icinga2.Cache.PerformanceCounter -ne $null) {
# if ($Icinga2.Cache.PerformanceCounter.ContainsKey($counter) -eq $TRUE) { # if ($Icinga2.Cache.PerformanceCounter.ContainsKey($counter) -eq $TRUE) {
$RequireSleep = $FALSE; $RequireSleep = $FALSE;
# } # }
#} #}
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE; $obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) { if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value()); $CounterResult.Add($obj.Name(), $obj.Value());
} }
} }
# Above we initialse ever single counter and we only require a sleep once # TODO: Add a cache for our Performance Counters to only fetch them once
# in case a new, yet unknown counter was added # for each session to speed up the loading. This cold be something like
if ($RequireSleep) { # this:
Start-Sleep -Milliseconds 500; # New-IcingaPerformanceCounterCache $CounterResult;
# Internally we could do something like this
# $global:Icinga_PerfCounterCache += $CounterResult;
# Agreed, this is some sort of code duplication but it wouldn't make # Above we initialse ever single counter and we only require a sleep once
# any sense to create a own function for this. Why are we doing # in case a new, yet unknown counter was added
# this anway? if ($RequireSleep) {
# Simple: In case we found counters which have yet not been initialised Start-Sleep -Milliseconds 500;
# we did this above. Now we have waited 500 ms to receive proper
# values from these counters. As the previous generated result
# might have contained counters with 0 results, we will now
# check all counters again to receive the proper values.
# Agreed, might sound like a overhead, but the impact only
# applies to the first call of the module with the counters.
# This 'duplication' however decreased the execution from
# certain modules from 25s to 1s on the first run. Every
# additional run is then beeing executed within 0.x s
# which sounds like a very good performance and solution
$CounterResult = @{};
foreach ($counter in $CounterArray) {
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
}
}
return $CounterResult; # Agreed, this is some sort of code duplication but it wouldn't make
} # any sense to create a own function for this. Why are we doing
# this anway?
# Simple: In case we found counters which have yet not been initialised
# we did this above. Now we have waited 500 ms to receive proper
# values from these counters. As the previous generated result
# might have contained counters with 0 results, we will now
# check all counters again to receive the proper values.
# Agreed, might sound like a overhead, but the impact only
# applies to the first call of the module with the counters.
# This 'duplication' however decreased the execution from
# certain modules from 25s to 1s on the first run. Every
# additional run is then beeing executed within 0.x s
# which sounds like a very good performance and solution
$CounterResult = @{};
foreach ($counter in $CounterArray) {
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
}
}
return $CounterResult;
}