Skip to content

Commit 2b26aae

Browse files
committed
add locust_plugin_data.py
1 parent 2d29c8f commit 2b26aae

File tree

7 files changed

+117
-37
lines changed

7 files changed

+117
-37
lines changed
Binary file not shown.
-1.6 KB
Binary file not shown.

test/random_tree_generator/generate_random_tree.py

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# locust -f locust_plugin_data.py --headless -u 2 -t 10
2+
import json
3+
import requests
4+
from locust.runners import MasterRunner
5+
from locust import events, HttpUser, constant, task, SequentialTaskSet, tag
6+
7+
URL = 'http://adm:pass@localhost:5984'
8+
DB = '/demo'
9+
SESSION = requests.session()
10+
11+
12+
def insert_docs():
13+
payload = {"docs": []}
14+
for i in range(10, 100, 10):
15+
with open(f'data_{i}.json') as json_file:
16+
payload['docs'].append(json.load(json_file))
17+
SESSION.post(URL + DB + '/_bulk_docs', json=payload,
18+
headers={"Content-Type": "application/json"})
19+
20+
21+
def insert_indexes():
22+
design_docs = {
23+
"_id": "_design/search_example",
24+
"indexes": {
25+
"search_index": {
26+
"index": "function(doc) {if(doc.id) {index(\"id\", doc.id, {\"store\": true} );};"
27+
"if(doc.max_length) {index(\"max_length\", doc.max_length, {\"store\": true} );}}"
28+
}
29+
}
30+
}
31+
SESSION.put(URL + DB + '/_design/search_example', data=json.dumps(design_docs))
32+
33+
34+
print('1. Create Database: demo')
35+
SESSION.put(URL + DB)
36+
37+
38+
@events.test_start.add_listener
39+
def _(environment, **_kwargs):
40+
print('2. Insert docs & _design docs')
41+
# happens only once in headless runs, but can happen multiple times in web ui-runs
42+
# in a distributed run, the master does not typically need any test data
43+
if not isinstance(environment.runner, MasterRunner):
44+
insert_docs()
45+
insert_indexes()
46+
47+
48+
class CouchDBTest(SequentialTaskSet):
49+
def on_start(self):
50+
self.client.get('/', name=self.on_start.__name__)
51+
print('Start testing ... ')
52+
53+
@task
54+
@tag('get')
55+
def get_all_docs(self):
56+
with self.client.get('/demo/_all_docs', catch_response=True, name='Get All Docs') as response:
57+
if len(response.text) and response.elapsed.total_seconds() < 2.0:
58+
response.success()
59+
else:
60+
response.failure('get_all_docs FAILED.')
61+
62+
@task
63+
@tag('search')
64+
def search_all_docs(self):
65+
with self.client.get('/demo/_design/search_example/_search/search_index?query=*:*',
66+
catch_response=True, name='Search All Docs') as response:
67+
if response.status_code == 200 and response.json()['total_rows'] == 0:
68+
response.success()
69+
else:
70+
response.failure('search_all_docs FAILED')
71+
72+
def on_stop(self):
73+
self.client.get('/', name=self.on_stop.__name__)
74+
print('Stop')
75+
76+
77+
class LoadTest(HttpUser):
78+
host = URL
79+
wait_time = constant(0.1)
80+
tasks = [CouchDBTest]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
import time
3+
4+
from anytree import RenderTree
5+
from anytree.exporter import DictExporter
6+
from tree import Tree
7+
8+
9+
def export(root, file_name):
10+
exporter = DictExporter()
11+
data = exporter.export(root)
12+
with open(file_name, 'w') as outfile:
13+
json.dump(data, outfile)
14+
15+
16+
def generate(max_depth=0, file_name='data.json'):
17+
if max_depth < 0:
18+
raise Exception('The max_depth of the random tree should be a positive number!')
19+
20+
root = Tree(max_depth)
21+
root.generator()
22+
23+
# for pre, _, node in RenderTree(root):
24+
# tree_str = u"%s%s" % (pre, node.id)
25+
# print(tree_str.ljust(8), node.data)
26+
27+
export(root, file_name)
28+
29+
30+
if __name__ == '__main__':
31+
for i in range(10, 100, 10):
32+
t_start = time.perf_counter()
33+
generate(i, f'data_{i}.json')
34+
t_end = time.perf_counter()
35+
print(f'data_{i}.json:', t_end - t_start)

test/random_tree_generator/test_tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def test_num_branches_between_depth_and_max_depth(x):
1919

2020
def test_generator_with_max_depth_0():
2121
node = Tree(0)
22-
node.generator()
2322
assert node.id == '0_0'
2423
assert node.max_depth == 0
2524
assert node.is_root
@@ -39,3 +38,4 @@ def test_generator_with_positive_max_depth_5():
3938
assert node.parent is None
4039
if len(node.children) != 0:
4140
assert node.children[0].parent == node
41+
assert node.generator() != node

test/random_tree_generator/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import random
22

33
import faker
4-
from anytree import NodeMixin, RenderTree
4+
from anytree import NodeMixin
55

66

77
class Tree(NodeMixin):

0 commit comments

Comments
 (0)