Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Prefix delimiter Fixes
Browse files Browse the repository at this point in the history
- Allows specifying custom prefix delimiter on command line
- Use argparse instead of fileinput
- Fixes #1, hat-tip to @bobrik
  • Loading branch information
Mike Krieger committed May 16, 2012
1 parent c225d72 commit ab72f2b
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions redis-faina.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env python
import fileinput
import argparse
import sys
from collections import defaultdict
import re

Expand All @@ -9,7 +10,7 @@

class StatCounter(object):

def __init__(self):
def __init__(self, prefix_delim=':'):
self.line_count = 0
self.skipped_lines = 0
self.commands = defaultdict(int)
Expand All @@ -19,7 +20,7 @@ def __init__(self):
self._cached_sorts = {}
self.start_ts = None
self.last_ts = None
self.prefix_delim = ':'
self.prefix_delim = prefix_delim

def _record_duration(self, entry):
ts = float(entry['timestamp']) * 1000 * 1000 # microseconds
Expand Down Expand Up @@ -105,6 +106,10 @@ def _top_n(self, stat, n=8):
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:
Expand All @@ -125,8 +130,8 @@ def print_stats(self):
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):
for line in fileinput.input():
def process_input(self, input):
for line in input:
self.line_count += 1
line = line.strip()
match = line_re.match(line)
Expand All @@ -137,6 +142,19 @@ def process_input(self):
self.process_entry(match.groupdict())

if __name__ == '__main__':
counter = StatCounter()
counter.process_input()
parser = argparse.ArgumentParser()
parser.add_argument(
'input',
type = argparse.FileType('r'),
default = sys.stdin,
nargs = '?',
help = "File to parse; will read from stdin otherwise")
parser.add_argument(
'--prefix-delimiter',
type = str,
help = "String to split on for delimiting prefix and rest of key",
required = False)
args = parser.parse_args()
counter = StatCounter(prefix_delim = args.prefix_delimiter)
counter.process_input(args.input)
counter.print_stats()

0 comments on commit ab72f2b

Please sign in to comment.