|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import data |
| 4 | + |
| 5 | +from locust import events, HttpUser, constant, task, SequentialTaskSet, tag |
| 6 | + |
| 7 | +URL = 'http://adm:pass@localhost:15984' |
| 8 | +DB = 'http://adm:pass@localhost:15984/demo' |
| 9 | +SESSION = requests.session() |
| 10 | + |
| 11 | + |
| 12 | +def create_database(): |
| 13 | + if SESSION.get(DB).status_code == 200: |
| 14 | + SESSION.delete(DB) |
| 15 | + SESSION.put(DB) |
| 16 | + |
| 17 | + |
| 18 | +def insert_docs(): |
| 19 | + payload = {"docs": []} |
| 20 | + with open('data.json') as json_file: |
| 21 | + payload['docs'].extend(json.load(json_file)) |
| 22 | + SESSION.post(DB + '/_bulk_docs', json=payload, headers={"Content-Type": "application/json"}) |
| 23 | + |
| 24 | + |
| 25 | +def create_indexes(): |
| 26 | + design_docs = { |
| 27 | + "_id": "_design/search", |
| 28 | + "indexes": { |
| 29 | + "search_index": { |
| 30 | + "index": "function(doc) {if(doc.gender) {index(\"gender\", doc.gender, {\"store\": true} );};" |
| 31 | + "if(doc.age) {index(\"age\", doc.age, {\"store\": true} );};" |
| 32 | + "if(doc.married) {index(\"married\", doc.married, {\"store\": true} );};" |
| 33 | + "if(doc.ethnicity) {index(\"ethnicity\", doc.ethnicity, {\"store\": true} );}}" |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + SESSION.put(f'{DB}/_design/search', data=json.dumps(design_docs)) |
| 38 | + |
| 39 | + |
| 40 | +def get_result(condition, response, func_name): |
| 41 | + response.success() if condition else response.failure(func_name + ' FAILED.') |
| 42 | + |
| 43 | + |
| 44 | +@events.init_command_line_parser.add_listener |
| 45 | +def _(parser): |
| 46 | + parser.add_argument("--docs-number", type=int, env_var="LOCUST_DOCS_NUMBER", default=5, |
| 47 | + help="How many documents do you want to generate") |
| 48 | + |
| 49 | + |
| 50 | +@events.test_start.add_listener |
| 51 | +def _(environment, **kw): |
| 52 | + print('1. Generate documents') |
| 53 | + data.gen_data(environment.parsed_options.docs_number) |
| 54 | + |
| 55 | + |
| 56 | +class CouchDBTest(SequentialTaskSet): |
| 57 | + def on_start(self): |
| 58 | + self.client.get('/', name=self.on_start.__name__) |
| 59 | + print('2. Create Database, Insert docs and _design docs') |
| 60 | + create_database() |
| 61 | + insert_docs() |
| 62 | + create_indexes() |
| 63 | + print('3. Start testing ... ') |
| 64 | + with open('analysis.json') as json_file: |
| 65 | + self.data = json.load(json_file) |
| 66 | + |
| 67 | + @tag('get') |
| 68 | + @task |
| 69 | + def get_all_dbs(self): |
| 70 | + with self.client.get('/demo/_all_dbs', catch_response=True, name='Get All DBs') as response: |
| 71 | + get_result( |
| 72 | + len(response.text) and response.elapsed.total_seconds() < 2.0, |
| 73 | + response, self.get_all_dbs.__name__) |
| 74 | + |
| 75 | + @tag('get') |
| 76 | + @task |
| 77 | + def get_all_docs(self): |
| 78 | + with self.client.get('/demo/_all_docs', catch_response=True, name='Get All Docs') as response: |
| 79 | + get_result( |
| 80 | + len(response.text) and response.elapsed.total_seconds() < 2.0, |
| 81 | + response, self.get_all_docs.__name__) |
| 82 | + |
| 83 | + @tag('search') |
| 84 | + @task |
| 85 | + def search_all_docs(self): |
| 86 | + with self.client.get('/demo/_design/search/_search/search_index?query=*:*', |
| 87 | + catch_response=True, name='Search All Docs') as response: |
| 88 | + get_result( |
| 89 | + response.status_code == 200 and response.json()['total_rows'] == self.data['total_rows'], |
| 90 | + response, self.search_all_docs.__name__) |
| 91 | + |
| 92 | + @tag('search') |
| 93 | + @task |
| 94 | + def search_gender_is_male(self): |
| 95 | + with self.client.get('/demo/_design/search/_search/search_index?query=gender:m', |
| 96 | + catch_response=True, name='Search Gender is Male') as response: |
| 97 | + get_result( |
| 98 | + response.status_code == 200 and response.json()['total_rows'] == self.data['gender']['M'], |
| 99 | + response, self.search_gender_is_male.__name__) |
| 100 | + |
| 101 | + @tag('search') |
| 102 | + @task |
| 103 | + def search_gender_is_male_with_limit_2(self): |
| 104 | + with self.client.get('/demo/_design/search/_search/search_index?query=gender:m&limit=2', |
| 105 | + catch_response=True, name='Search Gender is Male with Limit 2') as response: |
| 106 | + get_result( |
| 107 | + response.status_code == 200 and len(response.json()['rows']) == 2, |
| 108 | + response, self.search_gender_is_male_with_limit_2.__name__) |
| 109 | + |
| 110 | + @tag('search') |
| 111 | + @task |
| 112 | + def search_gender_is_female_and_sort_by_age(self): |
| 113 | + with self.client.get('/demo/_design/search/_search/search_index?query=gender:f&sort="age"', |
| 114 | + catch_response=True, name='Search Gender is Female AND Sort by age') as response: |
| 115 | + result = response.json() |
| 116 | + if self.data['gender']['F'] >= 2: |
| 117 | + conditions = result['total_rows'] == self.data['gender']['F'] and \ |
| 118 | + result['rows'][0]['fields']['age'] <= result['rows'][1]['fields']['age'] |
| 119 | + else: |
| 120 | + conditions = result['total_rows'] == self.data['gender']['F'] |
| 121 | + get_result(conditions, response, self.search_gender_is_female_and_sort_by_age.__name__) |
| 122 | + |
| 123 | + @tag('search') |
| 124 | + @task |
| 125 | + def search_married_people_age_should_greater_than_21(self): |
| 126 | + with self.client.get( |
| 127 | + '/demo/_design/search/_search/search_index?query=married:true', |
| 128 | + catch_response=True, name='Search married people age > 21') as response: |
| 129 | + result = response.json() |
| 130 | + for i in result['rows']: |
| 131 | + if i['fields']['age'] <= 21: |
| 132 | + response.failure(self.search_married_people_age_should_greater_than_21.__name__) |
| 133 | + response.success() |
| 134 | + |
| 135 | + @tag('search') |
| 136 | + @task |
| 137 | + def search_ethnicity_White_OR_Asian(self): |
| 138 | + with self.client.get( |
| 139 | + f'/demo/_design/search/_search/search_index?query=ethnicity:White OR ethnicity:Asian', |
| 140 | + catch_response=True, name='Search ethnicity White OR Asian') as response: |
| 141 | + result = response.json() |
| 142 | + get_result( |
| 143 | + response.status_code == 200 and |
| 144 | + result['total_rows'] == self.data['ethnicity']['White'] + self.data['ethnicity']['Asian'], |
| 145 | + response, self.search_ethnicity_White_OR_Asian.__name__) |
| 146 | + |
| 147 | + def on_stop(self): |
| 148 | + self.client.get('/', name=self.on_stop.__name__) |
| 149 | + print("4. Delete database, and shut down the locust") |
| 150 | + SESSION.delete(DB) |
| 151 | + |
| 152 | + |
| 153 | +class LoadTest(HttpUser): |
| 154 | + host = URL |
| 155 | + wait_time = constant(1) |
| 156 | + tasks = [CouchDBTest] |
0 commit comments