Skip to content

Commit

Permalink
[formrecognizer] Adding custom forms perf test (#16969)
Browse files Browse the repository at this point in the history
* Adding custom forms perf test

* improve test to use labeled model

* add README

* update test name

* use async training client

* rename file

* fix test check

* update readme cmd instructions

* update readme
  • Loading branch information
catalinaperalta authored Mar 3, 2021
1 parent 259934c commit 4f1c4a7
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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=<form recognizer service endpoint>
FORMRECOGNIZER_TEST_API_KEY=<form recognizer API Key>
FORMRECOGNIZER_TRAINING_DATA_CONTAINER_SAS_URL=<SAS url for container with training data>
```

### 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
```
Empty file.
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f1c4a7

Please sign in to comment.