import logging import threading import time import pytest from hdrh.histogram import HdrHistogram from urllib3 import connectionpool connectionpool.log.setLevel(logging.ERROR) @pytest.mark.parametrize("info", [ {'path': '/v1/tidbclusters', 'params': {'itemsPerPage': 10, 'page': 1}, 'count': 100}, {'path': '/v1/machines', 'params': {'itemsPerPage': 10, 'page': 1}}, {'path': '/v1/metrics', 'params': {'query': "sum(node_cpu_seconds_total{mode!='idle'})" " / sum(node_cpu_seconds_total)"}}, {'path': '/v1/metrics', 'params': {'query': "sum(node_memory_MemFree_bytes)"}}, {'path': '/v1/metrics', 'params': {'query': "sum(node_memory_MemTotal_bytes)"}}, {'path': '/v1/metrics', 'params': {'query': "sum(node_filesystem_avail_bytes)"}}, {'path': '/v1/metrics', 'params': {'query': "sum(node_filesystem_size_bytes)"}}, {'path': '/v1/machines/nodelabel-distribution'}, {'path': '/v1/top', 'count': 100, 'params': {'type': 'cpu', 'step': 20, 'start': int(time.time())-300, 'end': int(time.time())}}, {'path': '/v1/overview', 'count': 500, 'params': {'type': 'cpu', 'step': 20, 'start': int(time.time())-300, 'end': int(time.time())}}, {'path': '/v1/platformsettings/default-settings', 'count': 100}, {'path': '/v1/platformsettings/user-settings', 'count': 100}, {'path': '/v1/platform/basic-information', 'count': 100}, {'path': '/v1/receivers/alertmanager', 'count': 100}, {'path': '/v1/alerts', 'params': {'itemsPerPage': 10, 'page': 1}}, {'path': '/v1/platform-messages', 'params': {'itemsPerPage': 10, 'page': 1}}, ]) def test_bench_get_requests(cli, info): count = info.get('count', 1000) threads = info.get('threads', 5) h, t = simple_bench(lambda: cli.get(info['path'], params=info.get('params')), count, threads) logging.info("count=%d threads=%d throughput=%.2f latency(s)[50,90,99]=%s path=%s params=%s", h.total_count, threads, h.total_count/t, [h.get_value_at_percentile(p)/1e3 for p in (50, 90, 99)], info['path'], info.get('params')) def simple_bench(request, count=100, threads=1): k, h, lock = [0], HdrHistogram(1, 300000, 2), threading.Lock() def req_loop(): d = None while True: with lock: if d is not None: h.record_value(d) k[0] += 1 if k[0] > count: return t0 = time.time() request() t1 = time.time() d = round((t1 - t0) * 1000) ts = [threading.Thread(target=req_loop) for _ in range(threads)] t0 = time.time() for t in ts: t.start() for t in ts: t.join() t1 = time.time() return h, t1-t0