| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2025 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import logging |
| 10 |
import os |
| 11 |
import itertools |
| 12 |
import ldap |
| 13 |
import pytest |
| 14 |
import re |
| 15 |
import subprocess |
| 16 |
import time |
| 17 |
from lib389._constants import DEFAULT_SUFFIX |
| 18 |
from lib389.dirsrv_log import DirsrvAccessJSONLog, DirsrvAccessLog |
| 19 |
from test389.topologies import topology_st as topo |
| 20 |
|
| 21 |
|
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
FGOT_KEYS = [ 'wqtime', 'wtime', 'etime', 'optime', 'writetime' ] |
| 25 |
tested_keys = [] |
| 26 |
for r in range(1,len(FGOT_KEYS)): |
| 27 |
tested_keys.extend(itertools.combinations(FGOT_KEYS, r)) |
| 28 |
|
| 29 |
@pytest.fixture(scope="module") |
| 30 |
def setup_topo(topo): |
| 31 |
"""Configure log settings and threads number""" |
| 32 |
inst = topo.standalone |
| 33 |
inst.config.set("nsslapd-accesslog-level", "256") |
| 34 |
inst.config.replace('nsslapd-accesslog-logbuffering', 'off') |
| 35 |
inst.config.replace('nsslapd-threadnumber', '3') |
| 36 |
inst.restart() |
| 37 |
return topo |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture(params=[True,False]) |
| 41 |
def setup_json(request, setup_topo): |
| 42 |
inst = setup_topo.standalone |
| 43 |
format = 'json' if request.param else 'default' |
| 44 |
inst.config.replace('nsslapd-accesslog-log-format', format) |
| 45 |
return (setup_topo, request.param) |
| 46 |
|
| 47 |
|
| 48 |
def get_last_logs(inst, nblines, jsonlog): |
| 49 |
if jsonlog: |
| 50 |
access_log = DirsrvAccessJSONLog(inst) |
| 51 |
else: |
| 52 |
access_log = DirsrvAccessLog(inst) |
| 53 |
log_lines = access_log.readlines() |
| 54 |
while (len(log_lines) > nblines): |
| 55 |
log_lines.pop(0) |
| 56 |
if jsonlog: |
| 57 |
for line in log_lines: |
| 58 |
event = access_log.parse_line(line) |
| 59 |
if event is None or 'header' in event: |
| 60 |
# Skip non-json or header lines |
| 61 |
continue |
| 62 |
if event['operation'] == 'RESULT': |
| 63 |
yield event |
| 64 |
else: |
| 65 |
pattern = r'(\w+)=("(?:[^"\\]|\\.)*"|\S+)' |
| 66 |
for line in log_lines: |
| 67 |
if 'RESULT' in line: |
| 68 |
matches = re.findall(pattern, line) |
| 69 |
d = { m[0]: m[1] for m in matches } |
| 70 |
yield d |
| 71 |
|
| 72 |
|
| 73 |
def test_wqtime(setup_json): |
| 74 |
"""Check wqtime impact when not having enough worker threads |
| 75 |
|
| 76 |
:id: 8559e688-220b-11f1-b2cc-c85309d5c3e3 |
| 77 |
:setup: Standalone instance with very few working threads |
| 78 |
:steps: |
| 79 |
1. Hammer server with searches using ldclt |
| 80 |
2. Check that on some operations wqtime is the main contributor |
| 81 |
:expectedresults: |
| 82 |
1. Success |
| 83 |
2. Success |
| 84 |
""" |
| 85 |
topo, json_log = setup_json |
| 86 |
|
| 87 |
inst = topo.standalone |
| 88 |
cmd = [ 'ldclt', '-H', f'ldap://localhost:{inst.port}', '-e', |
| 89 |
'esearch', '-f', '(uid=*)', '-N', '3' ] |
| 90 |
subprocess.run(cmd, check=True) |
| 91 |
|
| 92 |
max_wqtime_ratio = 0 |
| 93 |
for event in get_last_logs(inst, 40, json_log): |
| 94 |
wqtime = float(event['wqtime']) |
| 95 |
etime = float(event['etime']) |
| 96 |
ratio = int(100 * wqtime / etime) |
| 97 |
if (ratio > max_wqtime_ratio): |
| 98 |
max_wqtime_ratio = ratio |
| 99 |
assert max_wqtime_ratio > 50 |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize("tested_key", tested_keys) |
| 103 |
def test_fgot_config(setup_json, tested_key): |
| 104 |
"""Check fine grain operation timing configuration |
| 105 |
|
| 106 |
:id: 38af4f8a-26c0-11f1-920b-c85309d5c3e3 |
| 107 |
:setup: Standalone instance with very few working threads |
| 108 |
:steps: |
| 109 |
1. Configure ds-fine-grain-operation-timing toi tested value |
| 110 |
2. Perform a search |
| 111 |
3. Check that result has the proper fine grain operation timing |
| 112 |
keywords that are set and nonme of the unset one |
| 113 |
:expectedresults: |
| 114 |
1. Success |
| 115 |
2. Success |
| 116 |
3. Success |
| 117 |
""" |
| 118 |
topo, json_log = setup_json |
| 119 |
inst = topo.standalone |
| 120 |
|
| 121 |
fgot_val = "+".join(tested_key) |
| 122 |
inst.config.replace('ds-fine-grain-operation-timing', fgot_val) |
| 123 |
inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(uid=*)") |
| 124 |
# Wait enough to ensure the result is logged |
| 125 |
log.info(f'Trying: {tested_key}') |
| 126 |
time.sleep(2) |
| 127 |
event = next(get_last_logs(inst, 1, json_log)) |
| 128 |
log.info(f'Got: {event}') |
| 129 |
for k in FGOT_KEYS: |
| 130 |
if k in tested_key: |
| 131 |
assert k in event |
| 132 |
else: |
| 133 |
assert k not in event |
| 134 |
|
| 135 |
|
| 136 |
if __name__ == '__main__': |
| 137 |
# Run isolated |
| 138 |
# -s for DEBUG mode |
| 139 |
CURRENT_FILE = os.path.realpath(__file__) |
| 140 |
pytest.main(["-s", CURRENT_FILE]) |