from collections import defaultdict
import re
line_re = re.compile(r"""
^(?P<timestamp>[\d\.]+)\s(\(db\s(?P<db>\d+)\)\s)?"(?P<command>\w+)"(\s"(?P<key>[^(?<!\\)"]+)(?<!\\)")?(\s(?P<args>.+))?$
""", re.VERBOSE)
class StatCounter(object):
def __init__(self, prefix_delim=':'):
self.line_count = 0
self.skipped_lines = 0
self.commands = defaultdict(int)
self.keys = defaultdict(int)
self.prefixes = defaultdict(int)
self.times = []
self._cached_sorts = {}
self.start_ts = None
self.last_ts = None
self.prefix_delim = prefix_delim
def _record_duration(self, entry):
ts = float(entry['timestamp']) * 1000 * 1000 # microseconds
if not self.start_ts:
self.start_ts = ts
self.last_ts = ts
duration = ts - self.last_ts
if duration:
self.times.append((duration, entry))
self.last_ts = ts
def _record_command(self, entry):
self.commands[entry['command']] += 1
def _record_key(self, key):
self.keys[key] += 1
parts = key.split(self.prefix_delim)
if len(parts) > 1:
self.prefixes[parts[0]] += 1
@staticmethod
def _reformat_entry(entry):
max_args_to_show = 5
output = '"%(command)s"' % entry
if entry['key']:
output += ' "%(key)s"' % entry
if entry['args']:
arg_parts = entry['args'].split(' ')
ellipses = ' ...' if len(arg_parts) > max_args_to_show else ''
output += ' %s%s' % (' '.join(arg_parts[0:max_args_to_show]), ellipses)
return output
def _get_or_sort_list(self, ls):
key = id(ls)
if not key in self._cached_sorts:
sorted_items = sorted(ls)
self._cached_sorts[key] = sorted_items
return self._cached_sorts[key]
def _time_stats(self, times):
sorted_times = self._get_or_sort_list(times)
num_times = len(sorted_times)
percent_50 = sorted_times[int(num_times / 2)][0]
percent_75 = sorted_times[int(num_times * .75)][0]
percent_90 = sorted_times[int(num_times * .90)][0]
percent_99 = sorted_times[int(num_times * .99)][0]
return (("Median", percent_50),
("75%", percent_75),
("90%", percent_90),
("99%", percent_99))
def _heaviest_commands(self, times):
times_by_command = defaultdict(int)
for time, entry in times:
times_by_command[entry['command']] += time
return self._top_n(times_by_command)
def _slowest_commands(self, times, n=8):
sorted_times = self._get_or_sort_list(times)
slowest_commands = reversed(sorted_times[-n:])
printable_commands = [(str(time), self._reformat_entry(entry)) \
for time, entry in slowest_commands]
return printable_commands
def _general_stats(self):
total_time = (self.last_ts - self.start_ts) / (1000*1000)
return (
("Lines Processed", self.line_count),
("Commands/Sec", '%.2f' % (self.line_count / total_time))
)
def process_entry(self, entry):
self._record_duration(entry)
self._record_command(entry)
if entry['key']:
self._record_key(entry['key'])
def _top_n(self, stat, n=8):
sorted_items = sorted(stat.iteritems(), key = lambda x: x[1], reverse = True)
return sorted_items[:n]
def _pretty_print(self, result, title, percentages=False):
print title
print '=' * 40
if not result:
print 'n/a\n'
return
max_key_len = max((len(x[0]) for x in result))
max_val_len = max((len(str(x[1])) for x in result))
for key, val in result:
key_padding = max(max_key_len - len(key), 0) * ' '
if percentages:
val_padding = max(max_val_len - len(str(val)), 0) * ' '
val = '%s%s\t(%.2f%%)' % (val, val_padding, (float(val) / self.line_count) * 100)
print key,key_padding,'\t',val
print
def print_stats(self):
self._pretty_print(self._general_stats(), 'Overall Stats')
self._pretty_print(self._top_n(self.prefixes), 'Top Prefixes', percentages = True)
self._pretty_print(self._top_n(self.keys), 'Top Keys', percentages = True)
self._pretty_print(self._top_n(self.commands), 'Top Commands', percentages = True)
self._pretty_print(self._time_stats(self.times), 'Command Time (microsecs)')
self._pretty_print(self._heaviest_commands(self.times), 'Heaviest Commands (microsecs)')
self._pretty_print(self._slowest_commands(self.times), 'Slowest Calls')
def process_input(self, input):
for line in input:
self.line_count += 1
line = line.strip()
match = line_re.match(line)
if not match:
if line != "OK":
self.skipped_lines += 1
continue
self.process_entry(match.groupdict())
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共5个文件
py:2个
redis-faina:1个
sh:1个
资源分类:Python库 所属语言:Python 资源全名:redis-faina-0.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
redis-faina-0.1.0.tar.gz (5个子文件)
redis-faina-0.1.0
PKG-INFO 281B
bin
redis-faina 687B
tools
heroku-redistogo-faina.sh 2KB
setup.py 467B
redis_faina
__init__.py 5KB
共 5 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- BrushNet电商公司和摄影公司都在用的AI工作流
- 12_base.apk
- 520马上到了两款动态爱心表白HTML代码(附源码)李峋同款爱心,快送给你爱的她或(他)吧12 情侣纪念日代码.zip
- 电子设计竞赛的单相不间断电源设计
- cutcamera1715961370938.png
- 基于MATLAB的图像处理课程设计报告.doc
- tensorflow-gpu-2.6.0-cp38-cp38-manylinux2010-x86-64.whl
- mmexport1715960553858.png
- tensorflow-gpu-2.6.0-cp37-cp37m-manylinux2010-x86-64.whl
- 通过 .NET 应用程序中的源代码查找 SQL 注入
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功