Skip to content

Commit a49a532

Browse files
authored
Merge pull request #410 from Icinga:fix/improve_test_for_add_type
Fix: Performance for Add-Type testing With recent versions we added a feature to test if Add-Type functions are added to the current PowerShell session, ensuring that the same type is not re-added which caused errors on the error stack. While the code works perfectly fine, it takes a long time for execution which causes the plugin `Invoke-IcingaCheckDiskHealth` for example, to take way longer than usual. This PR prepares the future `Private` and `Public` section of the Icinga for Windows environment variables and adds an internal, quick test if a Add-Type function is already added to the current session, once being tested with the old behaviour. This reduces the disk health plugin execution time after the first run from around 10 seconds to 2 seconds.
2 parents 2832383 + 32c4541 commit a49a532

File tree

6 files changed

+137
-13
lines changed

6 files changed

+137
-13
lines changed

doc/100-General/10-Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1717
* [#403](https://github.com/Icinga/icinga-powershell-framework/pull/403) Fixes memory leak on newly EventLog reader for CLI event stream
1818
* [#407](https://github.com/Icinga/icinga-powershell-framework/pull/407) Removes unnecessary module import inside `Invoke-IcingaNamespaceCmdlets`
1919

20+
### Enhancements
21+
22+
* [#388](https://github.com/Icinga/icinga-powershell-framework/issues/388) Improves performance for testing if `Add-Type` functions have been added, by adding an internal test for newly introduced environment variables within a PowerShell session
23+
2024
## 1.7.1 (2021-11-11)
2125

2226
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/22?closed=1)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Reads a private environment variable from Icinga for Windows
4+
of the current PowerShell session
5+
.DESCRIPTION
6+
Reads a private environment variable from Icinga for Windows
7+
of the current PowerShell session
8+
.PARAMETER Name
9+
The name of the variable to load the content from
10+
.EXAMPLE
11+
Get-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions';
12+
.NOTES
13+
General notes
14+
#>
15+
function Get-IcingaPrivateEnvironmentVariable()
16+
{
17+
param (
18+
[string]$Name
19+
);
20+
21+
if ([string]::IsNullOrEmpty($Name)) {
22+
return $null;
23+
}
24+
25+
# Setup the environments in case not present already
26+
New-IcingaEnvironmentVariable;
27+
28+
if ($global:Icinga.Private.ContainsKey($Name) -eq $FALSE) {
29+
return $null;
30+
}
31+
32+
return $global:Icinga.Private[$Name];
33+
}

lib/core/framework/New-IcingaCheckSchedulerEnvironment.psm1

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,5 @@ function New-IcingaCheckSchedulerEnvironment()
4444
$IcingaDaemonData.IcingaThreadContent.Add('Scheduler', @{ });
4545
}
4646

47-
if ($null -eq $global:Icinga) {
48-
$global:Icinga = @{ };
49-
}
50-
51-
if ($global:Icinga.ContainsKey('CheckResults') -eq $FALSE) {
52-
$global:Icinga.Add('CheckResults', @());
53-
}
54-
if ($global:Icinga.ContainsKey('PerfData') -eq $FALSE) {
55-
$global:Icinga.Add('PerfData', @());
56-
}
57-
if ($global:Icinga.ContainsKey('CheckData') -eq $FALSE) {
58-
$global:Icinga.Add('CheckData', @{ });
59-
}
47+
New-IcingaEnvironmentVariable;
6048
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<#
2+
.SYNOPSIS
3+
Creates all environment variables for Icinga for Windows for the
4+
PowerShell session
5+
.DESCRIPTION
6+
Creates all environment variables for Icinga for Windows for the
7+
PowerShell session
8+
.EXAMPLE
9+
New-IcingaEnvironmentVariable;
10+
#>
11+
12+
function New-IcingaEnvironmentVariable()
13+
{
14+
if ($null -eq $global:Icinga) {
15+
$global:Icinga = @{ };
16+
}
17+
18+
# Session specific configuration for this shell
19+
if ($global:Icinga.ContainsKey('Private') -eq $FALSE) {
20+
$global:Icinga.Add('Private', @{ });
21+
}
22+
23+
# Shared configuration for all threads
24+
if ($global:Icinga.ContainsKey('Public') -eq $FALSE) {
25+
$global:Icinga.Add('Public', [hashtable]::Synchronized(@{ }));
26+
}
27+
28+
if ($global:Icinga.ContainsKey('CheckResults') -eq $FALSE) {
29+
$global:Icinga.Add('CheckResults', @());
30+
}
31+
if ($global:Icinga.ContainsKey('PerfData') -eq $FALSE) {
32+
$global:Icinga.Add('PerfData', @());
33+
}
34+
if ($global:Icinga.ContainsKey('CheckData') -eq $FALSE) {
35+
$global:Icinga.Add('CheckData', @{ });
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<#
2+
.SYNOPSIS
3+
Sets a private variable for the Icinga for Windows environment
4+
to use within the current PowerShell Session
5+
.DESCRIPTION
6+
Sets a private variable for the Icinga for Windows environment
7+
to use within the current PowerShell Session
8+
.PARAMETER Name
9+
The name of the variable
10+
.PARAMETER Value
11+
The value the variable will be assigned with
12+
.EXAMPLE
13+
Set-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions' -Value @{ 'IcingaDiskAttributes', $TRUE };
14+
#>
15+
16+
function Set-IcingaPrivateEnvironmentVariable()
17+
{
18+
param (
19+
[string]$Name,
20+
$Value
21+
);
22+
23+
if ([string]::IsNullOrEmpty($Name)) {
24+
return;
25+
}
26+
27+
# Setup the environments in case not present already
28+
New-IcingaEnvironmentVariable;
29+
30+
if ($global:Icinga.Private.ContainsKey($Name) -eq $FALSE) {
31+
$global:Icinga.Private.Add($Name, $Value);
32+
return;
33+
}
34+
35+
$global:Icinga.Private[$Name] = $Value;
36+
}

lib/core/tools/Test-IcingaAddTypeExist.psm1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
<#
2+
.SYNOPSIS
3+
Tests if a Add-Type function is already installed inside the current
4+
PowerShell session
5+
.DESCRIPTION
6+
Tests if a Add-Type function is already installed inside the current
7+
PowerShell session
8+
.PARAMETER Type
9+
The name of the function being added
10+
.EXAMPLE
11+
Test-IcingaAddTypeExis -Type 'IcingaDiskAttributes';
12+
#>
113
function Test-IcingaAddTypeExist()
214
{
315
param (
@@ -8,8 +20,22 @@ function Test-IcingaAddTypeExist()
820
return $FALSE;
921
}
1022

23+
[hashtable]$LoadedTypes = Get-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions';
24+
25+
if ($null -eq $LoadedTypes) {
26+
$LoadedTypes = @{ };
27+
}
28+
29+
if ($LoadedTypes.ContainsKey($Type)) {
30+
return $TRUE;
31+
}
32+
1133
foreach ($entry in [System.AppDomain]::CurrentDomain.GetAssemblies()) {
1234
if ($entry.GetTypes() -Match $Type) {
35+
$LoadedTypes.Add($Type, $TRUE);
36+
37+
Set-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions' -Value $LoadedTypes;
38+
1339
return $TRUE;
1440
}
1541
}

0 commit comments

Comments
 (0)