RamblingCookieMonster / PSDepend

PowerShell Dependency Handler
MIT License
285 stars 76 forks source link

Only check PSGallery when required #81

Closed mrhockeymonkey closed 6 years ago

mrhockeymonkey commented 6 years ago

it seems that if you specify a dependency specifying a version like so:

@{
    psake = @{
        Version = '1.2.3'
    }
}

then running Invoke-PSDepend -Test will query PSGallery for each module BEFORE checking if it is installed locally which is quite slow. Could we not instead just check the required module exists at that version locally and if it does move on to the next without having to wait for a response from PSGallery?

Obviously when using 'latest' you will want to query PSGallery every time but for me personally I like to strictly define my expected modules.

I appreciate speed probably isn't the main driver here but its always nice as long as it doesn't affect outcome. Your thoughts?

RamblingCookieMonster commented 6 years ago

Hiyo! Yeah, that's certainly unintentional, unless there was some weird reason requiring it - thanks for the heads up, will poke around!

RamblingCookieMonster commented 6 years ago

I don't even... Yeah : ) Will change that, hopefully this weekend - thanks again for the heads up!

RamblingCookieMonster commented 6 years ago

Weird! Do you see it go out and check the gallery? Will have to test this out, but logic wise, it seems like it should be skipping before calling Find-Module per this - The Find-Module call is in a scriptblock that isn't invoked until later on (the &)

Cheers!

mrhockeymonkey commented 6 years ago

You are indeed correct. apologies i assumed it was checking psgallery each time due to the verbose out put:

VERBOSE: Getting dependency [psake] from PowerShell repository [GrPsGet]
VERBOSE: Populating RepositorySourceLocation property for module psake.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\psake\4.6.0\psake.psm1'.
VERBOSE: Found existing module [psake]
VERBOSE: You have the requested version [4.6.0] of [psake]

Juts had a poke around and its actually Get-PSDependType thats taking up the time. This is called a few times it seems during the process here and here

i wrapped these calls in measure-command to quantify and its about 10 seconds each call which is why i thought it was speaking to the gallery. here is the full output:

PS C:\Program Files\WindowsPowerShell\Modules\psdepend\0.2.1> Invoke-PSDepend -Path D:\Code\DSCDemo -verbose -force
VERBOSE: Running Invoke-PSDepend with ParameterSetName 'installimport-file', PSDependAction Install, and params:
Key     Value
---     -----
Path    {D:\Code\DSCDemo}
Verbose True
Force   True

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 10
Milliseconds      : 17
Ticks             : 100175520
TotalDays         : 0.000115943888888889
TotalHours        : 0.00278265333333333
TotalMinutes      : 0.1669592
TotalSeconds      : 10.017552
TotalMilliseconds : 10017.552

VERBOSE: done1
VERBOSE: Working with [1] dependency files from [D:\Code\DSCDemo]:
D:\Code\DSCDemo\requirements.psd1

VERBOSE: Running Invoke-DependencyScript with ParameterSetName 'Map' and params:
Key              Value
---              -----
PSDependAction   {Install}
PSDependTypePath C:\Program Files\WindowsPowerShell\Modules\PSDepend\0.2.1\PSDependMap.psd1
Dependency       @{DependencyFile=D:\Code\DSCDemo\requirements.psd1; DependencyName=psake; DependencyType=PSGalleryModule; Name=; Version=4.6.0; Parameters=System.Collections.Hashtable; Source=; Target=; AddToPath=; Tags=; DependsOn=; PreScripts=; PostScripts=; PSDependOptions=; Raw=System.Collecti...

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 9
Milliseconds      : 819
Ticks             : 98196420
TotalDays         : 0.000113653263888889
TotalHours        : 0.00272767833333333
TotalMinutes      : 0.1636607
TotalSeconds      : 9.819642
TotalMilliseconds : 9819.642

VERBOSE: done
VERBOSE: Dependencies:

DependencyFile  : D:\Code\DSCDemo\requirements.psd1
DependencyName  : psake
DependencyType  : PSGalleryModule
Name            :
Version         : 4.6.0
Parameters      : {Repository}
Source          :
Target          :
AddToPath       :
Tags            :
DependsOn       :
PreScripts      :
PostScripts     :
PSDependOptions :
Raw             : {Version, Parameters}

VERBOSE: Searching for C:\Program Files\WindowsPowerShell\Modules\PSDepend\0.2.1\PSDependScripts\PSGalleryModule.ps1
VERBOSE: Searching for PSGalleryModule.ps1
VERBOSE: Get-Parameters for C:\Program Files\WindowsPowerShell\Modules\PSDepend\0.2.1\PSDependScripts\PSGalleryModule.ps1\PSGalleryModule.ps1
VERBOSE: Found parameters [AllowClobber Dependency Import PSDependAction Repository SkipPublisherCheck]
VERBOSE: Invoking 'C:\Program Files\WindowsPowerShell\Modules\PSDepend\0.2.1\PSDependScripts\PSGalleryModule.ps1' with parameters
Name                           Value
----                           -----
PSDependAction                 {Install}
Dependency                     @{DependencyFile=D:\Code\DSCDemo\requirements.psd1; DependencyName=psake; DependencyType=PSGalleryModule; Name=; Version=4.6.0; Parameters=System.Collections.Hashtable; Source=; Target=; AddToPath=; Tags=; DependsOn=; PreScripts=; PostScripts=; PSDependOptions=; Raw=S...
Repository                     GrPsGet

VERBOSE: Getting dependency [psake] from PowerShell repository [GrPsGet]
VERBOSE: Populating RepositorySourceLocation property for module psake.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\psake\4.6.0\psake.psm1'.
VERBOSE: Found existing module [psake]
VERBOSE: You have the requested version [4.6.0] of [psake]

I will try this at home today and see if i get the same on my laptop. it might be something odd in my work environment. wouldn't be the first time.

RamblingCookieMonster commented 6 years ago

Hmm. Maybe I should add timestamps and other data to help diagnose performance issues. Speed isn't the goal, but is certainly a factor!

At least on my system, it's not terrible:

PS /Users/wframe/sc/PSDepend> Measure-Command {get-psdependtype} | select totalseconds

TotalSeconds
------------
    0.145524

That said, might not be the case here, but I've seen AV slow down tasks that hit the filesystem significantly (e.g. modules that dot sourced individual function files took a few seconds per file)

Cheers!

mrhockeymonkey commented 6 years ago

Managed to take another look and it was the Get-Help call that was taking the time. typical that I couldn't replicate the 10 second wait anymore but now its ~2 seconds. I've fixed it my end by parametizing the help

Makes it more zippy:

PS D:\Code\dscdemo> Measure-Command {Get-PSDependType}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 2
Milliseconds      : 473
Ticks             : 24732520
TotalDays         : 2.86256018518519E-05
TotalHours        : 0.000687014444444444
TotalMinutes      : 0.0412208666666667
TotalSeconds      : 2.473252
TotalMilliseconds : 2473.252

PS D:\Code\dscdemo> Measure-Command {Get-PSDependType -SkipHelp}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 16
Ticks             : 169921
TotalDays         : 1.96667824074074E-07
TotalHours        : 4.72002777777778E-06
TotalMinutes      : 0.000283201666666667
TotalSeconds      : 0.0169921
TotalMilliseconds : 16.9921

However I kinda of realize I'm doing things wrong anyway. As you say speed isn't the goal and the reason it was an issue for me was because I was running invoke-PSDepend every time I build. I've stopped that now and just do it once when i start a change and let CI do it each time instead. Kinda like "If you keep getting stung try not walking through nettles".

Im happy to close this unless you think that commit is worth a pull? Thanks for the feedback.

RamblingCookieMonster commented 6 years ago

Hiyo! Completely forgot to reply - good catch! And sure, would absolutely merge that : D

Cheers!

RamblingCookieMonster commented 6 years ago

Thanks again! Now I'm curious to see where else I might have something slow and unnecessary in the typical (install) context!