1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
# Azure Load Testing client library for Python
Azure Load Testing provides client library in python to the user by which they can interact natively with Azure Load Testing service. Azure Load Testing is a fully managed load-testing service that enables you to generate high-scale load. The service simulates traffic for your applications, regardless of where they're hosted. Developers, testers, and quality assurance (QA) engineers can use it to optimize application performance, scalability, or capacity.
## Documentation
Various documentation is available to help you get started
<!-- - [Source code][source_code] -->
- [API reference documentation][api_reference_doc]
- [Product Documentation][product_documentation]
## Getting started
### Installing the package
```bash
python -m pip install azure-developer-loadtesting
```
#### Prequisites
- Python 3.7 or later is required to use this package.
- You need an [Azure subscription][azure_sub] to use this package.
- An existing Azure Developer LoadTesting instance.
#### Create with an Azure Active Directory Credential
To use an [Azure Active Directory (AAD) token credential][authenticate_with_token],
provide an instance of the desired credential type obtained from the
[azure-identity][azure_identity_credentials] library.
To authenticate with AAD, you must first [pip][pip] install [`azure-identity`][azure_identity_pip]
After setup, you can choose which type of [credential][azure_identity_credentials] from azure.identity to use.
As an example, sign in via the Azure CLI `az login` command and [DefaultAzureCredential](https://learn.microsoft.com/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python) will authenticate as that user.
Use the returned token credential to authenticate the client.
#### Create the client
The data plane URI should be provided as the endpoint to create the client.
```python
from azure.developer.loadtesting import LoadTestingClient
# for managing authentication and authorization
# can be installed from pypi, follow: https://pypi.org/project/azure-identity/
# using DefaultAzureCredentials, read more at: https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
from azure.identity import DefaultAzureCredential
client = LoadTestingClient(endpoint='<endpoint>', credential=DefaultAzureCredential())
```
## Key concepts
The following components make up te Azure Load Testing service. The Azure Load Test client library for python allows you to interact with each of these components through the use of clients. There are two top-level clients which are the main entry points for the library
- `LoadTestingClient` (`azure.developer.loadtesting.LoadTestingClient`)
- Async `LoadTestingClient` (`azure.developer.loadtesting.aio.LoadTestingClient`)
The two clients have similar methods in them except the methods in the async client are async as well.
The top-level clients have two sub-clients
- `load_test_runs` (`azure.developer.loadtesting.LoadTestingClient.load_test_runs`)
- `load_test_administration` (`azure.developer.loadtesting.LoadTestingClient.load_test_administration`)
These sub-clients are used for managing and using different components of the service.
### Load Test Administration Client
The `load_test_administration` sub-clients is used to administer and configure the load tests, app components and metrics.
#### Test
A test specifies the test script, and configuration settings for running a load test. You can create one or more tests in an Azure Load Testing resource.
#### App Component
When you run a load test for an Azure-hosted application, you can monitor resource metrics for the different Azure application components (server-side metrics). While the load test runs, and after completion of the test, you can monitor and analyze the resource metrics in the Azure Load Testing dashboard.
#### Metrics
During a load test, Azure Load Testing collects metrics about the test execution. There are two types of metrics:
1. Client-side metrics give you details reported by the test engine. These metrics include the number of virtual users, the request response time, the number of failed requests, or the number of requests per second.
2. Server-side metrics are available for Azure-hosted applications and provide information about your Azure application components. Metrics can be for the number of database reads, the type of HTTP responses, or container resource consumption.
### Test Run Client
The `load_test_runs` sub-clients is used to start and stop test runs corresponding to a load test. A test run represents one execution of a load test. It collects the logs associated with running the Apache JMeter script, the load test YAML configuration, the list of app components to monitor, and the results of the test.
### Data-Plane Endpoint
Data-plane of Azure Load Testing resources is addressable using the following URL format:
`00000000-0000-0000-0000-000000000000.aaa.cnt-prod.loadtesting.azure.com`
The first GUID `00000000-0000-0000-0000-000000000000` is the unique identifier used for accessing the Azure Load Testing resource. This is followed by `aaa` which is the Azure region of the resource.
The data-plane endpoint is obtained from Control Plane APIs.
**Example:** `1234abcd-12ab-12ab-12ab-123456abcdef.eus.cnt-prod.loadtesting.azure.com`
In the above example, `eus` represents the Azure region `East US`.
## Examples
### Creating a load test
```python
from azure.developer.loadtesting import LoadTestingClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
TEST_ID = "some-test-id"
DISPLAY_NAME = "my-load-test"
SUBSCRIPTION_ID = os.environ["SUBSCRIPTION_ID"]
client = LoadTestingClient(endpoint='<endpoint>', credential=DefaultAzureCredential())
try:
result = client.load_test_administration.create_or_update_test(
TEST_ID,
{
"description": "",
"displayName": DISPLAY_NAME,
"loadTestConfig": {
"engineInstances": 1,
"splitAllCSVs": False,
},
"secrets": {},
"environmentVariables": {},
"passFailCriteria": {"passFailMetrics": {}}
},
)
print(result)
except HttpResponseError as e:
print('Service responded with error: {}'.format(e.response.json()))
```
### Uploading .jmx file to a Test
```python
from azure.developer.loadtesting import LoadTestingClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
TEST_ID = "some-test-id"
FILE_ID = "some-file-id"
client = LoadTestingClient(endpoint='<endpoint>', credential=DefaultAzureCredential())
try:
result = client.load_test_administration.upload_test_file(TEST_ID, FILE_ID, open("sample.jmx", "rb"))
print(result)
except HttpResponseError as e:
print("Failed with error: {}".format(e.response.json()))
```
### Running a Test
```python
from azure.developer.loadtesting import LoadTestingClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
TEST_ID = "some-test-id"
TEST_RUN_ID = "some-testrun-id"
DISPLAY_NAME = "my-load-test-run"
client = LoadTestingClient(endpoint='<endpoint>', credential=DefaultAzureCredential())
try:
result = client.load_test_runs.create_or_update_test(
TEST_RUN_ID,
{
"testId": TEST_ID,
"displayName": DISPLAY_NAME,
},
)
print(result)
except HttpResponseError as e:
print("Failed with error: {}".format(e.response.json()))
```
## Next steps
More samples can be found [here](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/loadtestservice/azure-developer-loadtesting/samples).
## Contributing
This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.
This project has adopted the
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
additional questions or comments.
## Troubleshooting
More about it is coming soon...
<!-- LINKS -->
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
[authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token
[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials
[azure_identity_pip]: https://pypi.org/project/azure-identity/
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
[pip]: https://pypi.org/project/pip/
[azure_sub]: https://azure.microsoft.com/free/
[api_reference_doc]: https://docs.microsoft.com/rest/api/loadtesting/
[product_documentation]: https://azure.microsoft.com/services/load-testing/
|