|
| 1 | +import json |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from random import randrange |
| 6 | + |
| 7 | +import boto3 |
| 8 | + |
| 9 | +client = boto3.client('sns') |
| 10 | + |
| 11 | + |
| 12 | +def handle(event, context): |
| 13 | + input_topic = os.environ.get('INPUT_TOPIC_ARN') |
| 14 | + target = os.environ.get('OUTPUT_TOPIC_ARN') |
| 15 | + run(100, input_topic, target) |
| 16 | + |
| 17 | + |
| 18 | +def run(target, input_topic, amount): |
| 19 | + start = datetime.utcnow() |
| 20 | + for i in range(0, amount): |
| 21 | + delay = randrange(60 * 5) |
| 22 | + scheduled_time = (datetime.utcnow() + timedelta(seconds=delay)).isoformat() |
| 23 | + payload = { |
| 24 | + 'payload': scheduled_time, |
| 25 | + 'date': scheduled_time, |
| 26 | + 'target': target |
| 27 | + } |
| 28 | + |
| 29 | + client.publish( |
| 30 | + TopicArn=input_topic, |
| 31 | + Message=json.dumps(payload) |
| 32 | + ) |
| 33 | + |
| 34 | + if i % 10 == 0: |
| 35 | + total_millis = (datetime.utcnow() - start).total_seconds() * 1000 |
| 36 | + print(f'total: {i}, {int(total_millis / i)} per event') |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == '__main__': |
| 40 | + arguments = sys.argv[1:] |
| 41 | + if len(arguments) >= 1: |
| 42 | + target = arguments[0] |
| 43 | + else: |
| 44 | + print('You must specify the output topic as the first parameter') |
| 45 | + exit() |
| 46 | + |
| 47 | + if len(arguments) >= 3: |
| 48 | + input_topic = arguments[2] |
| 49 | + else: |
| 50 | + input_topic = 'arn:aws:sns:us-east-1:256608350746:scheduler-input-prod' |
| 51 | + |
| 52 | + if len(arguments) >= 2: |
| 53 | + amount = int(arguments[1]) |
| 54 | + else: |
| 55 | + amount = 100 |
| 56 | + |
| 57 | + run(target, input_topic, amount) |
0 commit comments