-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
86 lines (69 loc) · 2.44 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
""" User API token rate limiter
"""
import redis
from redis import RedisError
import sys
import signal
import argparse
import random
import string
import time
import threading
import datetime
from random import randint
from time import sleep
ARGS=None
STOP=False
conn=None
def main(argv):
global conn
signal.signal(signal.SIGINT, signal_handler)
parser = argparse.ArgumentParser(description='redistroy, Provision random data and do things')
parser.add_argument('--host', default="127.0.0.1", help='Host (default: 127.0.0.1)')
parser.add_argument('-p', '--port', type=int, default=6379, help='Port (default: 6379)')
parser.add_argument('-u', '--user', required=False, help='User')
parser.add_argument('-P', '--password', required=False, help='Password')
parser.add_argument('-c', '--concurrency', type=int, required=True, help='Concurrency')
parser.add_argument('-i', '--iterations', type=int, default=1000, required=False, help='Iterations')
parser.add_argument('-t', '--threshold', type=int, required=True, help='Allowed operations per minute')
args = parser.parse_args()
global ARGS
ARGS=args
pool = redis.ConnectionPool(host=ARGS.host, port=ARGS.port, db=0, decode_responses=True)
conn = redis.Redis(connection_pool=pool)
threads = []
for x in range(0, ARGS.concurrency):
processThread = threading.Thread(target=api_call)
threads.append(processThread)
processThread.start()
start = time.time()
for x in threads:
x.join()
end = time.time()
print("Duration: " + str(format(round(end-start, 2))))
sys.exit()
def api_call():
token = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
print("initializing API " + token)
for x in range(ARGS.iterations):
if not limiter(token, ARGS.threshold):
print(token + ' is exceeding the threshold')
else:
print(token + ' ok')
sleep(randint(0,100)/1000)
def limiter(token, threshold):
token_minute = "{api:" + token + "}:" + str(datetime.datetime.now().minute)
ops = conn.get(token_minute)
if (ops is not None) and (int(ops) > threshold):
return False
p = conn.pipeline(transaction=True)
p.incr(token_minute)
p.expire(token_minute, 59)
p.execute()
return True
def signal_handler(sig, frame):
print("Simulation stopped")
global STOP
STOP=True
if __name__ == "__main__":
main(sys.argv[1:])