Skip to content

SLOWLOG GET Complexity Analysis #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,22 @@ def parse_zscan(response, **options):


def parse_slowlog_get(response, **options):
return [{
'id': item[0],
'start_time': int(item[1]),
'duration': int(item[2]),
'command': b(' ').join(item[3])
} for item in response]
def parse_item(item):
result = {
'id': item[0],
'start_time': int(item[1]),
'duration': int(item[2]),
}
if len(item) == 5:
# Garantia Data custom Redis result, with complexity analysis
command_idx = 4
result['complexity'] = item[3]
else:
# Vanilla Redis result
command_idx = 3
result['command'] = b(' ').join(item[command_idx])
return result
return [parse_item(item) for item in response]


def parse_cluster_info(response, **options):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ def test_slowlog_get(self, r, slowlog):
assert isinstance(slowlog[0]['start_time'], int)
assert isinstance(slowlog[0]['duration'], int)

# fake/test Garantia Data custom Redis result if we didn't get it
# (with complexity analysis)
if 'complexity' not in slowlog[0]:
# monkey patch parse_response()
COMPLEXITY_STATEMENT = "Complexity info: N:4712,M:3788"
old_parse_response = r.parse_response

def parse_response(connection, command_name, **options):
if command_name != 'SLOWLOG GET':
return old_parse_response(connection,
command_name,
**options)
responses = connection.read_response()
for response in responses:
# Garantia Data returns complexity as fourth item in list
response.insert(3, COMPLEXITY_STATEMENT)
return r.response_callbacks[command_name](responses, **options)
r.parse_response = parse_response

# test
slowlog = r.slowlog_get()
assert isinstance(slowlog, list)
commands = [log['command'] for log in slowlog]
assert get_command in commands
idx = commands.index(get_command)
assert slowlog[idx]['complexity'] == COMPLEXITY_STATEMENT

# tear down monkeypatch
r.parse_response = old_parse_response

def test_slowlog_get_limit(self, r, slowlog):
assert r.slowlog_reset()
r.get('foo')
Expand Down