Skip to content

Commit af4869b

Browse files
authored
[Perf] Call Run() once before starting recording (Azure#21162)
- Avoids capturing one-time setup like authorization requests - Ported from Azure/azure-sdk-for-net#24263
1 parent fbdf6f4 commit af4869b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tools/azure-devtools/src/azure_devtools/perfstress_tests/perf_stress_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ async def global_cleanup(self):
4848
return
4949

5050
async def record_and_start_playback(self):
51+
# Make one call to Run() before starting recording, to avoid capturing one-time setup like authorization requests
52+
if self.args.sync:
53+
self.run_sync()
54+
else:
55+
await self.run_async()
56+
5157
await self._start_recording()
5258
self._test_proxy_policy.recording_id = self._recording_id
5359
self._test_proxy_policy.mode = "record"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
from azure.core import PipelineClient
7+
from azure.core import AsyncPipelineClient
8+
from azure.core.pipeline import PipelineResponse
9+
from azure.core.pipeline.transport import HttpResponse
10+
11+
from azure_devtools.perfstress_tests import PerfStressTest
12+
13+
14+
class PipelineClientGetTest(PerfStressTest):
15+
pipeline_client: PipelineClient = None
16+
async_pipeline_client: AsyncPipelineClient = None
17+
_first_run = True
18+
19+
def __init__(self, arguments):
20+
super().__init__(arguments)
21+
self.pipeline_client = PipelineClient(base_url=self.args.url, **self._client_kwargs)
22+
self.async_pipeline_client = AsyncPipelineClient(base_url=self.args.url, **self._client_kwargs)
23+
24+
def run_sync(self):
25+
if self._first_run:
26+
for _ in range(self.args.first_run_extra_requests):
27+
self._send_request_sync()
28+
self._first_run = False
29+
self._send_request_sync()
30+
31+
async def run_async(self):
32+
if self._first_run:
33+
for _ in range(self.args.first_run_extra_requests):
34+
await self._send_request_async()
35+
self._first_run = False
36+
await self._send_request_async()
37+
38+
def _send_request_sync(self):
39+
request = self.pipeline_client.get(self.args.url)
40+
response: PipelineResponse = self.pipeline_client._pipeline.run(request)
41+
# Consume response body
42+
http_response: HttpResponse = response.http_response
43+
data = http_response.body()
44+
45+
async def _send_request_async(self):
46+
request = self.async_pipeline_client.get(self.args.url)
47+
response: PipelineResponse = await self.async_pipeline_client._pipeline.run(request)
48+
# Consume response body
49+
http_response: HttpResponse = response.http_response
50+
data = http_response.body()
51+
52+
async def close(self):
53+
await self.async_pipeline_client.close()
54+
await super().close()
55+
56+
@staticmethod
57+
def add_arguments(parser):
58+
parser.add_argument("--first-run-extra-requests", type=int, default=0, help='Extra requests to send on first run. ' +
59+
'Simulates SDKs which require extra requests (like authentication) on first API call.')
60+
parser.add_argument("-u", "--url", required=True)

0 commit comments

Comments
 (0)