|
| 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] |
0 commit comments