-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Use new JSON Streaming methods for Invoke-RestMethod #15697
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
Use new JSON Streaming methods for Invoke-RestMethod #15697
Conversation
…lets pass it directly instead of having to worry about properties
|
@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. |
|
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 The main problem is that the MSFT team is too small to review everything PR quickly. 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.
|
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! |
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. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
TravisEz13
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
|
Converted to draft, please marks as ready for review when the parent PR is merged. |
|
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. |
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:
ThisPR:
ThisPR -AsHashtable:
Benchmarks for a 100MB JSON response with Object root element
7.1.3
This PR:
This PR -AsHashtable
Benchmarks for a 12MB JSON Response
7.1.3:
ThisPR:
Runtime is misleading as I am hitting a real API
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).