Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of Get-GitHubContent #232

Merged
merged 2 commits into from
Jun 10, 2020

Conversation

HowardWolosky
Copy link
Member

@HowardWolosky HowardWolosky commented Jun 10, 2020

Description

The call to ConvertTo-SmarterObject was adding a significant amount of time to the execution of Get-GitHubContent.

    Set-GitHubConfiguration -DisableSmarterObjects:$false
    Measure-Command { $c = Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path README.md }
    # This averages to be around 1.2 seconds

    Set-GitHubConfiguration -DisableSmarterObjects:$true
    Measure-Command { $c = Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path README.md }
    # This averages to be around 14 seconds

The reasoning behind this was that the return of Invoke-WebRequest for this repo's README.md was an array of [int32] that contained 22,000+ entries (one byte per entry). Because the array was created by ConverFrom-Json, each [int32] was also an [object], which meant that it was considered a [PSCustomObject] inside of ConverTo-SmarterObject, which caused it to do a lot of unnecessary work. Piping in 22,000 objects and doing all of that unnecessary work to end up just calling Write-Output on the original value took a lot of time.

The fix here was to skip trying to convert the result to a smarter object if it wasn't going to be a convertible object in the first place.

I also added in protection directly to ConverTo-SmarterObject as well. It made things better than the current runtime, but still twice as slow as not calling it in the first place (average runtime of above command was 3.5 seconds when we allowed it to go into the command and then do nothing).

This doesn't end up saving much time with the execution of Tests/GitHubContents.tests.ps1 because the file being returned is so small, but this should have some nice user experience improvements in practical usage.

Issues Fixed

None

References

n/a

Checklist

  • You actually ran the code that you just wrote, especially if you did just "one last quick change".
  • [] Comment-based help added/updated, including examples.
  • Static analysis is reporting back clean.
  • New/changed code adheres to our coding guidelines.
  • Changes to the manifest file follow the manifest guidance.
  • Unit tests were added/updated and are all passing. See testing guidelines.
  • Relevant usage examples have been added/updated in USAGE.md.
  • If desired, ensure your name is added to our Contributors list

The call to `ConvertTo-SmarterObject` was adding a significant amount
of time to the execution of Get-GitHubContent.

```powershell
    Set-GitHubConfiguration -DisableSmarterObjects:$false
    Measure-Command { $c = Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path README.md }
    # This averages to be around 1.2 seconds

    Set-GitHubConfiguration -DisableSmarterObjects:$true
    Measure-Command { $c = Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path README.md }
    # This averages to be around 14 seconds
```

The reasoning behind this was that the return of `Invoke-WebRequest`
for this repo's `README.md` was an `[Object[]]` that contained 22,000+
entries (one byte per entry).  Piping in 22,000 objects and just writing
each of them out to via `Write-Output` took a _lot_ of time.

The fix here was to skip trying to convert the result to a smarter object
if it wasn't going to be a convertible object in the first place.

This doesn't end up saving much time with the execution of
`Tests/GitHubContents.tests.ps1` because the file being returned
is so small, but this should have some nice user experience improvements
in practical usage.
@HowardWolosky HowardWolosky added the bug This relates to a bug in the existing module. label Jun 10, 2020
@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky HowardWolosky merged commit 7818776 into microsoft:master Jun 10, 2020
@HowardWolosky HowardWolosky deleted the contentsPerf branch June 10, 2020 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This relates to a bug in the existing module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant