Created
September 24, 2020 20:45
-
-
Save davisp/3863ab02fb5def733f1a6991651840f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import json | |
| import random | |
| import time | |
| import requests | |
| SESS = requests.session() | |
| SESS.auth = ("adm", "pass") | |
| DB_URL = "http://127.0.0.1:15984/viewdb" | |
| NUM_DOCS = 1000000 | |
| BATCH_SIZE = 1000 | |
| MAX_INT = 2**20 | |
| def maybe_delete_db(): | |
| resp = SESS.get(DB_URL) | |
| if resp.ok: | |
| resp = SESS.delete(DB_URL) | |
| resp.raise_for_status() | |
| time.sleep(5) | |
| def create_db(): | |
| resp = SESS.put(DB_URL, params={"q": "1"}) | |
| resp.raise_for_status() | |
| def populate_db(): | |
| for i in range(NUM_DOCS / BATCH_SIZE): | |
| headers = {"Content-Type": "application/json"} | |
| data = json.dumps({"docs": create_docs(BATCH_SIZE)}) | |
| resp = SESS.post(DB_URL + "/_bulk_docs", headers=headers, data=data) | |
| resp.raise_for_status() | |
| def create_docs(count): | |
| ret = [] | |
| for i in xrange(count): | |
| ret.append({"value": random.randint(1, MAX_INT)}) | |
| return ret | |
| def main(): | |
| maybe_delete_db() | |
| create_db() | |
| populate_db() | |
| if __name__ == "__main__": | |
| main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import json | |
| import requests | |
| SESS = requests.session() | |
| SESS.auth = ("adm", "pass") | |
| DB_URL = "http://127.0.0.1:15984/viewdb" | |
| DDOC = { | |
| "_id": "test", | |
| "views": { | |
| "yar": { | |
| "map": "function(doc) {emit(doc.value, 1);}" | |
| } | |
| }, | |
| "options": {"trace": True} | |
| } | |
| def create_ddoc(): | |
| if SESS.get(DB_URL + "/_design/test").ok: | |
| return | |
| headers = {"Content-Type": "application/json"} | |
| data = json.dumps(DDOC) | |
| resp = SESS.put(DB_URL + "/_design/test", headers=headers, data=data) | |
| resp.raise_for_status() | |
| def query_view(): | |
| resp = SESS.get(DB_URL + "/_design/test/_view/yar") | |
| resp.raise_for_status() | |
| def main(): | |
| create_ddoc() | |
| for i in range(1): | |
| query_view() | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment