面试题答案
一键面试选择语言
Python是一个不错的选择,它有丰富的第三方库,处理文件读取、数据处理和统计较为方便,且代码简洁易读。
实现思路
- 读取日志文件:使用Python内置的
open()
函数逐行读取日志文件。例如:
with open('redis_slow_query.log', 'r') as f:
lines = f.readlines()
- 解析日志:日志通常有特定格式,比如
# Slowlog line <id>, <timestamp>, <duration> us, <caller>, <arguments>
,使用字符串的切割或正则表达式提取命令、执行时间等关键信息。例如,使用正则表达式:
import re
pattern = r'# Slowlog line \d+, \d+, (\d+) us,.*?(\w+)'
for line in lines:
match = re.search(pattern, line)
if match:
duration = int(match.group(1))
command = match.group(2)
- 统计信息:使用字典来存储每个命令的执行时间总和、执行次数、最大执行时间等信息。
command_stats = {}
for line in lines:
match = re.search(pattern, line)
if match:
duration = int(match.group(1))
command = match.group(2)
if command not in command_stats:
command_stats[command] = {
'total_duration': 0,
'count': 0,
'max_duration': 0
}
command_stats[command]['total_duration'] += duration
command_stats[command]['count'] += 1
command_stats[command]['max_duration'] = max(command_stats[command]['max_duration'], duration)
- 计算平均执行时间:遍历统计字典,计算每个命令的平均执行时间。
for command, stats in command_stats.items():
stats['average_duration'] = stats['total_duration'] / stats['count'] if stats['count'] > 0 else 0
- 输出结果:可以将统计结果打印输出,或保存到文件中。
for command, stats in command_stats.items():
print(f"Command: {command}, Average Duration: {stats['average_duration']} us, Max Duration: {stats['max_duration']} us")
可能用到的Redis命令
在实际的Redis操作中,若要从Redis获取慢查询日志(假设日志未记录在文件而是直接从Redis获取),可以使用SLOWLOG GET
命令获取慢查询日志列表,使用SLOWLOG LEN
获取慢查询日志的长度,使用SLOWLOG RESET
重置慢查询日志。不过在本题中是基于已有的日志文件分析,未直接与Redis交互。