-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[Perf] corehttp - updating async stream upload httpx test #34674
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
Merged
swathipil
merged 4 commits into
Azure:main
from
swathipil:swathipil/corehttp/async-stream-httpx-test
Mar 8, 2024
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sdk/core/corehttp/tests/perf_tests/_async_iterator_random_stream.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| from devtools_testutils.perfstress_tests._random_stream import get_random_bytes, _DEFAULT_LENGTH | ||
|
|
||
|
|
||
| # The AsyncIteratorRandomStream is used specifically for the AsyncHttpXTransport upload stream scenario, since the | ||
| # async httpx transport requires the request body stream to be type AsyncIterator (i.e. have an __aiter__ and __anext__ method rather than __iter__). | ||
| # Specific check in httpx here: https://github.com/encode/httpx/blob/7df47ce4d93a06f2c3310cd692b4c2336d7663ba/httpx/_content.py#L116. | ||
|
|
||
| class AsyncIteratorRandomStream(): | ||
| def __init__(self, length, initial_buffer_length=_DEFAULT_LENGTH): | ||
| super().__init__() | ||
| self._base_data = get_random_bytes(initial_buffer_length) | ||
| self._data_length = length | ||
| self._base_buffer_length = initial_buffer_length | ||
| self._position = 0 | ||
| self._remaining = length | ||
| self._closed = False | ||
|
|
||
| def __len__(self): | ||
| return self._remaining | ||
|
|
||
| def __aiter__(self): | ||
| return self | ||
|
|
||
| async def __anext__(self): | ||
| if self._remaining == 0: | ||
| raise StopAsyncIteration | ||
| return self.read() | ||
|
|
||
| def reset(self): | ||
| self._position = 0 | ||
| self._remaining = self._data_length | ||
| self._closed = False | ||
|
|
||
| def read(self, size=None): | ||
| if self._remaining == 0: | ||
| return b"" | ||
|
|
||
| if size is None: | ||
| e = self._base_buffer_length | ||
| else: | ||
| e = size | ||
| e = min(e, self._remaining) | ||
| if e > self._base_buffer_length: | ||
| self._base_data = get_random_bytes(e) | ||
| self._base_buffer_length = e | ||
| self._remaining = self._remaining - e | ||
| self._position += e | ||
| return self._base_data[:e] | ||
|
|
||
| def seek(self, index, whence=0): | ||
swathipil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if whence == 0: | ||
| self._position = index | ||
| elif whence == 1: | ||
| self._position = self._position + index | ||
| elif whence == 2: | ||
| self._position = self._data_length - 1 + index | ||
|
|
||
| def tell(self): | ||
| return self._position | ||
|
|
||
| def remaining(self): | ||
| return self._remaining | ||
|
|
||
| def close(self): | ||
| self._closed = True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.