-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathmemprof.py
executable file
·77 lines (67 loc) · 2.92 KB
/
memprof.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Creates count of keys from a document
import argparse
from urlparse import urlparse
import os
import redis
# http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/#c7
def GetHumanReadable(size, precision=2):
suffixes = ['B ', 'KB', 'MB', 'GB', 'TB', 'PB', 'ZB']
suffixIndex = 0
while size > 1024:
suffixIndex += 1 # increment the index of the suffix
size = size / 1024.0 # apply the division
fmt = '{{:4.{}f}} {{}}'.format(precision)
return fmt.format(size, suffixes[suffixIndex])
if __name__ == '__main__':
# handle arguments
parser = argparse.ArgumentParser(description='ReJSON memory profiler', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('file', type=str, default=None, help='JSON filename')
parser.add_argument('-u', '--uri', type=str, default=None, help='Redis server URI')
parser.add_argument('-s', '--steps', type=int, default=5, help='number of steps')
parser.add_argument('-c', '--count', type=int, default=1, help='initial count of documents')
args = parser.parse_args()
if args.uri is not None:
uri = urlparse(args.uri)
else:
uri = None
# initialize
port = None
serverpath = os.path.abspath(os.path.join(os.getcwd(), '../../redis/src/redis-server'))
serverargs = {
'loadmodule': os.path.abspath(os.path.join(os.getcwd(), '../lib/rejson.so')),
'save': '',
}
with open(args.file) as f:
json = f.read()
count = args.count
info = {
'before': [],
'after': [],
}
# TODO make client connect to an existing instance
# client = DisposableRedis(port=port, path=serverpath, **serverargs)
r = redis.StrictRedis()
# Print file and ReJSON sizes
r.execute_command('JSON.SET', 'json', '.', json)
print 'File size: {}'.format(GetHumanReadable(len(json)))
print 'As ReJSON: {}'.format(GetHumanReadable(r.execute_command('JSON.MEMORY', 'json')))
print
# do the steps
print '| Step | Documents | Dataset memory | Server memory |'
print '| ---- | --------- | -------------- | ------------- |'
for i in range(0, args.steps):
# with client as r:
# create documents
info['before'].append(r.info(section='memory'))
for j in range(0, count):
r.execute_command('JSON.SET', 'json:{}:{}'.format(i, j), '.', json)
info['after'].append(r.info(section='memory'))
# print report
row = [i + 1, count,
GetHumanReadable(info['after'][-1]['used_memory_dataset'] - info['before'][-1]['used_memory_dataset']),
GetHumanReadable(info['after'][-1]['used_memory'] - info['before'][-1]['used_memory'])]
fmt = '| {{:>4}} | {{:>{}}} | {{:>14}} | {{:>13}} |'.format(max(args.steps, 9))
print fmt.format(*row)
# wrap up
count = count * 10
print