|
| 1 | +""" |
| 2 | +Lambda function for Python Driver testing |
| 3 | +
|
| 4 | +Creates the client that is cached for all requests, subscribes to |
| 5 | +relevant events, and forces the connection pool to get populated. |
| 6 | +""" |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | +from bson import has_c as has_bson_c |
| 11 | +from pymongo import MongoClient |
| 12 | +from pymongo import has_c as has_pymongo_c |
| 13 | +from pymongo.monitoring import ( |
| 14 | + CommandListener, |
| 15 | + ConnectionPoolListener, |
| 16 | + ServerHeartbeatListener, |
| 17 | +) |
| 18 | + |
| 19 | +open_connections = 0 |
| 20 | +heartbeat_count = 0 |
| 21 | +total_heartbeat_duration = 0 |
| 22 | +total_commands = 0 |
| 23 | +total_command_duration = 0 |
| 24 | + |
| 25 | +# Ensure we are using C extensions |
| 26 | +assert has_bson_c() |
| 27 | +assert has_pymongo_c() |
| 28 | + |
| 29 | + |
| 30 | +class CommandHandler(CommandListener): |
| 31 | + def started(self, event): |
| 32 | + print("command started", event) |
| 33 | + |
| 34 | + def succeeded(self, event): |
| 35 | + global total_commands, total_command_duration |
| 36 | + total_commands += 1 |
| 37 | + total_command_duration += event.duration_micros / 1e6 |
| 38 | + print("command succeeded", event) |
| 39 | + |
| 40 | + def failed(self, event): |
| 41 | + global total_commands, total_command_duration |
| 42 | + total_commands += 1 |
| 43 | + total_command_duration += event.duration_micros / 1e6 |
| 44 | + print("command failed", event) |
| 45 | + |
| 46 | + |
| 47 | +class ServerHeartbeatHandler(ServerHeartbeatListener): |
| 48 | + def started(self, event): |
| 49 | + print("server heartbeat started", event) |
| 50 | + |
| 51 | + def succeeded(self, event): |
| 52 | + global heartbeat_count, total_heartbeat_duration |
| 53 | + heartbeat_count += 1 |
| 54 | + total_heartbeat_duration += event.duration |
| 55 | + print("server heartbeat succeeded", event) |
| 56 | + |
| 57 | + def failed(self, event): |
| 58 | + global heartbeat_count, total_heartbeat_duration |
| 59 | + heartbeat_count += 1 |
| 60 | + total_heartbeat_duration += event.duration |
| 61 | + print("server heartbeat failed", event) |
| 62 | + |
| 63 | + |
| 64 | +class ConnectionHandler(ConnectionPoolListener): |
| 65 | + def connection_created(self, event): |
| 66 | + global open_connections |
| 67 | + open_connections += 1 |
| 68 | + print("connection created") |
| 69 | + |
| 70 | + def connection_ready(self, event): |
| 71 | + pass |
| 72 | + |
| 73 | + def connection_closed(self, event): |
| 74 | + global open_connections |
| 75 | + open_connections -= 1 |
| 76 | + print("connection closed") |
| 77 | + |
| 78 | + def connection_check_out_started(self, event): |
| 79 | + pass |
| 80 | + |
| 81 | + def connection_check_out_failed(self, event): |
| 82 | + pass |
| 83 | + |
| 84 | + def connection_checked_out(self, event): |
| 85 | + pass |
| 86 | + |
| 87 | + def connection_checked_in(self, event): |
| 88 | + pass |
| 89 | + |
| 90 | + def pool_created(self, event): |
| 91 | + pass |
| 92 | + |
| 93 | + def pool_ready(self, event): |
| 94 | + pass |
| 95 | + |
| 96 | + def pool_cleared(self, event): |
| 97 | + pass |
| 98 | + |
| 99 | + def pool_closed(self, event): |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +listeners = [CommandHandler(), ServerHeartbeatHandler(), ConnectionHandler()] |
| 104 | +print("Creating client") |
| 105 | +client = MongoClient(os.environ["MONGODB_URI"], event_listeners=listeners) |
| 106 | + |
| 107 | + |
| 108 | +# Populate the connection pool. |
| 109 | +print("Connecting") |
| 110 | +client.lambdaTest.list_collections() |
| 111 | +print("Connected") |
| 112 | + |
| 113 | + |
| 114 | +# Create the response to send back. |
| 115 | +def create_response(): |
| 116 | + return dict( |
| 117 | + averageCommandDuration=total_command_duration / total_commands, |
| 118 | + averageHeartbeatDuration=total_heartbeat_duration / heartbeat_count, |
| 119 | + openConnections=open_connections, |
| 120 | + heartbeatCount=heartbeat_count, |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +# Reset the numbers. |
| 125 | +def reset(): |
| 126 | + global open_connections, heartbeat_count, total_heartbeat_duration, total_commands, total_command_duration |
| 127 | + open_connections = 0 |
| 128 | + heartbeat_count = 0 |
| 129 | + total_heartbeat_duration = 0 |
| 130 | + total_commands = 0 |
| 131 | + total_command_duration = 0 |
| 132 | + |
| 133 | + |
| 134 | +def lambda_handler(event, context): |
| 135 | + """ |
| 136 | + The handler function itself performs an insert/delete and returns the |
| 137 | + id of the document in play. |
| 138 | + """ |
| 139 | + print("initializing") |
| 140 | + db = client.lambdaTest |
| 141 | + collection = db.test |
| 142 | + result = collection.insert_one({"n": 1}) |
| 143 | + collection.delete_one({"_id": result.inserted_id}) |
| 144 | + # Create the response and then reset the numbers. |
| 145 | + response = json.dumps(create_response()) |
| 146 | + reset() |
| 147 | + print("finished!") |
| 148 | + |
| 149 | + return dict(statusCode=200, body=response) |
0 commit comments