Created
September 21, 2019 07:58
-
-
Save IISResetMe/9379d52cc51ee1e1b6771af1394fb423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module PSCache | |
# Define fetcher | |
$Fetcher = { | |
# Emulate a potentially slow operation | |
Start-Sleep -Milliseconds (Get-Random -Minimum 15 -Maximum 30) | |
# Return an object corresponding to $_ | |
[pscustomobject]@{ | |
ID = $_ | |
Data = "SomeValue${_}" | |
} | |
} | |
# Create PSCache instance with LRU policy | |
$LRUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy LRU -Capacity 100 | |
# Create PSCache instance with LFU policy | |
$LFUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy LFU -Capacity 100 | |
# Create PSCache instance with Segmented LRU policy | |
$SLRUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy SLRU -Capacity 50 | |
# Prepare our access pattern | |
$IDs = 1..1000 |ForEach-Object { | |
1..200 |Get-Random | |
} | |
# Without Caching | |
Write-Host "Testing semi-random access pattern:" -ForegroundColor Green | |
Measure-Command { | |
$IDs |ForEach-Object -Process $Fetcher | |
} |Select-Object @{N='Strategy';E={'No Cache'}},TotalSeconds | |
# With LRU | |
Measure-Command { | |
$IDs |ForEach-Object { | |
$LRUcache.Get($_) | |
} | |
} |Select-Object @{N='Strategy';E={'Evict Least-Recently Used'}},TotalSeconds | |
# With LFU | |
Measure-Command { | |
$IDs |ForEach-Object { | |
$LFUcache.Get($_) | |
} | |
} |Select-Object @{N='Strategy';E={'Evict Least-Frequently Used'}},TotalSeconds | |
# With SLRU | |
Measure-Command { | |
$IDs |ForEach-Object { | |
$SLRUcache.Get($_) | |
} | |
} |Select-Object @{N='Strategy';E={'Evict Segmented LRU'}},TotalSeconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment