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