|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import aiohttp |
| 8 | +import requests |
| 9 | +from concurrent.futures import ThreadPoolExecutor |
| 10 | + |
| 11 | +from devtools_testutils.perfstress_tests import RandomStream |
| 12 | + |
| 13 | +from ._test_base import _BlobTest |
| 14 | + |
| 15 | + |
| 16 | +TOKEN_SCOPE = "https://storage.azure.com/.default" |
| 17 | + |
| 18 | +class DownloadBasicTest(_BlobTest): |
| 19 | + def __init__(self, arguments): |
| 20 | + super().__init__(arguments) |
| 21 | + |
| 22 | + async def global_setup(self): |
| 23 | + await super().global_setup() |
| 24 | + data = RandomStream(self.args.size) |
| 25 | + await self.async_blob_client.upload_blob(data, max_concurrency=10) |
| 26 | + |
| 27 | + self.chunk_size = self.args.max_block_size or 4 * 1024 * 1024 |
| 28 | + if self.async_token_credential: |
| 29 | + token = await self.async_token_credential.get_token(TOKEN_SCOPE) |
| 30 | + self.auth_header = "Bearer " + token.token |
| 31 | + else: |
| 32 | + raise NotImplementedError("DownloadBasicTest requires Entra ID authentication.") |
| 33 | + |
| 34 | + def run_sync(self): |
| 35 | + chunk_ranges = self._get_chunk_ranges() |
| 36 | + with ThreadPoolExecutor(self.args.max_concurrency) as executor: |
| 37 | + with requests.sessions.Session() as session: |
| 38 | + executor.map(lambda r: self.download_chunk_requests( |
| 39 | + session, r[0], r[1]), chunk_ranges) |
| 40 | + |
| 41 | + async def run_async(self): |
| 42 | + chunk_ranges = self._get_chunk_ranges() |
| 43 | + semaphore = asyncio.Semaphore(self.args.max_concurrency) |
| 44 | + |
| 45 | + async with aiohttp.ClientSession() as session: |
| 46 | + tasks = [self.download_chunk_aiohttp(session, offset, end, semaphore) for offset, end in chunk_ranges] |
| 47 | + await asyncio.gather(*tasks) |
| 48 | + |
| 49 | + def _get_chunk_ranges(self): |
| 50 | + chunk_ranges = [] |
| 51 | + offset = 0 |
| 52 | + while offset < self.args.size: |
| 53 | + end = min(offset + self.chunk_size - 1, self.args.size - 1) |
| 54 | + chunk_ranges.append((offset, end)) |
| 55 | + offset = end + 1 |
| 56 | + return chunk_ranges |
| 57 | + |
| 58 | + def download_chunk_requests(self, session: requests.sessions.Session, offset: int, end: int): |
| 59 | + headers = {'x-ms-version': self.blob_client.api_version, 'Range': f'bytes={offset}-{end}', 'Authorization': self.auth_header} |
| 60 | + response = session.get(self.blob_client.url, headers=headers) |
| 61 | + |
| 62 | + if response.status_code in (200, 206): |
| 63 | + pass |
| 64 | + else: |
| 65 | + raise Exception(f"Download failed with status code {response.status_code}") |
| 66 | + |
| 67 | + async def download_chunk_aiohttp(self, session: aiohttp.ClientSession, offset: int, end: int, semaphore: asyncio.Semaphore): |
| 68 | + async with semaphore: |
| 69 | + headers = {'x-ms-version': self.blob_client.api_version, 'Range': f'bytes={offset}-{end}', 'Authorization': self.auth_header} |
| 70 | + async with session.get(self.blob_client.url, headers=headers) as response: |
| 71 | + if response.status in (200, 206): |
| 72 | + await response.read() |
| 73 | + else: |
| 74 | + raise Exception(f"Download failed with status code {response.status}") |
0 commit comments