diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/README.md new file mode 100644 index 000000000000..038241597ae4 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/README.md @@ -0,0 +1,45 @@ +# Form Recognizer 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: + +``` +FORMRECOGNIZER_TEST_ENDPOINT=
+FORMRECOGNIZER_TEST_API_KEY= +FORMRECOGNIZER_TRAINING_DATA_CONTAINER_SAS_URL= +``` + +### Setup for perf test runs + +```cmd +(env) ~/azure-ai-formrecognizer> pip install -r dev_requirements.txt +(env) ~/azure-ai-formrecognizer> 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 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-formrecognizer> cd tests/perfstress_tests/ +(env) ~/azure-ai-formrecognizer/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-formrecognizer/tests/perfstress_tests> perfstress RecognizeCustomForms +``` \ No newline at end of file diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/__init__.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/perf_custom_forms.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/perf_custom_forms.py new file mode 100644 index 000000000000..7c24c86411b3 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/perfstress_tests/perf_custom_forms.py @@ -0,0 +1,73 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +import os +import pytest +import functools +from io import BytesIO +from datetime import date, time +from azure_devtools.perfstress_tests import PerfStressTest +from azure.core.credentials import AzureKeyCredential +from azure.ai.formrecognizer import FormRecognizerClient, FormContentType +from azure.ai.formrecognizer.aio import FormRecognizerClient as AsyncFormRecognizerClient, FormTrainingClient as AsyncFormTrainingClient + +class RecognizeCustomForms(PerfStressTest): + + def __init__(self, arguments): + super().__init__(arguments) + + with open(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./../sample_forms/forms/Form_1.jpg")), "rb") as fd: + self.custom_form_jpg = fd.read() + + # read test related env vars + self.formrecognizer_storage_container_sas_url = os.environ["FORMRECOGNIZER_TRAINING_DATA_CONTAINER_SAS_URL"] + formrecognizer_test_endpoint = os.environ["FORMRECOGNIZER_TEST_ENDPOINT"] + form_recognizer_account_key = os.environ["FORMRECOGNIZER_TEST_API_KEY"] + + # assign the clients that will be used in the perf tests + self.service_client = FormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key)) + self.async_service_client = AsyncFormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key)) + + # training client will be used for model training in set up + self.async_training_client = AsyncFormTrainingClient(formrecognizer_test_endpoint, AzureKeyCredential(form_recognizer_account_key)) + + async def global_setup(self): + """The global setup is run only once.""" + poller = await self.async_training_client.begin_training( + self.formrecognizer_storage_container_sas_url, + use_training_labels=True, + model_name="labeled") + model = await poller.result() + self.model_id = model.model_id + + async def global_cleanup(self): + """The global cleanup is run only once.""" + await self.async_training_client.delete_model(self.model_id) + + async def close(self): + """This is run after cleanup.""" + await self.async_service_client.close() + self.service_client.close() + await self.async_training_client.close() + await super().close() + + def run_sync(self): + """The synchronous perf test.""" + poller = self.service_client.begin_recognize_custom_forms( + self.model_id, + self.custom_form_jpg, + content_type=FormContentType.IMAGE_JPEG) + result = poller.result() + assert result + + async def run_async(self): + """The asynchronous perf test.""" + poller = await self.async_service_client.begin_recognize_custom_forms( + self.model_id, + self.custom_form_jpg, + content_type=FormContentType.IMAGE_JPEG) + result = await poller.result() + assert result