| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from concurrent.futures import Future |
| 5 |
from awscrt import mqtt5 |
| 6 |
from awscrt.io import ClientBootstrap, EventLoopGroup, DefaultHostResolver |
| 7 |
import os |
| 8 |
import uuid |
| 9 |
import time |
| 10 |
import random |
| 11 |
import sys |
| 12 |
|
| 13 |
|
| 14 |
def create_client_id(): |
| 15 |
return f"aws-crt-python-canary-test-{uuid.uuid4()}" |
| 16 |
|
| 17 |
|
| 18 |
def _get_env(name): |
| 19 |
val = os.environ.get(name) |
| 20 |
if not val: |
| 21 |
raise Exception(name + "environment variable required for canary") |
| 22 |
return val |
| 23 |
|
| 24 |
|
| 25 |
TIMEOUT = 100.0 |
| 26 |
endpoint = _get_env("ENDPOINT") |
| 27 |
port = _get_env("AWS_TEST_MQTT5_DIRECT_MQTT_PORT") |
| 28 |
seconds = _get_env("CANARY_DURATION") |
| 29 |
threads = _get_env("CANARY_THREADS") |
| 30 |
tps = _get_env("CANARY_TPS") |
| 31 |
client_count = _get_env("CANARY_CLIENT_COUNT") |
| 32 |
log_level = _get_env("CANARY_LOG_LEVEL") |
| 33 |
elg = EventLoopGroup(num_threads=int(threads)) |
| 34 |
resolver = DefaultHostResolver(elg) |
| 35 |
bootstrap = ClientBootstrap(elg, resolver) |
| 36 |
|
| 37 |
|
| 38 |
class CanaryCore(): |
| 39 |
def __init__(self): |
| 40 |
|
| 41 |
self.stat_publishes_received = 0 |
| 42 |
self.stat_subscribes_attempted = 0 |
| 43 |
self.stat_subscribes_succeeded = 0 |
| 44 |
self.stat_unsubscribes_attempted = 0 |
| 45 |
self.stat_unsubscribes_succeeded = 0 |
| 46 |
self.stat_publishes_attempted = 0 |
| 47 |
self.stat_publishes_succeeded = 0 |
| 48 |
self.stat_total_operations = 0 |
| 49 |
self.stat_total_starts = 0 |
| 50 |
self.stat_total_stops = 0 |
| 51 |
|
| 52 |
self.future_connection_success = None |
| 53 |
self.future_stopped = None |
| 54 |
|
| 55 |
self.subscriptions = [] |
| 56 |
|
| 57 |
def ws_handshake_transform(self, transform_args): |
| 58 |
transform_args.set_done() |
| 59 |
|
| 60 |
def on_publish_received(self, publish_received_data: mqtt5.PublishReceivedData): |
| 61 |
self.stat_publishes_received += 1 |
| 62 |
|
| 63 |
def on_lifecycle_stopped(self, lifecycle_stopped: mqtt5.LifecycleStoppedData): |
| 64 |
if self.future_stopped is not None: |
| 65 |
if self.future_stopped.done(): |
| 66 |
pass |
| 67 |
else: |
| 68 |
self.future_stopped.set_result(None) |
| 69 |
|
| 70 |
def on_lifecycle_attempting_connect(self, lifecycle_attempting_connect: mqtt5.LifecycleAttemptingConnectData): |
| 71 |
pass |
| 72 |
|
| 73 |
def on_lifecycle_connection_success(self, lifecycle_connection_success: mqtt5.LifecycleConnectSuccessData): |
| 74 |
if self.future_connection_success is not None: |
| 75 |
if self.future_connection_success.done(): |
| 76 |
pass |
| 77 |
else: |
| 78 |
self.future_connection_success.set_result(lifecycle_connection_success) |
| 79 |
|
| 80 |
def on_lifecycle_connection_failure(self, lifecycle_connection_failure: mqtt5.LifecycleConnectFailureData): |
| 81 |
pass |
| 82 |
|
| 83 |
def on_lifecycle_disconnection(self, lifecycle_disconnect_data: mqtt5.LifecycleDisconnectData): |
| 84 |
pass |
| 85 |
|
| 86 |
|
| 87 |
class CanaryClient(): |
| 88 |
user_properties = [] |
| 89 |
user_properties.append(mqtt5.UserProperty(name="name1", value="value1")) |
| 90 |
user_properties.append(mqtt5.UserProperty(name="name2", value="value2")) |
| 91 |
|
| 92 |
def __init__(self): |
| 93 |
self.client_id = create_client_id() |
| 94 |
self.canary_core = CanaryCore() |
| 95 |
self.client = self._create_client(canary_core=self.canary_core) |
| 96 |
self.stopped = True |
| 97 |
|
| 98 |
def _create_client(self, |
| 99 |
client_options: mqtt5.ClientOptions = None, |
| 100 |
canary_core: CanaryCore = None): |
| 101 |
|
| 102 |
if client_options is None: |
| 103 |
client_options = mqtt5.ClientOptions(endpoint, port, bootstrap=bootstrap) |
| 104 |
|
| 105 |
if client_options.connect_options is None: |
| 106 |
client_options.connect_options = mqtt5.ConnectPacket() |
| 107 |
client_options.connect_options.client_id = create_client_id() |
| 108 |
|
| 109 |
client_options.host_name = endpoint |
| 110 |
client_options.port = int(port) |
| 111 |
|
| 112 |
if canary_core is not None: |
| 113 |
client_options.on_publish_callback_fn = canary_core.on_publish_received |
| 114 |
client_options.on_lifecycle_event_stopped_fn = canary_core.on_lifecycle_stopped |
| 115 |
client_options.on_lifecycle_event_attempting_connect_fn = canary_core.on_lifecycle_attempting_connect |
| 116 |
client_options.on_lifecycle_event_connection_success_fn = canary_core.on_lifecycle_connection_success |
| 117 |
client_options.on_lifecycle_event_connection_failure_fn = canary_core.on_lifecycle_connection_failure |
| 118 |
client_options.on_lifecycle_event_disconnection_fn = canary_core.on_lifecycle_disconnection |
| 119 |
|
| 120 |
client = mqtt5.Client(client_options) |
| 121 |
return client |
| 122 |
|
| 123 |
def random_operation(self): |
| 124 |
self.canary_core.stat_total_operations += 1 |
| 125 |
operation = random.randint(0, 100) |
| 126 |
|
| 127 |
if self.stopped: |
| 128 |
self.start() |
| 129 |
elif operation < 10: |
| 130 |
self.subscribe() |
| 131 |
elif operation < 20: |
| 132 |
self.unsubscribe() |
| 133 |
elif operation < 99: |
| 134 |
self.publish() |
| 135 |
else: |
| 136 |
if not self.stopped: |
| 137 |
self.stop() |
| 138 |
|
| 139 |
def start(self): |
| 140 |
if not self.stopped: |
| 141 |
return |
| 142 |
|
| 143 |
self.client.start() |
| 144 |
self.canary_core.stat_total_starts += 1 |
| 145 |
self.stopped = False |
| 146 |
|
| 147 |
def initial_start(self): |
| 148 |
if not self.stopped: |
| 149 |
return |
| 150 |
|
| 151 |
future_connection_success = Future() |
| 152 |
self.canary_core.future_connection_success = future_connection_success |
| 153 |
self.client.start() |
| 154 |
future_connection_success.result(TIMEOUT) |
| 155 |
self.stopped = False |
| 156 |
|
| 157 |
def stop(self): |
| 158 |
if self.stopped: |
| 159 |
return |
| 160 |
self.stopped = True |
| 161 |
self.client.stop() |
| 162 |
self.canary_core.stat_total_stops += 1 |
| 163 |
|
| 164 |
def final_stop(self): |
| 165 |
if self.stopped: |
| 166 |
return |
| 167 |
self.stopped = True |
| 168 |
future_stopped = Future() |
| 169 |
self.canary_core.future_stopped = future_stopped |
| 170 |
self.client.stop() |
| 171 |
future_stopped.result(TIMEOUT) |
| 172 |
|
| 173 |
def publish(self): |
| 174 |
self.canary_core.stat_publishes_attempted += 1 |
| 175 |
|
| 176 |
if len(self.canary_core.subscriptions) > 0: |
| 177 |
topic_filter = self.canary_core.subscriptions[0] |
| 178 |
else: |
| 179 |
topic_filter = str(time.time()) + self.client_id |
| 180 |
|
| 181 |
publish_packet = mqtt5.PublishPacket( |
| 182 |
topic=topic_filter, |
| 183 |
qos=random.randint(0, 1), |
| 184 |
payload=bytearray(os.urandom(random.randint(0, 10000))) |
| 185 |
) |
| 186 |
|
| 187 |
if random.getrandbits(1): |
| 188 |
publish_packet.user_properties = self.user_properties |
| 189 |
|
| 190 |
try: |
| 191 |
self.client.publish(publish_packet=publish_packet) |
| 192 |
self.canary_core.stat_publishes_succeeded += 1 |
| 193 |
except BaseException: |
| 194 |
pass |
| 195 |
|
| 196 |
def subscribe(self, topic_filter: str = None, qos: int = 1): |
| 197 |
self.canary_core.stat_subscribes_attempted += 1 |
| 198 |
if topic_filter is None: |
| 199 |
topic_filter = str(time.time()) + self.client_id |
| 200 |
|
| 201 |
self.canary_core.subscriptions.append(topic_filter) |
| 202 |
|
| 203 |
subscribe_packet = mqtt5.SubscribePacket(subscriptions=[mqtt5.Subscription(topic_filter=topic_filter, qos=qos)]) |
| 204 |
|
| 205 |
try: |
| 206 |
self.client.subscribe(subscribe_packet=subscribe_packet) |
| 207 |
self.canary_core.stat_subscribes_succeeded += 1 |
| 208 |
except BaseException: |
| 209 |
pass |
| 210 |
|
| 211 |
def unsubscribe(self): |
| 212 |
if len(self.canary_core.subscriptions) < 1: |
| 213 |
return |
| 214 |
|
| 215 |
self.canary_core.stat_unsubscribes_attempted += 1 |
| 216 |
unsubscribe_packet = mqtt5.UnsubscribePacket(topic_filters=[self.canary_core.subscriptions.pop()]) |
| 217 |
|
| 218 |
try: |
| 219 |
self.client.unsubscribe(unsubscribe_packet=unsubscribe_packet) |
| 220 |
self.canary_core.stat_unsubscribes_succeeded += 1 |
| 221 |
except BaseException: |
| 222 |
pass |
| 223 |
|
| 224 |
def print_stats(self): |
| 225 |
print(f"""\n |
| 226 |
Client Stats: |
| 227 |
publishes_received: {self.canary_core.stat_publishes_received} |
| 228 |
|
| 229 |
subscribes_attempted: {self.canary_core.stat_subscribes_attempted} |
| 230 |
subscribes_succeeded: {self.canary_core.stat_subscribes_succeeded} |
| 231 |
|
| 232 |
unsubscribes_attempted: {self.canary_core.stat_unsubscribes_attempted} |
| 233 |
unsubscribes_succeeded: {self.canary_core.stat_unsubscribes_succeeded} |
| 234 |
|
| 235 |
publishes_attempted: {self.canary_core.stat_publishes_attempted} |
| 236 |
publishes_succeeded: {self.canary_core.stat_publishes_succeeded} |
| 237 |
|
| 238 |
total_starts: {self.canary_core.stat_total_starts} |
| 239 |
total_stops: {self.canary_core.stat_total_stops} |
| 240 |
|
| 241 |
total operations: {self.canary_core.stat_total_operations}""", file=sys.stdout) |
| 242 |
|
| 243 |
|
| 244 |
if __name__ == '__main__': |
| 245 |
client = CanaryClient() |
| 246 |
time_end = time.time() + float(seconds) |
| 247 |
tpsdelay = 1.0 / float(tps) |
| 248 |
time_next_operation = time.time() |
| 249 |
|
| 250 |
print(f"""\n |
| 251 |
Canary running for {seconds} seconds |
| 252 |
TPS: {tps} |
| 253 |
Clients: {client_count} |
| 254 |
Threads: {threads} |
| 255 |
""", file=sys.stdout) |
| 256 |
|
| 257 |
clients = [] |
| 258 |
for i in range(int(client_count)): |
| 259 |
clients.append(CanaryClient()) |
| 260 |
|
| 261 |
for client in clients: |
| 262 |
client.initial_start() |
| 263 |
|
| 264 |
for client in clients: |
| 265 |
client.subscribe() |
| 266 |
|
| 267 |
while time.time() < time_end: |
| 268 |
if time.time() >= time_next_operation: |
| 269 |
time_next_operation += tpsdelay |
| 270 |
random.choice(clients).random_operation() |
| 271 |
|
| 272 |
for client in clients: |
| 273 |
client.final_stop() |
| 274 |
|
| 275 |
for client in clients: |
| 276 |
client.print_stats() |
| 277 |
|
| 278 |
for client in clients: |
| 279 |
del client |
| 280 |
|
| 281 |
time.sleep(0.1) |