redis_profiler.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from string import Template
  5. from optparse import OptionParser
  6. from rdbtools import RdbParser, MemoryCallback, PrintAllKeys, StatsAggregator
  7. TEMPLATES_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'templates'))
  8. def main():
  9. usage = """usage: %prog [options] /path/to/dump.rdb
  10. Example 1 : %prog -k "user.*" -k "friends.*" -f memoryreport.html /var/redis/6379/dump.rdb
  11. Example 2 : %prog /var/redis/6379/dump.rdb"""
  12. parser = OptionParser(usage=usage)
  13. parser.add_option("-f", "--file", dest="output",
  14. help="Output file", metavar="FILE")
  15. parser.add_option("-k", "--key", dest="keys", action="append",
  16. help="Keys that should be grouped together. Multiple regexes can be provided")
  17. (options, args) = parser.parse_args()
  18. if len(args) == 0:
  19. parser.error("Redis RDB file not specified")
  20. dump_file = args[0]
  21. stats = StatsAggregator()
  22. callback = MemoryCallback(stats, 64)
  23. parser = RdbParser(callback)
  24. parser.parse(dump_file)
  25. stats_as_json = stats.get_json()
  26. with open(os.path.join(TEMPLATES_DIR, "report.html.template"), 'r') as t:
  27. report_template = Template(t.read())
  28. html = report_template.substitute(REPORT_JSON = stats_as_json)
  29. if options.output:
  30. with open(options.output, 'w') as f:
  31. f.write(html)
  32. else:
  33. print(html)
  34. if __name__ == '__main__':
  35. main()