Created
October 12, 2016 20:00
-
-
Save mathieulongtin/fa2efceb7b546cbb6626ee899e2cfa0b to your computer and use it in GitHub Desktop.
Prints the number of bytes per db in Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import redis | |
if len(sys.argv) == 0: | |
print "Usage: redis-db-mem.py HOST [PORT]" | |
sys.exit(2) | |
host = sys.argv[1] | |
if len(sys.argv) > 2: | |
port = int(sys.argv[2]) | |
else: | |
port = 6379 | |
info = redis.StrictRedis(host, port).info() | |
dbs = [int(_[2:]) for _ in info if _[0:2] == 'db'] | |
dbs.sort() | |
total_bytes = 0 | |
for db in dbs: | |
r = redis.StrictRedis(host, port, db=db) | |
key_count = 0 | |
byte_count = 0 | |
for key in r.scan_iter('*'): | |
object_dump = r.dump(key) | |
if object_dump: | |
key_count += 1 | |
byte_count += len(object_dump) - 8 | |
total_bytes += byte_count | |
print "db %d: %d objects, %d bytes" % (db, key_count, byte_count) | |
print "total: %d bytes" |
Well, I expected this to make redis unhappy, but it was enough to take down the server. Not sure which line of code did it, but I'm not ready to figure out. Damn. Looked useful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is estimated using the DUMP command. On my servers, I would need to 4X with the result to get the actual memory usage.