Skip to content

Instantly share code, notes, and snippets.

@nickva
Last active January 30, 2024 06:18
Show Gist options
  • Save nickva/31e0f1f1c2a5a651259dc897a1bb5cfa to your computer and use it in GitHub Desktop.
Save nickva/31e0f1f1c2a5a651259dc897a1bb5cfa to your computer and use it in GitHub Desktop.
Mango execution stats checker
#!/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()
@nickva
Copy link
Author

nickva commented Jan 29, 2024

Q=2

(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '2'}
 * creating 5000 docs with val range 200
 * created docs in 1.4 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 2396, 'total_docs_examined': 2396, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 72.228}
(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '2'}
 * creating 5000 docs with val range 200
 * created docs in 1.5 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 2466, 'total_docs_examined': 2466, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 101.229}
(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '2'}
 * creating 5000 docs with val range 200
 * created docs in 1.4 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 2492, 'total_docs_examined': 2492, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 90.525}

Q=4

(venv3)  % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '4'}
 * creating 5000 docs with val range 200
 * created docs in 2.4 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 3175, 'total_docs_examined': 3175, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 68.49}
(venv3)  % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '4'}
 * creating 5000 docs with val range 200
 * created docs in 2.2 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 3164, 'total_docs_examined': 3164, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 68.271}
(venv3)  % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '4'}
 * creating 5000 docs with val range 200
 * created docs in 2.2 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 3107, 'total_docs_examined': 3107, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 64.834}

Q=8

(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '8'}
 * creating 5000 docs with val range 200
 * created docs in 3.5 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 4253, 'total_docs_examined': 4253, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 66.808}
(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '8'}
 * creating 5000 docs with val range 200
 * created docs in 3.5 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 4222, 'total_docs_examined': 4222, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 67.306}
(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '8'}
 * creating 5000 docs with val range 200
 * created docs in 3.6 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 4218, 'total_docs_examined': 4218, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 68.228}

Q=16

(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '16'}
 * creating 5000 docs with val range 200
 * created docs in 5.1 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 4975, 'total_docs_examined': 4975, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 61.336}
(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '16'}
 * creating 5000 docs with val range 200
 * created docs in 4.8 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 4889, 'total_docs_examined': 4889, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 65.843}
(venv3) % ./findlimit.py
 * deleting http://127.0.0.1:15984/db
 * creating http://127.0.0.1:15984/db {'q': '16'}
 * creating 5000 docs with val range 200
 * created docs in 5.5 seconds
 * calling _find with selector: {'x': 50}
 * _find in 0.1 seconds 10 docs {'total_keys_examined': 4931, 'total_docs_examined': 4931, 'total_quorum_docs_examined': 0, 'results_returned': 10, 'execution_time_ms': 68.646}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment