Skip to content
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

[translation] perf test #19317

Merged
merged 4 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Document Translation 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 variable will need to be set for the tests to access the live resources:

```
TRANSLATION_DOCUMENT_TEST_ENDPOINT=<translation-endpoint>
TRANSLATION_DOCUMENT_TEST_API_KEY=<translation-key>
TRANSLATION_DOCUMENT_STORAGE_NAME=<storage-blob-account-name>
TRANSLATION_DOCUMENT_STORAGE_KEY=<storage-shared-key>
```

### Setup for perf test runs

```cmd
(env) ~/azure-ai-translation-document> pip install -r dev_requirements.txt
(env) ~/azure-ai-translation-document> pip install -e .
```

## Test commands

When `azure-devtools` is installed, you will have access to the `perfstress` command line tool, which will scan the current module for runable perf tests. Only a specific test can be run at a time (i.e. there is no "run all" feature).

```cmd
(env) ~/azure-ai-translation-document> cd tests/perfstress_tests/
(env) ~/azure-ai-translation-document/tests/perfstress_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). This flag must be used for Storage legacy tests, which do not support async.
- `--no-cleanup` Whether to keep newly created resources after test run. Default is False (resources will be deleted).

## Example command
```cmd
(env) ~/azure-ai-translation-document/tests/perfstress_tests> perfstress TranslationPerfStressTest
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
import uuid
import datetime
from azure_devtools.perfstress_tests import PerfStressTest
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient
from azure.ai.translation.document.aio import DocumentTranslationClient as AsyncDocumentTranslationClient
from azure.storage.blob.aio import ContainerClient, BlobServiceClient
from azure.storage.blob import generate_container_sas


class Document(object):
"""Represents a document to be uploaded to source/target container"""
def __init__(self, **kwargs):
self.name = kwargs.get("name", str(uuid.uuid4()))
self.suffix = kwargs.get("suffix", ".txt")
self.prefix = kwargs.get("prefix", "")
self.data = kwargs.get("data", b'This is written in english.')

@classmethod
def create_docs(cls, docs_count):
result = []
for i in range(docs_count):
result.append(cls())
return result


class TranslationPerfStressTest(PerfStressTest):
def __init__(self, arguments):
super().__init__(arguments)

# test related env vars
endpoint = os.environ["TRANSLATION_DOCUMENT_TEST_ENDPOINT"]
key = os.environ["TRANSLATION_DOCUMENT_TEST_API_KEY"]
self.storage_name = os.environ["TRANSLATION_DOCUMENT_STORAGE_NAME"]
self.storage_key = os.environ["TRANSLATION_DOCUMENT_STORAGE_KEY"]
self.storage_endpoint = "https://" + self.storage_name + ".blob.core.windows.net/"
self.source_container_name = "source-perf-" + str(uuid.uuid4())
self.target_container_name = "target-perf-" + str(uuid.uuid4())

self.service_client = DocumentTranslationClient(
endpoint,
AzureKeyCredential(key),
**self._client_kwargs
)
self.async_service_client = AsyncDocumentTranslationClient(
endpoint,
AzureKeyCredential(key),
**self._client_kwargs
)

async def create_source_container(self):
container_client = ContainerClient(self.storage_endpoint, self.source_container_name,
self.storage_key)
async with container_client:
await container_client.create_container()
docs = Document.create_docs(10)
for blob in docs:
await container_client.upload_blob(name=blob.prefix + blob.name + blob.suffix, data=blob.data)
return self.generate_sas_url(self.source_container_name, "rl")

async def create_target_container(self):
container_client = ContainerClient(self.storage_endpoint, self.target_container_name,
self.storage_key)
async with container_client:
await container_client.create_container()

return self.generate_sas_url(self.target_container_name, "wl")

def generate_sas_url(self, container_name, permission):
sas_token = generate_container_sas(
account_name=self.storage_name,
container_name=container_name,
account_key=self.storage_key,
permission=permission,
expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=2)
)

container_sas_url = self.storage_endpoint + container_name + "?" + sas_token
return container_sas_url

async def global_setup(self):
"""The global setup is run only once."""
self.source_container_sas_url = await self.create_source_container()
self.target_container_sas_url = await self.create_target_container()
poller = await self.async_service_client.begin_translation(
self.source_container_sas_url, self.target_container_sas_url, "fr"
)
self.translation_id = poller.id

async def global_cleanup(self):
"""The global cleanup is run only once."""
blob_service_client = BlobServiceClient(self.storage_endpoint, self.storage_key)
async with blob_service_client:
await blob_service_client.delete_container(self.source_container_name)
await blob_service_client.delete_container(self.target_container_name)

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

def run_sync(self):
"""The synchronous perf test."""
statuses = self.service_client.list_all_document_statuses(
self.translation_id
)
for doc in statuses:
pass

async def run_async(self):
"""The asynchronous perf test."""
statuses = self.async_service_client.list_all_document_statuses(
self.translation_id
)
async for doc in statuses:
pass