Test-FastLookup.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
function Test-FastLookup {
    <#
    .SYNOPSIS
        Designed to optimize the speed of searching an array.
 
    .DESCRIPTION
        Improve the speed of looking up a value in an array by creating a hashtable index.
        Good for looping through very large arrays or CSV files
 
    .NOTES
        Version: 1.2
        Author: Miles Gratz
        Creation Date: April 20, 2017
        Purpose/Change: Improve examples
  
    .OUTPUTS
        1. Creates a sample array
        2. Measures speed of searching array with Where-Object, Foreach/If, and Get-FastLookup
     
    .EXAMPLE
         
        PS> Test-FastLookup -ArraySize 100000 -SearchSize 10000
 
        Creating 'speedtest' array (total array size: 100000; search size: 10000)
        Example object:
 
        DeviceName : DeviceName9475
        Type : Security Camera
        IP : 10.10.128.62
        VLAN : VLAN86
        Location : Los Angeles
        Patch Window : Saturday
        Warranty Service Level : 24x7
        Warranty Days Left : 124
        Primary Technical Contact : Chris
        Application Manager : Doug
        Severity Level : Harry
        Technical Emergency Contact Number : P1
        Manager Emergency Contact Number : 515-249-1602
 
 
        ---------------------------------------------------------------
 
        Measuring speed of Where-Object lookup:
         
        $Results = $Array | Where-Object { $_.DeviceName -in $Search }
   
  Days : 0
        Hours : 0
        Minutes : 4
        Seconds : 18
        Milliseconds : 366
   
 
  ---------------------------------------------------------------
 
        Measuring speed of Foreach/If lookup:
         
        [System.Collections.ArrayList]$Results = @()
        foreach ($Item in $Array)
        {
            If ($Item.DeviceName -in $Search)
            {
                [void]$Results.Add($Item)
            }
        }    
         
        Days : 0
        Hours : 0
        Minutes : 4
        Seconds : 14
        Milliseconds : 148
   
 
  ---------------------------------------------------------------
 
        Measuring speed of Get-FastLookup lookup:
         
        $Hashtable = New-FastLookup -Array $Array -Header "DeviceName"
        [System.Collections.ArrayList]$Results = @()
        foreach ($Item in $Search)
        {
            [void]$Results.Add((Get-FastLookup -Value $Item -Array $Array -Table $Hashtable))
        }
 
        Days : 0
        Hours : 0
        Minutes : 0
        Seconds : 52
        Milliseconds : 773
 
 
        --------------------------------------------------------------
     
        [NOTE] Performance test on Windows 10 x64 (i5-6200U/8GB/SSD)
 
    #>

    param(
        # Default sample size of 100K, searching for 10K objects
        [int]$ArraySize = 100000,
        [int]$SearchSize = 10000
    )

    # Define empty ArrayList for sample array
    [System.Collections.ArrayList]$csvLines = @()

    # Create array based on quantity
    $Index = 0
    Write-Output "Creating 'speedtest' array (total array size: $ArraySize; search size: $SearchSize)"
    while ($Index -lt $ArraySize)
    {
        # Calculate percentage
        $PercentageIndex = $Percentage
        $Percentage = [math]::Round((($Index / $ArraySize)*100))

        # Announce percentage
        If ($Percentage -gt $PercentageIndex)
        {
            Write-Output "Creating 'speedtest' array ($Percentage%)"
        }
        
        # Example template
        $DeviceNumber = Get-Random -Maximum $($ArraySize*5)
        $Type = Get-Random 'Laptop','Workstation','Network Switch','Wireless Access Point','Security Camera','Server'
        $IP =  "10.10.128." + (Get-Random (1..254))
        $VLAN = "VLAN" + (Get-Random (1..100))
        $Location = Get-Random 'Chicago','New York','Los Angeles'
        $PatchWindow = Get-Random 'Sunday','Saturday'
        $WarrantyServiceLevel = Get-Random '24x7','Next Business Day','Parts Only'
        $WarrantyDaysLeft = Get-Random -Minimum -100 -Maximum 1095
        $PrimaryTech = Get-Random 'Angela','Bob','Chris'
        $SecondaryTech = Get-Random 'Doug','Eileen','Francis'
        $AppManager = Get-Random 'Grace','Harry','Isabel'
        $SeverityLevel = Get-Random 'P1','P2','P3','P4'
        $TechEmergencyNum = "515-249-" + (0..9 | Get-Random) + (0..9 | Get-Random) + (0..9 | Get-Random) + (0..9 | Get-Random)
        $MgrEmergencyNum = "515-249-" + (0..9 | Get-Random) + (0..9 | Get-Random) + (0..9 | Get-Random) + (0..9 | Get-Random)
        
        # Create custom object to add to array
        $csvLine = (('"DeviceName' + $DeviceNumber),
                    $Type,
                    $IP,
                    $VLAN,
                    $Location,
                    $PatchWindow,
                    $WarrantyServiceLevel,
                    $WarrantyDaysLeft,
                    $PrimaryTech,
                    $SecondaryTech,
                    $AppManager,
                    $SeverityLevel,
                    $TechEmergencyNum,
                    ($MgrEmergencyNum + '"')) -join '","'
        
        # Add object to array
        [void]$csvLines.Add($csvLine)
    
        # Increment loop
        $Index++
    }

    # Convert lines to CSV
    [string[]]$stringArray += '"DeviceName","Type","IP","VLAN","Location","Patch Window","Warranty Service Level","Warranty Days Left","Primary Technical Contact","Application Manager","Severity Level","Technical Emergency Contact Number","Manager Emergency Contact Number"'
    $stringArray += $csvLines
    [array]$Array = $stringArray | ConvertFrom-Csv
    
    # Create list of random server names
    $Index = 0
    [System.Collections.ArrayList]$Search = @()

    while ($Index -lt $SearchSize)
    {
        $RandomIndex = Get-Random -Minimum 0 -Maximum ($Array.Count-1)
        $Random = $Array[$RandomIndex]
        [void]$Search.Add($Random.DeviceName)
        $Index++
    }

    # Example object
    Write-Output ""
    Write-Output "Example object:"
    Write-Output $Array[0]

    # Measuring speed of Where-Object lookup
    Write-Output ""
    Write-Output ('-'*80)
    Write-Output 'Measuring speed of Where-Object lookup:'
    Write-Output ""
    Write-Output ('$Results = $Array | Where-Object { $_.DeviceName -in $Search }')
    $MeasureWhereObject = Measure-Command { 
        $Results = $Array | Where-Object { $_.DeviceName -in $Search }
    } | Select-Object Days,Hours,Minutes,Seconds,Milliseconds
    Write-Output $MeasureWhereObject
    Write-Output ""
    Write-Output "Number of results: $($Results.Count)"
    Write-Output "First result:"
    Write-Output ""
    Write-Output $Results[0]
    
     # Measuring speed of ForEach lookup
    Write-Output ""
    Write-Output ('-'*80)
    Write-Output 'Measuring speed of Foreach/If lookup:'
    Write-Output ""
    Write-Output '[System.Collections.ArrayList]$Results = @()'
    Write-Output 'foreach ($Item in $Array)'
    Write-Output '{'
    Write-Output ' If ($Item.DeviceName -in $Search)' 
    Write-Output ' {'
    Write-Output ' [void]$Results.Add($Item)'
    Write-Output ' }'
    Write-Output '}'
    $MeasureForeachIf = Measure-Command {
        [System.Collections.ArrayList]$Results = @()
        foreach ($Item in $Array)
        { 
            If ($Item.DeviceName -in $Search)
            { 
                [void]$Results.Add($Item)
            } 
        }
    } | Select-Object Days,Hours,Minutes,Seconds,Milliseconds
    Write-Output $MeasureForeachIf
    Write-Output ""
    Write-Output "Number of results: $($Results.Count)"
    Write-Output "First result:"
    Write-Output ""
    Write-Output $Results[0]
 
    # Measuring speed of Get-FastLookup
    Write-Output ""
    Write-Output ('-'*80)
    Write-Output 'Measuring speed of Get-FastLookup lookup:'
    Write-Output ""
    Write-Output '$Hashtable = New-FastLookup -Array $Array -Header "DeviceName"'
    Write-Output '[System.Collections.ArrayList]$Results = @()'
    Write-Output 'foreach ($Item in $Search)'
    Write-Output '{'
    Write-Output ' [void]$Results.Add((Get-FastLookup -Value $Item -Array $Array -Table $Hashtable))'
    Write-Output '}'
    $MeasureFastLookup = Measure-Command {
        $Hashtable = New-FastLookup -Array $Array -Header "DeviceName"
        [System.Collections.ArrayList]$Results = @()
        foreach ($Item in $Search)
        {  
            [void]$Results.Add((Get-FastLookup -Value $Item -Array $Array -Table $Hashtable))
        }
    } | Select-Object Days,Hours,Minutes,Seconds,Milliseconds
    Write-Output ""
    Write-Output $MeasureFastLookup
    Write-Output ""
    Write-Output "Number of results: $($Results.Count)"
    Write-Output "First result:"
    Write-Output ""
    Write-Output $Results[0]
}