|
| 1 | +import random |
| 2 | +import string |
| 3 | +import sys |
| 4 | +import time |
| 5 | + |
| 6 | +from pymongo import MongoClient |
| 7 | + |
| 8 | +port = 27017 |
| 9 | +if len(sys.argv) == 2: |
| 10 | + port = int(sys.argv[1]) |
| 11 | +elif len(sys.argv) > 2: |
| 12 | + print("usage: rtss_demo.py <port>") |
| 13 | + |
| 14 | +collections = ['coll1', 'coll2', 'coll3'] |
| 15 | +dbs = ['db1', 'db2', 'db3'] |
| 16 | + |
| 17 | +# Operations Chart + Network In/Out |
| 18 | +def many_ops(m): |
| 19 | + coll = m[dbs[random.randint(0, len(dbs) - 1)]][collections[random.randint(0, len(collections) - 1)]] |
| 20 | + [coll.update_one({"x": 1}, {"$inc": {"x": 1}}) for _ in range( |
| 21 | + random.randint(0,100))] |
| 22 | + [coll.find_one({"x": 1}) for _ in range(random.randint(0, 100))] |
| 23 | + [coll.find_one({"x": 1}) for _ in range(random.randint(0, 100))] |
| 24 | + [coll.find_one({"x": 1}) for _ in range(random.randint(0, 100))] |
| 25 | + [coll.insert_one({"x": 1}) for _ in range(random.randint(0, 100))] |
| 26 | + [coll.delete_one({"x": 1}) for _ in range(random.randint(0, 100))] |
| 27 | + |
| 28 | +# Network Connections |
| 29 | +def many_connections(): |
| 30 | + new_connections = random.randint(0, 5) |
| 31 | + connections = [MongoClient(port=port) for _ in range(new_connections)] |
| 32 | + time.sleep(random.randint(0, 2)) |
| 33 | + for m in connections: |
| 34 | + m.close() |
| 35 | + |
| 36 | +# Memory |
| 37 | +def move_data(m): |
| 38 | + randdata = ''.join(random.choice( |
| 39 | + string.ascii_uppercase + string.digits) for _ in range(100)) |
| 40 | + randkey =''.join(random.choice( |
| 41 | + string.ascii_uppercase + string.digits) for _ in range(100)) |
| 42 | + if random.randint(0, 1): |
| 43 | + print('inserting') |
| 44 | + m.test.coll.insert_many( |
| 45 | + [{randkey: randdata, 'even': random.randint(0, 1)} for _ in range(1000)]) |
| 46 | + else: |
| 47 | + print('deleting') |
| 48 | + m.test.coll.delete_many({'even': random.randint(0, 1)}) |
| 49 | + |
| 50 | + if random.randint(0, 1000) == 5: |
| 51 | + m.test.drop_collection('coll') |
| 52 | + |
| 53 | +while(True): |
| 54 | + many_ops(client) |
| 55 | + many_connections() |
| 56 | + #move_data(client) keep out for now |
| 57 | + time.sleep(random.randint(0, 3)) |
| 58 | + |
0 commit comments