-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestMemory.py
84 lines (69 loc) · 2.52 KB
/
testMemory.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
78
79
80
81
82
83
84
"""
Script to profile memory consumption for dictionary objects
versus home made custom objects
"""
from __future__ import print_function
import os
import sys
import time
import random
import string
import psutil
from memory_profiler import profile
def randomString(stringLength):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
class MSBlock():
__slots__ = ["name", "blockSize", "locations"]
def __init__(self, name, size, locations):
self.name = name
self.size = size
self.location = locations
profileFp = open('slots.log', 'w+')
@profile(stream=profileFp)
def createSlots(numDsets):
"""Creates random datasets/blocks/size/location using custom objects"""
data = {}
for i in range(numDsets):
dataset = randomString(100)
data.setdefault(dataset, [])
# 100 blocks
for b in range(100):
blockName = randomString(125)
locations = []
for site in range(3):
locations.append(randomString(20))
block = MSBlock(blockName, random.randint(1e5, 1e7), locations)
data[dataset].append(block)
print("%s dataset objects created" % numDsets)
profileFp = open('dicts.log', 'w+')
@profile(stream=profileFp)
def createDicts(numDsets):
"""Creates random datasets/blocks/size/location using dict objects"""
data = {}
for i in range(numDsets):
dataset = randomString(100)
data.setdefault(dataset, {})
# 100 blocks
for b in range(100):
blockName = randomString(125)
block = {blockName: {"blockSize": random.randint(1e5, 1e7), "locations": []}}
for site in range(3):
block[blockName]["locations"].append(randomString(20))
data[dataset].update(block)
print("%s dataset dicts created" % numDsets)
def main():
# make it a different function such that gc can collect objects
thisProcess = psutil.Process(os.getpid())
print("Initial dict RSS: %s" % thisProcess.memory_info().rss)
tStart = time.time()
createDicts(1000)
totalTime = time.time() - tStart
print("Final dict RSS: %s took %s seconds" % (thisProcess.memory_info().rss, totalTime))
print("Initial slots RSS: %s" % thisProcess.memory_info().rss)
tStart = time.time()
createSlots(1000)
totalTime = time.time() - tStart
print("Final slots RSS: %s took %s seconds" % (thisProcess.memory_info().rss, totalTime))
if __name__ == "__main__":
sys.exit(main())