Skip to content

Commit

Permalink
[Perf] Call Run() once before starting recording (#21162)
Browse files Browse the repository at this point in the history
- Avoids capturing one-time setup like authorization requests
- Ported from Azure/azure-sdk-for-net#24263
  • Loading branch information
mikeharder authored Oct 8, 2021
1 parent fbdf6f4 commit af4869b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ async def global_cleanup(self):
return

async def record_and_start_playback(self):
# Make one call to Run() before starting recording, to avoid capturing one-time setup like authorization requests
if self.args.sync:
self.run_sync()
else:
await self.run_async()

await self._start_recording()
self._test_proxy_policy.recording_id = self._recording_id
self._test_proxy_policy.mode = "record"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.core import PipelineClient
from azure.core import AsyncPipelineClient
from azure.core.pipeline import PipelineResponse
from azure.core.pipeline.transport import HttpResponse

from azure_devtools.perfstress_tests import PerfStressTest


class PipelineClientGetTest(PerfStressTest):
pipeline_client: PipelineClient = None
async_pipeline_client: AsyncPipelineClient = None
_first_run = True

def __init__(self, arguments):
super().__init__(arguments)
self.pipeline_client = PipelineClient(base_url=self.args.url, **self._client_kwargs)
self.async_pipeline_client = AsyncPipelineClient(base_url=self.args.url, **self._client_kwargs)

def run_sync(self):
if self._first_run:
for _ in range(self.args.first_run_extra_requests):
self._send_request_sync()
self._first_run = False
self._send_request_sync()

async def run_async(self):
if self._first_run:
for _ in range(self.args.first_run_extra_requests):
await self._send_request_async()
self._first_run = False
await self._send_request_async()

def _send_request_sync(self):
request = self.pipeline_client.get(self.args.url)
response: PipelineResponse = self.pipeline_client._pipeline.run(request)
# Consume response body
http_response: HttpResponse = response.http_response
data = http_response.body()

async def _send_request_async(self):
request = self.async_pipeline_client.get(self.args.url)
response: PipelineResponse = await self.async_pipeline_client._pipeline.run(request)
# Consume response body
http_response: HttpResponse = response.http_response
data = http_response.body()

async def close(self):
await self.async_pipeline_client.close()
await super().close()

@staticmethod
def add_arguments(parser):
parser.add_argument("--first-run-extra-requests", type=int, default=0, help='Extra requests to send on first run. ' +
'Simulates SDKs which require extra requests (like authentication) on first API call.')
parser.add_argument("-u", "--url", required=True)

0 comments on commit af4869b

Please sign in to comment.