Last active
January 30, 2024 06:18
-
-
Save nickva/31e0f1f1c2a5a651259dc897a1bb5cfa to your computer and use it in GitHub Desktop.
Mango execution stats checker
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 time | |
import requests | |
import string | |
import random | |
S = requests.session() | |
S.auth = ('adm', 'pass') | |
S.headers = {'Content-type': 'application/json'} | |
DB_URL = 'http://127.0.0.1:15984/db' | |
NUM_DOCS = 5000 | |
BATCH_SIZE = 500 | |
POPULATE_RANGE = 200 | |
FIND_VAL = 50 | |
FIND_LIMIT = 10 | |
def maybe_delete_db(): | |
resp = S.get(DB_URL) | |
if resp.ok: | |
print(f' * deleting {DB_URL}') | |
resp = S.delete(DB_URL) | |
resp.raise_for_status() | |
time.sleep(1) | |
def create_db(): | |
params = {'q': '8'} | |
print(f' * creating {DB_URL} {params}') | |
resp = S.put(DB_URL, params=params) | |
resp.raise_for_status() | |
def rand_id(): | |
s = string.ascii_lowercase + string.digits | |
return ''.join(random.sample(s, 20)) | |
def populate_db(val_range): | |
print(f' * creating {NUM_DOCS} docs with val range {val_range}') | |
t0 = time.time() | |
random.seed(42) | |
for i in range(NUM_DOCS // BATCH_SIZE): | |
docs = [{'_id':rand_id(), 'x': i % val_range} for i in range(BATCH_SIZE)] | |
data = json.dumps({'docs': docs}) | |
resp = S.post(f'{DB_URL}/_bulk_docs', data=data) | |
resp.raise_for_status() | |
dt = time.time() - t0 | |
print(f' * created docs in {dt:.1f} seconds') | |
def find(limit, val): | |
selector = {'x': val} | |
print(f' * calling _find with selector: {selector}') | |
data = json.dumps({ | |
'selector': selector, | |
'limit': limit, | |
'execution_stats': True | |
}) | |
t0 = time.time() | |
resp = S.post(f'{DB_URL}/_find', data=data) | |
resp.raise_for_status() | |
dt = time.time() - t0 | |
resp_json = resp.json() | |
doc_count = len(resp_json['docs']) | |
exec_stats = resp_json['execution_stats'] | |
print(f' * _find in {dt:.1f} seconds {doc_count} docs {exec_stats}') | |
def main(): | |
maybe_delete_db() | |
create_db() | |
time.sleep(1) | |
populate_db(POPULATE_RANGE) | |
find(FIND_LIMIT, FIND_VAL) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Q=2
Q=4
Q=8
Q=16