SHARE
TWEET

Untitled

a guest Oct 9th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module PSCache
  2.  
  3. # Define fetcher
  4. $Fetcher = {
  5.     # Emulate a potentially slow operation
  6.     Start-Sleep -Milliseconds (Get-Random -Minimum 15 -Maximum 30)
  7.  
  8.     # Return an object corresponding to $_
  9.     [pscustomobject]@{
  10.         ID   = $_
  11.         Data = "SomeValue${_}"
  12.     }
  13. }
  14.  
  15. # Create PSCache instance with LRU policy
  16. $LRUcache  = New-PSCache -Fetcher $Fetcher -EvictionPolicy LRU -Capacity 100
  17.  
  18. # Create PSCache instance with LFU policy
  19. $LFUcache  = New-PSCache -Fetcher $Fetcher -EvictionPolicy LFU -Capacity 100
  20.  
  21. # Create PSCache instance with Segmented LRU policy
  22. $SLRUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy SLRU -Capacity 50
  23.  
  24. # Prepare our access pattern
  25. $IDs = 1..1000 |ForEach-Object {
  26.     1..200 |Get-Random
  27. }
  28.  
  29. # Without Caching
  30. Write-Host "Testing semi-random access pattern:" -ForegroundColor Green
  31. Measure-Command {
  32.     $IDs |ForEach-Object -Process $Fetcher
  33. } |Select-Object @{N='Strategy';E={'No Cache'}},TotalSeconds
  34.  
  35. # With LRU
  36. Measure-Command {
  37.     $IDs |ForEach-Object {
  38.         $LRUcache.Get($_)
  39.     }
  40. } |Select-Object @{N='Strategy';E={'Evict Least-Recently Used'}},TotalSeconds
  41.  
  42. # With LFU
  43. Measure-Command {
  44.     $IDs |ForEach-Object {
  45.         $LFUcache.Get($_)
  46.     }
  47. } |Select-Object @{N='Strategy';E={'Evict Least-Frequently Used'}},TotalSeconds
  48.  
  49. # With SLRU
  50. Measure-Command {
  51.     $IDs |ForEach-Object {
  52.         $SLRUcache.Get($_)
  53.     }
  54. } |Select-Object @{N='Strategy';E={'Evict Segmented LRU'}},TotalSeconds
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top