PowerShell / DSC

This repo is for the DSC v3 project
MIT License
133 stars 22 forks source link

Added DscResourceCache #432

Closed anmenaga closed 1 month ago

anmenaga commented 1 month ago

PR Summary

Added caching of Get-DscResource results to PSDscAdapter, reducing execution time for classic PS DSC resources:

Before:

PS C:\DSCv3> (Measure-Command {dsc.exe resource list -a "Microsoft.DSC/PowerShell"}).TotalSeconds
8.0908457
PS C:\DSCv3> (Measure-Command {"{'Name':'TestPSRepository1'}" | dsc resource set -r 'PSTestModule/TestPSRepository'}).TotalSeconds
12.1434988

After:

PS C:\DSCv3> (Measure-Command {dsc.exe resource list -a "Microsoft.DSC/PowerShell"}).TotalSeconds
2.3765501
PS C:\DSCv3> (Measure-Command {"{'Name':'TestPSRepository1'}" | dsc resource set -r 'PSTestModule/TestPSRepository'}).TotalSeconds
6.6152832

1) Cache location is in user-specific folders: on Windows - $env:LocalAppData\dsc\PSAdapterCache.json for WindowsPS adapter - $env:LocalAppData\dsc\WindowsPSAdapterCache.json on Linux/Mac - $env:HOME/.dsc/PSAdapterCache.json

2) Checks for stale cache: a) cache contains every subfolder (+1 level deep to account for new module versions) in PSModulePath (i.e. all visible module paths), which is analyzed when cache is accessed. I.e. if PSModulePath changes or a module is added/removed from existing PSModulePath, that is detected, and cache is rebuilt. b) for each resource type cache also contains all "*.ps1","*.psd1","*psm1","*.mof" files under $dscResource.ParentPath and their LastWrite timestamps. On accessing the cache LastWrite times of live files is compared with those in cache and cache is rebuilt if they are different.

anmenaga commented 1 month ago

PR description updated; @SteveL-MSFT please take another look.