Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Use new JSON Streaming methods for Invoke-RestMethod#15697

Closed
strawgate wants to merge 15 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
strawgate:json-optimization-streaming-webrequeststrawgate/PowerShell:json-optimization-streaming-webrequestCopy head branch name to clipboard
Closed

Use new JSON Streaming methods for Invoke-RestMethod#15697
strawgate wants to merge 15 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
strawgate:json-optimization-streaming-webrequeststrawgate/PowerShell:json-optimization-streaming-webrequestCopy head branch name to clipboard

Conversation

@strawgate

@strawgate strawgate commented Jun 30, 2021

Copy link
Copy Markdown
Contributor

PR Summary

This PR is a "child" PR to: #15695

This PR is in good shape but needs tests for the additional parameter on invoke-restmethod for -ashashtable

Essentially if the streaming approach from that PR is approved we can make new methods which take a stream directly instead of the string body. We can then wire up invoke-restmethod so that it takes the response stream and sends it directly to newtonsoft.

This should improve all use-cases of invoke-restmethod but will particularly improve cases where the source API has an array as its root element.

PR Context

This should greatly reduce the memory usage of the invoke-restmethod cmdlets and somewhat reduce the execution time/cpu time.

Benchmarks for a 100MB JSON Response with Array root element (Time to first byte from the API is about 8s so for realistic times subtract 8000ms from each runtime number)

7.1.3:

  • Memory Usage: 1597 MB
  • Runtime: 20679.9797 ms
  • Est Runtime: 12679.9797 ms

ThisPR:

  • Memory Usage: 718.55 MB
  • Runtime: 16323.1985 ms
  • Est Runtime: 8323.1985 ms

ThisPR -AsHashtable:

  • Memory Usage: 415.41 MB
  • Runtime: 13592.6211 ms
  • Est Runtime: 5592.6211 ms

Benchmarks for a 100MB JSON response with Object root element
7.1.3

  • Memory Usage: 1672.84765625 MB
  • Runtime: 12771.3602 ms
  • Est Runtime: 12771.3602 ms

This PR:

  • Memory Usage: 1489.90625 MB
  • Runtime: 12222.5628 ms

This PR -AsHashtable

  • Memory Usage: 1146.078125 MB
  • Runtime: 11367.9809 ms

Benchmarks for a 12MB JSON Response
7.1.3:

  • Memory Usage: 268.9921875 MB
  • Runtime: 2553.9797 ms

ThisPR:

  • Memory Usage: 100.05078125 MB
  • Runtime: 1950.1985 ms

Runtime is misleading as I am hitting a real API

PR Checklist

@ghost ghost assigned TravisEz13 Jun 30, 2021
@iSazonov

iSazonov commented Jun 30, 2021

Copy link
Copy Markdown
Collaborator

@strawgate I believe we have had a consensus that we froze current Json cmdlets based on NewtonSoft and implement new ones based on .Net API as experimental feature.

@strawgate

strawgate commented Jun 30, 2021

Copy link
Copy Markdown
Contributor Author

I looked around a fair amount for information on the status of System.Text.Json and all I found was a couple 2 year old PRs that haven't moved.

My goal was to offer a highly-compatible set of changes that make a big impact on performance while we wait for System.Text.Json. The changes outlined here cut memory usage to 1/4 of what it was previously if the root element is an array and ~75% of what it was if it's an object.

The high memory usage of Powershell with API use-cases was the primary reason we moved most of our projects to C#. I've talked with a lot of people who have moved to C# or Python due to the overhead related to parsing JSON. Instead of complaining I figured i'd take a stab at it and get some experience working with the codebase.

If you guys don't want to merge the changes that's totally fine!

@strawgate strawgate changed the title WIP: Use new JSON Streaming methods for Invoke-RestMethod Use new JSON Streaming methods for Invoke-RestMethod Jun 30, 2021
@iSazonov

Copy link
Copy Markdown
Collaborator

@strawgate The main problem is that the MSFT team is too small to review everything PR quickly.
They introduced Working Groups to get more developers from the community. If you want to work on commandlets in this repository, you can join one of the working groups.

The current strategic direction in the C# community is migration from NewtonSoft.JsonNet to System.Text.Json API. NewtonSoft has been found to be out of development. All new developers should be on System.Text.Json API.

It doesn't make sense to invest much in code based on NewtonSoft.JsonNet, only security issues, simple bugs/perfs.

If you are interested in working on these commandlets, the current conclusion is to implement new commandlets based on the System.Text.Json API as experimental features. You can grab my PRs then I could review your PRs.

…s prevents errors when a null is passed through.
@strawgate

strawgate commented Jun 30, 2021

Copy link
Copy Markdown
Contributor Author

I've been using https://www.mockaroo.com/ to generate random json data with numbers, dates, etc and have been using http://www.jsondiff.com/ to do a semantic compare between the 7.1.3 cmdlets and the ones in this PR by converting it from json and then back to json and comparing the two. So far so good!

@strawgate

Copy link
Copy Markdown
Contributor Author

@strawgate The main problem is that the MSFT team is too small to review everything PR quickly.
They introduced Working Groups to get more developers from the community. If you want to work on commandlets in this repository, you can join one of the working groups.

The current strategic direction in the C# community is migration from NewtonSoft.JsonNet to System.Text.Json API. NewtonSoft has been found to be out of development. All new developers should be on System.Text.Json API.

It doesn't make sense to invest much in code based on NewtonSoft.JsonNet, only security issues, simple bugs/perfs.

If you are interested in working on these commandlets, the current conclusion is to implement new commandlets based on the System.Text.Json API as experimental features. You can grab my PRs then I could review your PRs.

I can't find information on joining a specific working group, is that info available somewhere?

@iSazonov

iSazonov commented Jul 2, 2021

Copy link
Copy Markdown
Collaborator

I can't find information on joining a specific working group, is that info available somewhere?

You could e-mail directly @joeyaiello and @SteveL-MSFT.

@ghost

ghost commented Jul 9, 2021

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@ghost ghost added the Review - Needed The PR is being reviewed label Jul 9, 2021

@TravisEz13 TravisEz13 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preliminary review

}
}

private static List<object> DeserializeRootArrayHelper(JsonReader reader, JsonSerializer serializer, bool returnHashtable, out ErrorRecord error)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add description of the method and parameters

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Jul 12, 2021
@TravisEz13 TravisEz13 marked this pull request as draft July 12, 2021 19:16
@TravisEz13

Copy link
Copy Markdown
Member

Converted to draft, please marks as ready for review when the parent PR is merged.

@strawgate

Copy link
Copy Markdown
Contributor Author

I think this PR will be abandoned as there is no interest in improving the performance of the existing cmdlets due to backwards compatibility concerns.

@strawgate strawgate closed this Jul 12, 2021
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jul 12, 2021
@iSazonov

Copy link
Copy Markdown
Collaborator

I think this PR will be abandoned as there is no interest in improving the performance of the existing cmdlets due to backwards compatibility concerns.

All this could be in new experimental cmdlets based on System.Text.Json API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.