Skip to content

Commit

Permalink
[KeyVault Admin] Add one perfstress test (#23303)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yalin Li authored Mar 8, 2022
1 parent c44c69a commit de80523
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
5 changes: 2 additions & 3 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,12 @@
"livekvtestcertrec",
"livekvtestgetcertperfcert",
"livekvtestgetsecretperfsecret",
"livekvtestlistperfsecret",
"lmazuel",
"livekvtest",
"livekvtestlistperfsecret",
"livekvtestdecryptperfkey",
"livekvtestgetkeyperfkey",
"livekvtestsignperfkey",
"livekvtestunwrapperfkey",
"lmazuel",
"logz",
"LPCWSTR",
"Lucene",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# azure-keyvault-administration 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 virtual environment for your perf tests. This will need to be a
Python 3 environment, preferably >=3.7.

### Setup for test resources

The following environment variables will need to be set for the tests to access the live resources:

```
AZURE_TENANT_ID=<tenant ID of testing service principal>
AZURE_CLIENT_ID=<client ID of testing service principal>
AZURE_CLIENT_SECRET=<client secret of testing service principal>
AZURE_MANAGEDHSM_URL=<URL of the testing key vault>
```

### Setup for perf test runs

```cmd
(env) ~/azure-keyvault-administration> pip install -r dev_requirements.txt
(env) ~/azure-keyvault-administration> pip install -e .
```

## 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).

```cmd
(env) ~/azure-keyvault-administration> cd tests/perfstress_tests/
(env) ~/azure-keyvault-administration/tests> perfstress
```
Using the `perfstress` command alone will list the available perf tests found.

### 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
```cmd
(env) ~/azure-keyvault-administration/tests> perfstress GetRoleDefinitionTest
```
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

import uuid
from azure_devtools.perfstress_tests import PerfStressTest
from azure.identity import DefaultAzureCredential
from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential
from azure.keyvault.administration import (
KeyVaultAccessControlClient,
KeyVaultDataAction,
KeyVaultPermission,
KeyVaultRoleScope,
)
from azure.keyvault.administration.aio import KeyVaultAccessControlClient as AsyncKeyVaultAccessControlClient


class GetRoleDefinitionTest(PerfStressTest):

def __init__(self, arguments):
super().__init__(arguments)

# Auth configuration
self.credential = DefaultAzureCredential()
self.async_credential = AsyncDefaultAzureCredential()

# Create clients
vault_url = self.get_from_env("AZURE_MANAGEDHSM_URL")
self.client = KeyVaultAccessControlClient(vault_url, self.credential, **self._client_kwargs)
self.async_client = AsyncKeyVaultAccessControlClient(vault_url, self.async_credential, **self._client_kwargs)
self.role_name = uuid.uuid4()
self.scope = KeyVaultRoleScope.GLOBAL
self.permissions = [KeyVaultPermission(data_actions=[KeyVaultDataAction.CREATE_HSM_KEY])]

async def global_setup(self):
"""The global setup is run only once."""
await super().global_setup()
await self.async_client.set_role_definition(scope=self.scope, name=self.role_name, permissions=self.permissions)

async def global_cleanup(self):
"""The global cleanup is run only once."""
await self.async_client.delete_role_definition(scope=self.scope, name=self.role_name)
await super().global_cleanup()

async def close(self):
"""This is run after cleanup."""
await self.async_client.close()
await self.async_credential.close()
await super().close()

def run_sync(self):
"""The synchronous perf test."""
self.client.get_role_definition(self.scope, self.role_name)

async def run_async(self):
"""The asynchronous perf test."""
await self.async_client.get_role_definition(self.scope, self.role_name)

0 comments on commit de80523

Please sign in to comment.