-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
63 lines (55 loc) · 1.26 KB
/
bench.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
import json
import random
import requests
import time
import threading
import uuid
URL = 'http://localhost'
PORTS = [8080, 8081, 8082]
LOOPS = 10
BULK_SIZE = 1000
THREADS = 100
INDEX_NAME = 'hello-world'
def build_batch():
s = ''
for i in range(BULK_SIZE):
s += json.dumps({
"index": {
"_index": INDEX_NAME,
"_id": str(uuid.uuid4()),
}
})
s += '\n'
s += json.dumps({"hello": str(uuid.uuid4())})
s += '\n'
return s
def bench_loop(n):
batch = build_batch()
for i in range(LOOPS):
port = random.choice(PORTS)
url = "{}:{}/{}/_bulk".format(
URL,
port,
INDEX_NAME,
)
print("{} {}".format(n, url))
print(requests.post(url, data=batch).content)
def run_benchmark():
threads = []
for n in range(THREADS):
thread = threading.Thread(target=bench_loop, args=[n])
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
start = time.time()
run_benchmark()
end = time.time()
count = THREADS * LOOPS * BULK_SIZE
dt = end - start
rate = count / dt
print("Indexed {} documents in {} seconds ({} docs/sec)".format(
count,
dt,
rate
))