|
| 1 | +#------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +#-------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import os |
| 8 | +import asyncio |
| 9 | +from argparse import ArgumentParser |
| 10 | +from datetime import timedelta |
| 11 | + |
| 12 | +from azure.servicebus import ServiceBusClient |
| 13 | +from azure.servicebus.aio import ServiceBusClient as AsyncServiceBusClient |
| 14 | + |
| 15 | +from stress_test_base import StressTestRunner, StressTestRunnerAsync |
| 16 | +from app_insights_metric import AzureMonitorMetric |
| 17 | +from process_monitor import ProcessMonitor |
| 18 | + |
| 19 | +CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR'] |
| 20 | +QUEUE_NAME = os.environ["SERVICE_BUS_QUEUE_NAME"] |
| 21 | + |
| 22 | + |
| 23 | +def sync_send(client, args): |
| 24 | + azure_monitor_metric = AzureMonitorMetric("Sync ServiceBus Sender") |
| 25 | + process_monitor = ProcessMonitor("monitor_sender_stress_sync.log", "sender_stress_sync") |
| 26 | + stress_test = StressTestRunner( |
| 27 | + senders=[client.get_queue_sender(QUEUE_NAME)], |
| 28 | + receivers=[], |
| 29 | + message_size=args.message_size, |
| 30 | + send_batch_size=args.send_batch_size, |
| 31 | + duration=timedelta(seconds=args.duration), |
| 32 | + azure_monitor_metric=azure_monitor_metric, |
| 33 | + process_monitor=process_monitor, |
| 34 | + fail_on_exception=False |
| 35 | + ) |
| 36 | + stress_test.run() |
| 37 | + |
| 38 | + |
| 39 | +async def async_send(client, args): |
| 40 | + azure_monitor_metric = AzureMonitorMetric("Async ServiceBus Sender") |
| 41 | + process_monitor = ProcessMonitor("monitor_sender_stress_async.log", "sender_stress_async") |
| 42 | + stress_test = StressTestRunnerAsync( |
| 43 | + senders=[client.get_queue_sender(QUEUE_NAME)], |
| 44 | + receivers=[], |
| 45 | + message_size=args.message_size, |
| 46 | + send_batch_size=args.send_batch_size, |
| 47 | + duration=timedelta(seconds=args.duration), |
| 48 | + azure_monitor_metric=azure_monitor_metric, |
| 49 | + process_monitor=process_monitor, |
| 50 | + fail_on_exception=False |
| 51 | + ) |
| 52 | + await stress_test.run_async() |
| 53 | + |
| 54 | + |
| 55 | +def sync_receive(client, args): |
| 56 | + azure_monitor_metric = AzureMonitorMetric("Sync ServiceBus Receiver") |
| 57 | + process_monitor = ProcessMonitor("monitor_receiver_stress_sync.log", "receiver_stress_sync") |
| 58 | + stress_test = StressTestRunner( |
| 59 | + senders=[], |
| 60 | + receivers=[client.get_queue_receiver(QUEUE_NAME)], |
| 61 | + max_message_count=args.max_message_count, |
| 62 | + receive_type=args.receive_type, |
| 63 | + max_wait_time=args.max_wait_time, |
| 64 | + duration=timedelta(seconds=args.duration), |
| 65 | + azure_monitor_metric=azure_monitor_metric, |
| 66 | + process_monitor=process_monitor, |
| 67 | + fail_on_exception=False |
| 68 | + ) |
| 69 | + stress_test.run() |
| 70 | + |
| 71 | + |
| 72 | +async def async_receive(client, args): |
| 73 | + azure_monitor_metric = AzureMonitorMetric("Async ServiceBus Receiver") |
| 74 | + process_monitor = ProcessMonitor("monitor_receiver_stress_async.log", "receiver_stress_async") |
| 75 | + stress_test = StressTestRunnerAsync( |
| 76 | + senders=[], |
| 77 | + receivers=[client.get_queue_receiver(QUEUE_NAME)], |
| 78 | + max_message_count=args.max_message_count, |
| 79 | + receive_type=args.receive_type, |
| 80 | + max_wait_time=args.max_wait_time, |
| 81 | + duration=timedelta(seconds=args.duration), |
| 82 | + azure_monitor_metric=azure_monitor_metric, |
| 83 | + process_monitor=process_monitor, |
| 84 | + fail_on_exception=False |
| 85 | + ) |
| 86 | + await stress_test.run_async() |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + parser = ArgumentParser() |
| 91 | + parser.add_argument("--method", type=str) |
| 92 | + parser.add_argument("--duration", type=int, default=259200) |
| 93 | + parser.add_argument("--logging-enable", action="store_true") |
| 94 | + |
| 95 | + parser.add_argument("--send-batch-size", type=int, default=100) |
| 96 | + parser.add_argument("--message-size", type=int, default=100) |
| 97 | + |
| 98 | + parser.add_argument("--receive-type", type=str, default="pull") |
| 99 | + parser.add_argument("--max_wait_time", type=int, default=10) |
| 100 | + parser.add_argument("--max_message_count", type=int, default=1) |
| 101 | + |
| 102 | + args, _ = parser.parse_known_args() |
| 103 | + loop = asyncio.get_event_loop() |
| 104 | + |
| 105 | + if args.method.startswith("sync"): |
| 106 | + sb_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR) |
| 107 | + else: |
| 108 | + sb_client = AsyncServiceBusClient.from_connection_string(conn_str=CONNECTION_STR) |
| 109 | + |
| 110 | + if args.method == 'sync_send': |
| 111 | + sync_send(sb_client, args) |
| 112 | + elif args.method == 'async_send': |
| 113 | + loop.run_until_complete(async_send(sb_client, args)) |
| 114 | + elif args.method == 'sync_receive': |
| 115 | + sync_receive(sb_client, args) |
| 116 | + elif args.method == 'async_receive': |
| 117 | + loop.run_until_complete(async_receive(sb_client, args)) |
| 118 | + else: |
| 119 | + raise RuntimeError("Must set a method to run stress test.") |
0 commit comments