|
| 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 os |
| 7 | +import uuid |
| 8 | +import string |
| 9 | +import random |
| 10 | + |
| 11 | +from devtools_testutils.perfstress_tests import PerfStressTest |
| 12 | + |
| 13 | +from azure.core import PipelineClient, AsyncPipelineClient |
| 14 | +from azure.core.pipeline import Pipeline, AsyncPipeline |
| 15 | +from azure.core.pipeline.transport import ( |
| 16 | + RequestsTransport, |
| 17 | + AioHttpTransport, |
| 18 | + AsyncioRequestsTransport, |
| 19 | +) |
| 20 | +from azure.core.pipeline.policies import ( |
| 21 | + UserAgentPolicy, |
| 22 | + HeadersPolicy, |
| 23 | + ProxyPolicy, |
| 24 | + NetworkTraceLoggingPolicy, |
| 25 | + HttpLoggingPolicy, |
| 26 | + RetryPolicy, |
| 27 | + CustomHookPolicy, |
| 28 | + RedirectPolicy, |
| 29 | + AsyncRetryPolicy, |
| 30 | + AsyncRedirectPolicy, |
| 31 | + BearerTokenCredentialPolicy, |
| 32 | + AsyncBearerTokenCredentialPolicy, |
| 33 | +) |
| 34 | +import azure.core.pipeline.policies as policies |
| 35 | +from azure.core.credentials import AzureNamedKeyCredential |
| 36 | +from azure.core.exceptions import ( |
| 37 | + ClientAuthenticationError, |
| 38 | + ResourceExistsError, |
| 39 | + ResourceNotFoundError, |
| 40 | + ResourceNotModifiedError, |
| 41 | +) |
| 42 | +from azure.identity import ClientSecretCredential |
| 43 | +from azure.identity.aio import ClientSecretCredential as AsyncClientSecretCredential |
| 44 | +from azure.data.tables.aio import TableClient |
| 45 | + |
| 46 | +from azure.storage.blob._shared.authentication import SharedKeyCredentialPolicy as BlobSharedKeyCredentialPolicy |
| 47 | +from azure.data.tables._authentication import SharedKeyCredentialPolicy as TableSharedKeyCredentialPolicy |
| 48 | + |
| 49 | +_LETTERS = string.ascii_letters |
| 50 | + |
| 51 | + |
| 52 | +class _ServiceTest(PerfStressTest): |
| 53 | + transport = None |
| 54 | + async_transport = None |
| 55 | + |
| 56 | + def __init__(self, arguments): |
| 57 | + super().__init__(arguments) |
| 58 | + self.account_name = self.get_from_env("AZURE_STORAGE_ACCOUNT_NAME") |
| 59 | + self.account_key = self.get_from_env("AZURE_STORAGE_ACCOUNT_KEY") |
| 60 | + async_transport_types = {"aiohttp": AioHttpTransport, "requests": AsyncioRequestsTransport} |
| 61 | + sync_transport_types = {"requests": RequestsTransport} |
| 62 | + self.tenant_id = os.environ["CORE_TENANT_ID"] |
| 63 | + self.client_id = os.environ["CORE_CLIENT_ID"] |
| 64 | + self.client_secret = os.environ["CORE_CLIENT_SECRET"] |
| 65 | + self.storage_scope = "https://storage.azure.com/.default" |
| 66 | + |
| 67 | + # defaults transports |
| 68 | + self.sync_transport = RequestsTransport |
| 69 | + self.async_transport = AioHttpTransport |
| 70 | + |
| 71 | + # if transport is specified, use that |
| 72 | + if self.args.transport: |
| 73 | + # if sync, override sync default |
| 74 | + if self.args.sync: |
| 75 | + try: |
| 76 | + self.sync_transport = sync_transport_types[self.args.transport] |
| 77 | + except KeyError: |
| 78 | + raise ValueError(f"Invalid sync transport:{self.args.transport}\n Valid options are:\n- requests\n") |
| 79 | + # if async, override async default |
| 80 | + else: |
| 81 | + try: |
| 82 | + self.async_transport = async_transport_types[self.args.transport] |
| 83 | + except KeyError: |
| 84 | + raise ValueError( |
| 85 | + f"Invalid async transport:{self.args.transport}\n Valid options are:\n- aiohttp\n- requests\n" |
| 86 | + ) |
| 87 | + |
| 88 | + self.error_map = { |
| 89 | + 401: ClientAuthenticationError, |
| 90 | + 404: ResourceNotFoundError, |
| 91 | + 409: ResourceExistsError, |
| 92 | + 304: ResourceNotModifiedError, |
| 93 | + } |
| 94 | + |
| 95 | + def _build_sync_pipeline_client(self, auth_policy): |
| 96 | + default_policies = [ |
| 97 | + UserAgentPolicy, |
| 98 | + HeadersPolicy, |
| 99 | + ProxyPolicy, |
| 100 | + NetworkTraceLoggingPolicy, |
| 101 | + HttpLoggingPolicy, |
| 102 | + RetryPolicy, |
| 103 | + CustomHookPolicy, |
| 104 | + RedirectPolicy, |
| 105 | + ] |
| 106 | + |
| 107 | + if self.args.policies is None: |
| 108 | + # if None, only auth policy is passed in |
| 109 | + sync_pipeline = Pipeline(transport=self.sync_transport(), policies=[auth_policy]) |
| 110 | + elif self.args.policies == "all": |
| 111 | + # if all, autorest default policies + auth policy |
| 112 | + sync_policies = [auth_policy] |
| 113 | + sync_policies.extend([policy(sdk_moniker=self.sdk_moniker) for policy in default_policies]) |
| 114 | + sync_pipeline = Pipeline(transport=self.sync_transport(), policies=sync_policies) |
| 115 | + else: |
| 116 | + sync_policies = [auth_policy] |
| 117 | + for p in self.args.policies.split(","): |
| 118 | + try: |
| 119 | + policy = getattr(policies, p) |
| 120 | + except AttributeError as exc: |
| 121 | + raise ValueError( |
| 122 | + f"Azure Core has no policy named {exc.name}. Please use policies from the following list: {policies.__all__}" |
| 123 | + ) from exc |
| 124 | + sync_policies.append(policy(sdk_moniker=self.sdk_moniker)) |
| 125 | + sync_pipeline = Pipeline(transport=self.sync_transport(), policies=sync_policies) |
| 126 | + return PipelineClient(self.account_endpoint, pipeline=sync_pipeline) |
| 127 | + |
| 128 | + def _build_async_pipeline_client(self, auth_policy): |
| 129 | + default_policies = [ |
| 130 | + UserAgentPolicy, |
| 131 | + HeadersPolicy, |
| 132 | + ProxyPolicy, |
| 133 | + NetworkTraceLoggingPolicy, |
| 134 | + HttpLoggingPolicy, |
| 135 | + AsyncRetryPolicy, |
| 136 | + CustomHookPolicy, |
| 137 | + AsyncRedirectPolicy, |
| 138 | + ] |
| 139 | + if self.args.policies is None: |
| 140 | + # if None, only auth policy is passed in |
| 141 | + async_pipeline = AsyncPipeline(transport=self.async_transport(), policies=[auth_policy]) |
| 142 | + elif self.args.policies == "all": |
| 143 | + # if all, autorest default policies + auth policy |
| 144 | + async_policies = [auth_policy] |
| 145 | + async_policies.extend([policy(sdk_moniker=self.sdk_moniker) for policy in default_policies]) |
| 146 | + async_pipeline = AsyncPipeline(transport=self.async_transport(), policies=async_policies) |
| 147 | + else: |
| 148 | + async_policies = [auth_policy] |
| 149 | + # if custom list of policies, pass in custom list + auth policy |
| 150 | + for p in self.args.policies.split(","): |
| 151 | + try: |
| 152 | + policy = getattr(policies, p) |
| 153 | + except AttributeError as exc: |
| 154 | + raise ValueError( |
| 155 | + f"Azure Core has no policy named {exc.name}. Please use policies from the following list: {policies.__all__}" |
| 156 | + ) from exc |
| 157 | + async_policies.append(policy(sdk_moniker=self.sdk_moniker)) |
| 158 | + async_pipeline = AsyncPipeline(transport=self.async_transport(), policies=async_policies) |
| 159 | + return AsyncPipelineClient(self.account_endpoint, pipeline=async_pipeline) |
| 160 | + |
| 161 | + def _set_auth_policies(self): |
| 162 | + if not self.args.aad: |
| 163 | + # if tables, create table credential policy, else blob policy |
| 164 | + if "tables" in self.sdk_moniker: |
| 165 | + self.sync_auth_policy = TableSharedKeyCredentialPolicy( |
| 166 | + AzureNamedKeyCredential(self.account_name, self.account_key) |
| 167 | + ) |
| 168 | + self.async_auth_policy = self.sync_auth_policy |
| 169 | + else: |
| 170 | + self.sync_auth_policy = BlobSharedKeyCredentialPolicy(self.account_name, self.account_key) |
| 171 | + self.async_auth_policy = self.sync_auth_policy |
| 172 | + else: |
| 173 | + sync_credential = ClientSecretCredential(self.tenant_id, self.client_id, self.client_secret) |
| 174 | + self.sync_auth_policy = BearerTokenCredentialPolicy(sync_credential, self.storage_scope) |
| 175 | + async_credential = AsyncClientSecretCredential(self.tenant_id, self.client_id, self.client_secret) |
| 176 | + self.async_auth_policy = AsyncBearerTokenCredentialPolicy(async_credential, self.storage_scope) |
| 177 | + |
| 178 | + @staticmethod |
| 179 | + def add_arguments(parser): |
| 180 | + super(_ServiceTest, _ServiceTest).add_arguments(parser) |
| 181 | + parser.add_argument( |
| 182 | + "--transport", |
| 183 | + nargs="?", |
| 184 | + type=str, |
| 185 | + help="""Underlying HttpTransport type. Defaults to `aiohttp` if async, `requests` if sync. Other possible values for async:\n""" |
| 186 | + """ - `requests`\n""", |
| 187 | + default=None, |
| 188 | + ) |
| 189 | + parser.add_argument( |
| 190 | + "-s", "--size", nargs="?", type=int, help="Size of data to transfer. Default is 10240.", default=10240 |
| 191 | + ) |
| 192 | + parser.add_argument( |
| 193 | + "--policies", |
| 194 | + nargs="?", |
| 195 | + type=str, |
| 196 | + help="""List of policies to pass in to the pipeline. Options:""" |
| 197 | + """\n- None: No extra policies passed in, except for authentication policy. This is the default.""" |
| 198 | + """\n- 'all': All policies added automatically by autorest.""" |
| 199 | + """\n- 'policy1,policy2': Comma-separated list of policies, such as 'RetryPolicy,HttpLoggingPolicy'""", |
| 200 | + default=None, |
| 201 | + ) |
| 202 | + parser.add_argument("--aad", action="store_true", help="Use AAD authentication instead of shared key.") |
| 203 | + |
| 204 | + |
| 205 | +class _BlobTest(_ServiceTest): |
| 206 | + container_name = "perfstress-" + str(uuid.uuid4()) |
| 207 | + |
| 208 | + def __init__(self, arguments): |
| 209 | + super().__init__(arguments) |
| 210 | + self.account_endpoint = self.get_from_env("AZURE_STORAGE_BLOBS_ENDPOINT") |
| 211 | + self.container_name = self.get_from_env("AZURE_STORAGE_CONTAINER_NAME") |
| 212 | + self.api_version = "2021-12-02" |
| 213 | + self.sdk_moniker = f"storage-blob/{self.api_version}" |
| 214 | + |
| 215 | + self._set_auth_policies() |
| 216 | + self.pipeline_client = self._build_sync_pipeline_client(self.sync_auth_policy) |
| 217 | + self.async_pipeline_client = self._build_async_pipeline_client(self.async_auth_policy) |
| 218 | + |
| 219 | + async def close(self): |
| 220 | + self.pipeline_client.close() |
| 221 | + await self.async_pipeline_client.close() |
| 222 | + await super().close() |
| 223 | + |
| 224 | + |
| 225 | +class _TableTest(_ServiceTest): |
| 226 | + table_name = "".join(random.choice(_LETTERS) for i in range(30)) |
| 227 | + |
| 228 | + def __init__(self, arguments): |
| 229 | + super().__init__(arguments) |
| 230 | + self.account_endpoint = self.get_from_env("AZURE_STORAGE_TABLES_ENDPOINT") |
| 231 | + self.api_version = "2019-02-02" |
| 232 | + self.data_service_version = "3.0" |
| 233 | + self.sdk_moniker = f"tables/{self.api_version}" |
| 234 | + self._set_auth_policies() |
| 235 | + |
| 236 | + self.pipeline_client = self._build_sync_pipeline_client(self.sync_auth_policy) |
| 237 | + self.async_pipeline_client = self._build_async_pipeline_client(self.async_auth_policy) |
| 238 | + |
| 239 | + self.connection_string = self.get_from_env("AZURE_STORAGE_CONN_STR") |
| 240 | + self.async_table_client = TableClient.from_connection_string(self.connection_string, self.table_name) |
| 241 | + |
| 242 | + async def global_setup(self): |
| 243 | + await super().global_setup() |
| 244 | + await self.async_table_client.create_table() |
| 245 | + |
| 246 | + async def global_cleanup(self): |
| 247 | + await self.async_table_client.delete_table() |
| 248 | + |
| 249 | + def get_base_entity(self, pk, rk, size): |
| 250 | + # 227 is the length of the entity with Data of length 0 |
| 251 | + base_entity_length = 227 |
| 252 | + data_length = max(size - base_entity_length, 0) |
| 253 | + # size = 227 + data_length |
| 254 | + return { |
| 255 | + "PartitionKey": pk, |
| 256 | + "RowKey": rk, |
| 257 | + "Data": "a" * data_length, |
| 258 | + } |
| 259 | + |
| 260 | + def get_entity(self, rk=0): |
| 261 | + return {"PartitionKey": "pk", "RowKey": str(rk), "Property1": f"a{rk}", "Property2": f"b{rk}"} |
| 262 | + |
| 263 | + async def close(self): |
| 264 | + self.pipeline_client.close() |
| 265 | + await self.async_pipeline_client.close() |
| 266 | + await super().close() |
0 commit comments