1 |
|
2 |
import datetime |
3 |
import sys, os |
4 |
from random import randint |
5 |
from time import time |
6 |
import requests |
7 |
import json |
8 |
|
9 |
from random import randrange, randint |
10 |
|
11 |
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'hamlet_distinct.t'), 'r') as f: |
12 |
hamlet_all = f.read() |
13 |
|
14 |
genData = hamlet_all.split(' ') |
15 |
|
16 |
def hamlet(count): |
17 |
return genData[randint(1, len(genData) - 1)] + ('' if count == 1 else ' ' + hamlet(count - 1)) |
18 |
|
19 |
endpoint = 'http://127.0.0.1:9200/librarygen' |
20 |
|
21 |
def call(verb, endpoint, obj = None): |
22 |
headers = { |
23 |
"Accept": "application/json", |
24 |
} |
25 |
endpoint = endpoint.lower() |
26 |
|
27 |
verb = verb.lower() |
28 |
|
29 |
jsonData = json.dumps(obj) |
30 |
|
31 |
if verb == 'get': |
32 |
response = requests.get(endpoint, headers=headers) |
33 |
elif verb == 'post': |
34 |
response = requests.post(endpoint, headers=headers, data=jsonData) |
35 |
elif verb == 'put': |
36 |
response = requests.put(endpoint, headers=headers, data=jsonData) |
37 |
elif verb == 'delete': |
38 |
response = requests.delete(endpoint, headers=headers) |
39 |
|
40 |
return response |
41 |
|
42 |
def create_date(): |
43 |
return '{}-{:02}-{:02}T{:02}:{:02}:{:02}Z'.format(randint(2006, 2016), randint(1, 12), randint(1, 28), randint(0, 23), randint(0, 59), randint(0, 59)) |
44 |
|
45 |
call('PUT', endpoint, { |
46 |
"settings": { |
47 |
"index": { |
48 |
"number_of_replicas": 1, |
49 |
"number_of_shards": 6 |
50 |
} |
51 |
} |
52 |
}) |
53 |
|
54 |
def run(): |
55 |
then = time() |
56 |
count = 0 |
57 |
while True: |
58 |
try: |
59 |
item = { |
60 |
"title": hamlet(4), |
61 |
"authors": [hamlet(_) for _ in range(1, randrange(2,4))], |
62 |
"editor": hamlet(1) if randrange(4) == 0 else None, |
63 |
"abstract": hamlet(randrange(100, 400)), |
64 |
"metadata": { |
65 |
"pages": randrange(1,400), |
66 |
"isbn": '9780' + str(randrange(100000000, 999999999)), |
67 |
"genre": hamlet(1), |
68 |
}, |
69 |
"created": create_date(), |
70 |
"modified": create_date(), |
71 |
} |
72 |
call('POST', '{}/book'.format(endpoint), item) |
73 |
count = count + 1 |
74 |
except KeyboardInterrupt: |
75 |
now = time() |
76 |
print('Stopped ({})'.format(count / (now - then))) |
77 |
sys.exit(0) |
78 |
|
79 |
|
80 |
if __name__ == '__main__': |
81 |
run() |
82 |
|