From 2ce955e491474d2ca56c44d1b8cd4f9d28116600 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 17 Mar 2021 11:13:50 -0700 Subject: [PATCH 1/5] add BearerTokenPolicyTest --- .../tests/perfstress_tests/__init__.py | 5 +++ .../bearer_token_auth_policy.py | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 sdk/core/azure-core/tests/perfstress_tests/__init__.py create mode 100644 sdk/core/azure-core/tests/perfstress_tests/bearer_token_auth_policy.py diff --git a/sdk/core/azure-core/tests/perfstress_tests/__init__.py b/sdk/core/azure-core/tests/perfstress_tests/__init__.py new file mode 100644 index 000000000000..4a10bbeb47f4 --- /dev/null +++ b/sdk/core/azure-core/tests/perfstress_tests/__init__.py @@ -0,0 +1,5 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See LICENSE.txt in the project root for +# license information. +# ------------------------------------------------------------------------- diff --git a/sdk/core/azure-core/tests/perfstress_tests/bearer_token_auth_policy.py b/sdk/core/azure-core/tests/perfstress_tests/bearer_token_auth_policy.py new file mode 100644 index 000000000000..f990b002c0dd --- /dev/null +++ b/sdk/core/azure-core/tests/perfstress_tests/bearer_token_auth_policy.py @@ -0,0 +1,45 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See LICENSE.txt in the project root for +# license information. +# ------------------------------------------------------------------------- +import asyncio +import time +from unittest.mock import Mock + +from azure_devtools.perfstress_tests import PerfStressTest + +from azure.core.credentials import AccessToken +from azure.core.pipeline import AsyncPipeline, Pipeline +from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy, BearerTokenCredentialPolicy +from azure.core.pipeline.transport import HttpRequest + + +class BearerTokenPolicyTest(PerfStressTest): + def __init__(self, arguments): + super().__init__(arguments) + + token = AccessToken("**", int(time.time() + 3600)) + + self.request = HttpRequest("GET", "https://localhost") + + credential = Mock(get_token=Mock(return_value=token)) + self.pipeline = Pipeline( + transport=Mock(), policies=[BearerTokenCredentialPolicy(credential=credential)] + ) + + completed_future = asyncio.Future() + completed_future.set_result(token) + async_credential = Mock(get_token=Mock(return_value=completed_future)) + + # returning a token is okay because the policy does nothing with the transport's response + async_transport = Mock(send=Mock(return_value=completed_future)) + self.async_pipeline = AsyncPipeline( + async_transport, policies=[AsyncBearerTokenCredentialPolicy(credential=async_credential)] + ) + + def run_sync(self): + self.pipeline.run(self.request) + + async def run_async(self): + await self.async_pipeline.run(self.request) From ecd8776817863cc8702a369734b01d67f7cf97f5 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 22 Mar 2021 11:25:57 -0700 Subject: [PATCH 2/5] readme --- .../tests/perfstress_tests/README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sdk/core/azure-core/tests/perfstress_tests/README.md diff --git a/sdk/core/azure-core/tests/perfstress_tests/README.md b/sdk/core/azure-core/tests/perfstress_tests/README.md new file mode 100644 index 000000000000..2265fac60b07 --- /dev/null +++ b/sdk/core/azure-core/tests/perfstress_tests/README.md @@ -0,0 +1,32 @@ +# Core Performance Tests + +In order to run the performance tests, the `azure-devtools` package must be installed. This is done as part of the +`dev_requirements` install. Start by creating a new Python 3 virtual environment. + +## Test commands + +Once `azure-devtools` is installed, you will have access to the `perfstress` command line tool, which will scan the +current module for runnable perf tests. Only a specific test can be run at a time (i.e. there is no "run all" feature). + +`perfstress` with no options will list all available tests: +``` +(env) ~/azure-core/tests> perfstress +``` + +### Common perf command line options +These options are available for all perf tests: +- `--duration=10` Number of seconds to run as many operations (the "run" function) as possible. Default is 10. +- `--iterations=1` Number of test iterations to run. Default is 1. +- `--parallel=1` Number of tests to run in parallel. Default is 1. +- `--warm-up=5` Number of seconds to spend warming up the connection before measuring begins. Default is 5. +- `--sync` Whether to run the tests in sync or async. Default is False (async). +- `--no-cleanup` Whether to keep newly created resources after test run. Default is False (resources will be deleted). + +## Example command +``` +sdk/core/azure-core/tests> perfstress BearerTokenPolicyTest +``` + +## Tests +- `BearerTokenPolicyTest` Runs a single request through `BearerTokenCredentialPolicy`, + and a mock transport From a2db6aec60a0693ce7c0c5c8075b50cfd7b151fa Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Fri, 26 Mar 2021 16:41:16 -0700 Subject: [PATCH 3/5] move it all to sdk/identity --- .../azure-identity}/tests/perfstress_tests/README.md | 6 +++--- .../azure-identity}/tests/perfstress_tests/__init__.py | 0 .../tests/perfstress_tests/bearer_token_auth_policy.py | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename sdk/{core/azure-core => identity/azure-identity}/tests/perfstress_tests/README.md (90%) rename sdk/{core/azure-core => identity/azure-identity}/tests/perfstress_tests/__init__.py (100%) rename sdk/{core/azure-core => identity/azure-identity}/tests/perfstress_tests/bearer_token_auth_policy.py (100%) diff --git a/sdk/core/azure-core/tests/perfstress_tests/README.md b/sdk/identity/azure-identity/tests/perfstress_tests/README.md similarity index 90% rename from sdk/core/azure-core/tests/perfstress_tests/README.md rename to sdk/identity/azure-identity/tests/perfstress_tests/README.md index 2265fac60b07..f46968758bb4 100644 --- a/sdk/core/azure-core/tests/perfstress_tests/README.md +++ b/sdk/identity/azure-identity/tests/perfstress_tests/README.md @@ -1,4 +1,4 @@ -# Core Performance Tests +# azure-identity Performance Tests In order to run the performance tests, the `azure-devtools` package must be installed. This is done as part of the `dev_requirements` install. Start by creating a new Python 3 virtual environment. @@ -10,7 +10,7 @@ current module for runnable perf tests. Only a specific test can be run at a tim `perfstress` with no options will list all available tests: ``` -(env) ~/azure-core/tests> perfstress +(env) ~/azure-identity/tests> perfstress ``` ### Common perf command line options @@ -24,7 +24,7 @@ These options are available for all perf tests: ## Example command ``` -sdk/core/azure-core/tests> perfstress BearerTokenPolicyTest +(env) ~/azure-identity/tests> perfstress BearerTokenPolicyTest ``` ## Tests diff --git a/sdk/core/azure-core/tests/perfstress_tests/__init__.py b/sdk/identity/azure-identity/tests/perfstress_tests/__init__.py similarity index 100% rename from sdk/core/azure-core/tests/perfstress_tests/__init__.py rename to sdk/identity/azure-identity/tests/perfstress_tests/__init__.py diff --git a/sdk/core/azure-core/tests/perfstress_tests/bearer_token_auth_policy.py b/sdk/identity/azure-identity/tests/perfstress_tests/bearer_token_auth_policy.py similarity index 100% rename from sdk/core/azure-core/tests/perfstress_tests/bearer_token_auth_policy.py rename to sdk/identity/azure-identity/tests/perfstress_tests/bearer_token_auth_policy.py From 09da65fc236d128c2a139e733954670cdfb88f8f Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Fri, 2 Apr 2021 16:03:03 -0700 Subject: [PATCH 4/5] add cache read perf tests --- .../tests/perfstress_tests/README.md | 3 ++ .../perfstress_tests/memory_cache_read.py | 44 +++++++++++++++++ .../perfstress_tests/persistent_cache_read.py | 48 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 sdk/identity/azure-identity/tests/perfstress_tests/memory_cache_read.py create mode 100644 sdk/identity/azure-identity/tests/perfstress_tests/persistent_cache_read.py diff --git a/sdk/identity/azure-identity/tests/perfstress_tests/README.md b/sdk/identity/azure-identity/tests/perfstress_tests/README.md index f46968758bb4..46d2a604c04d 100644 --- a/sdk/identity/azure-identity/tests/perfstress_tests/README.md +++ b/sdk/identity/azure-identity/tests/perfstress_tests/README.md @@ -30,3 +30,6 @@ These options are available for all perf tests: ## Tests - `BearerTokenPolicyTest` Runs a single request through `BearerTokenCredentialPolicy`, and a mock transport +- `MemoryCacheRead` retrieves an access token from the default, in memory cache. + This is useful primarily as a baseline for `PersistentCacheRead`. +- `PersistentCacheRead` retrives an access token from the persistent cache diff --git a/sdk/identity/azure-identity/tests/perfstress_tests/memory_cache_read.py b/sdk/identity/azure-identity/tests/perfstress_tests/memory_cache_read.py new file mode 100644 index 000000000000..269191bd5bd6 --- /dev/null +++ b/sdk/identity/azure-identity/tests/perfstress_tests/memory_cache_read.py @@ -0,0 +1,44 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ +from azure_devtools.perfstress_tests import PerfStressTest + +from azure.identity import ClientSecretCredential +from azure.identity.aio import ClientSecretCredential as AsyncClientSecretCredential + +try: + from dotenv import load_dotenv + + load_dotenv() +except ImportError: + pass + + +class MemoryCacheRead(PerfStressTest): + def __init__(self, arguments): + super().__init__(arguments) + + client_id = self.get_from_env("AZURE_CLIENT_ID") + tenant_id = self.get_from_env("AZURE_TENANT_ID") + secret = self.get_from_env("AZURE_CLIENT_SECRET") + self.credential = ClientSecretCredential(tenant_id, client_id, secret) + self.async_credential = AsyncClientSecretCredential(tenant_id, client_id, secret) + self.scope = "https://vault.azure.net/.default" + + async def global_setup(self): + """Cache an access token""" + + await super().global_setup() + self.credential.get_token(self.scope) + await self.async_credential.get_token(self.scope) + + def run_sync(self): + self.credential.get_token(self.scope) + + async def run_async(self): + await self.async_credential.get_token(self.scope) + + async def close(self): + await self.async_credential.close() + await super().close() diff --git a/sdk/identity/azure-identity/tests/perfstress_tests/persistent_cache_read.py b/sdk/identity/azure-identity/tests/perfstress_tests/persistent_cache_read.py new file mode 100644 index 000000000000..64761ee76baf --- /dev/null +++ b/sdk/identity/azure-identity/tests/perfstress_tests/persistent_cache_read.py @@ -0,0 +1,48 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ +from azure_devtools.perfstress_tests import PerfStressTest + +from azure.identity import ClientSecretCredential, TokenCachePersistenceOptions +from azure.identity.aio import ClientSecretCredential as AsyncClientSecretCredential + +try: + from dotenv import load_dotenv + + load_dotenv() +except ImportError: + pass + + +class PersistentCacheRead(PerfStressTest): + def __init__(self, arguments): + super().__init__(arguments) + + client_id = self.get_from_env("AZURE_CLIENT_ID") + tenant_id = self.get_from_env("AZURE_TENANT_ID") + secret = self.get_from_env("AZURE_CLIENT_SECRET") + self.credential = ClientSecretCredential( + tenant_id, client_id, secret, cache_persistence_options=TokenCachePersistenceOptions() + ) + self.async_credential = AsyncClientSecretCredential( + tenant_id, client_id, secret, cache_persistence_options=TokenCachePersistenceOptions() + ) + self.scope = "https://vault.azure.net/.default" + + async def global_setup(self): + """Cache an access token""" + + await super().global_setup() + self.credential.get_token(self.scope) + await self.async_credential.get_token(self.scope) + + def run_sync(self): + self.credential.get_token(self.scope) + + async def run_async(self): + await self.async_credential.get_token(self.scope) + + async def close(self): + await self.async_credential.close() + await super().close() From 19e51b13346a0e0bb31ec3aed8b10a0caec23d41 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 5 Apr 2021 15:50:44 -0700 Subject: [PATCH 5/5] black --- .../tests/perfstress_tests/bearer_token_auth_policy.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk/identity/azure-identity/tests/perfstress_tests/bearer_token_auth_policy.py b/sdk/identity/azure-identity/tests/perfstress_tests/bearer_token_auth_policy.py index f990b002c0dd..1f3508295120 100644 --- a/sdk/identity/azure-identity/tests/perfstress_tests/bearer_token_auth_policy.py +++ b/sdk/identity/azure-identity/tests/perfstress_tests/bearer_token_auth_policy.py @@ -24,9 +24,7 @@ def __init__(self, arguments): self.request = HttpRequest("GET", "https://localhost") credential = Mock(get_token=Mock(return_value=token)) - self.pipeline = Pipeline( - transport=Mock(), policies=[BearerTokenCredentialPolicy(credential=credential)] - ) + self.pipeline = Pipeline(transport=Mock(), policies=[BearerTokenCredentialPolicy(credential=credential)]) completed_future = asyncio.Future() completed_future.set_result(token)