本文整理汇总了Python中pympler.summary.summarize函数的典型用法代码示例。如果您正苦于以下问题:Python summarize函数的具体用法?Python summarize怎么用?Python summarize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了summarize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __exit__
def __exit__(self, exc_type, exc_val, exc_tb):
if self.debug:
try:
gc.collect()
end_memory = self.process.memory_info().rss
net_memory = end_memory-self.start_memory
if net_memory > 100 * 1000 * 1000:
Log.warning(
"MEMORY WARNING (additional {{net_memory|comma}}bytes): "+self.description,
default_params=self.params,
net_memory=net_memory
)
from pympler import summary
from pympler import muppy
sum1 = sorted(summary.summarize(muppy.get_objects()), key=lambda r: -r[2])[:30]
Log.warning("{{data}}", data=sum1)
elif end_memory > 1000*1000*1000:
Log.warning(
"MEMORY WARNING (over {{end_memory|comma}}bytes): "+self.description,
default_params=self.params,
end_memory=end_memory
)
from pympler import summary
from pympler import muppy
sum1 = sorted(summary.summarize(muppy.get_objects()), key=lambda r: -r[2])[:30]
Log.warning("{{data}}", data=sum1)
except Exception as e:
Log.warning("problem in memory measure", cause=e)
示例2: file_test
def file_test(rows=500000, cols=50):
"File test"
print("Creating file with {} rows and {} columns".format(rows, cols))
file = create_file(rows, cols)
print("Size of the file: {:.2f} MiB".format(getsize(file) / (1024 * 1024)))
print("Reading file")
sum1 = summarize(get_objects())
las = read(file)
sum2 = summarize(get_objects())
diff = get_diff(sum1, sum2)
print_(diff)
for curve in las.curves:
print("Name: {}, Min: {:.2f}, Mean: {:.2f}, Max: {:.2f}"
.format(curve.mnemonic, nanmin(curve.data), nanmean(curve.data),
nanmax(curve.data)))
del las
las = read(file)
del las
las = read(file)
del las
las = read(file)
del las
print("Happy end")
示例3: process_response
def process_response(self, request, response):
req = request.META['PATH_INFO']
if req.find('static') == -1 and req.find('media') == -1:
print req
self.end_objects = muppy.get_objects()
sum_start = summary.summarize(self.start_objects)
sum_end = summary.summarize(self.end_objects)
diff = summary.get_diff(sum_start, sum_end)
summary.print_(diff)
#print '~~~~~~~~~'
#cb = refbrowser.ConsoleBrowser(response, maxdepth=2, \
#str_func=output_function)
#cb.print_tree()
print '~~~~~~~~~'
a = asizeof(response)
print 'Total size of response object in kB: %s' % \
str(a / 1024.0)
print '~~~~~~~~~'
a = asizeof(self.end_objects)
print 'Total size of end_objects in MB: %s' % \
str(a / 1048576.0)
b = asizeof(self.start_objects)
print 'Total size of start_objects in MB: %s' % \
str(b / 1048576.0)
print '~~~~~~~~~'
return response
示例4: _get_summaries
def _get_summaries(function, *args):
"""Get a 2-tuple containing one summary from before, and one summary
from after the function has been invoked.
"""
s_before = summary.summarize(get_objects())
function(*args)
s_after = summary.summarize(get_objects())
return (s_before, s_after)
示例5: create_summary
def create_summary(self):
"""Return a summary.
See also the notes on ignore_self in the class as well as the
initializer documentation.
"""
if not self.ignore_self:
res = summary.summarize(muppy.get_objects())
else:
# If the user requested the data required to store summaries to be
# ignored in the summaries, we need to identify all objects which
# are related to each summary stored.
# Thus we build a list of all objects used for summary storage as
# well as a dictionary which tells us how often an object is
# referenced by the summaries.
# During this identification process, more objects are referenced,
# namely int objects identifying referenced objects as well as the
# correspondind count.
# For all these objects it will be checked wether they are
# referenced from outside the monitor's scope. If not, they will be
# subtracted from the snapshot summary, otherwise they are
# included (as this indicates that they are relevant to the
# application).
all_of_them = [] # every single object
ref_counter = {} # how often it is referenced; (id(o), o) pairs
def store_info(o):
all_of_them.append(o)
if id(o) in ref_counter:
ref_counter[id(o)] += 1
else:
ref_counter[id(o)] = 1
# store infos on every single object related to the summaries
store_info(self.summaries)
for k, v in self.summaries.items():
store_info(k)
summary._traverse(v, store_info)
# do the summary
res = summary.summarize(muppy.get_objects())
# remove ids stored in the ref_counter
for _id in ref_counter:
# referenced in frame, ref_counter, ref_counter.keys()
if len(gc.get_referrers(_id)) == (3):
summary._subtract(res, _id)
for o in all_of_them:
# referenced in frame, summary, all_of_them
if len(gc.get_referrers(o)) == (ref_counter[id(o)] + 2):
summary._subtract(res, o)
return res
示例6: profile_expose_method
def profile_expose_method(profiled_method_wrapper, accept, args, func, kw, exclude_from_memory_profiling):
"""
Targeted to profile a specific method that wraps HTTP request processing endpoints into database context.
:param profiled_method_wrapper: method wrapped around profiled call to be passed in to memory profiler
:param accept: param specific to profiled call
:param args: args of a function that is being wrapped by a profiled method
:param func: function that is being wrapped by a profiled method
:param kw: kwargs of a function that is being wrapped by a profiled method
:return: output of a profiled method without modification
"""
if not exclude_from_memory_profiling and get_memory_profile_logging_on() and \
check_memory_profile_package_wide_disable(func):
controller_class = args[0].__class__.__name__ if args and len(args) > 0 else ''
end_point_name_parts = [s for s in [func.__module__, controller_class, func.__name__] if s != '']
end_point_name = ".".join(end_point_name_parts)
is_pympler_on = _is_pympler_profiling_value_on(end_point_name)
profile_output = {'output': {}}
if is_pympler_on:
all_objects = muppy.get_objects()
all_objects_summary_before = summary.summarize(all_objects)
memory_profile = memory_usage((_profile_me,
(profile_output, profiled_method_wrapper, func, accept, args, kw),
{}),
interval=0.1)
output = profile_output['output']
if is_pympler_on:
all_objects_summary_after = summary.summarize(all_objects)
diff = summary.get_diff(all_objects_summary_before, all_objects_summary_after)
diff_less = summary.format_(diff)
diff_out = ''
for s in diff_less:
diff_out += s+'\n'
thread_log.info("================ PYMPLER OUTPUT <{}> ==============\n{}".format(end_point_name, diff_out))
try:
message = json.dumps({'log_type': 'memory_profile',
'proc_id': os.getpid(),
'name': func.__name__,
'module': func.__module__,
'mem_profile': memory_profile,
'min': min(memory_profile),
'max': max(memory_profile),
'diff': max(memory_profile) - min(memory_profile),
'leaked': memory_profile[-1] - memory_profile[0],
'args': [arg for arg in args[1:]], # exclude self
'kwargs': kw})
memory_log.info(message,
extra={'controller_module': func.__module__,
'controller_class': controller_class,
'endpoint': func.__name__})
except Exception as e:
thread_log.exception('Logger failed: {}'.format(e))
else:
output = profiled_method_wrapper(accept, args, func, kw)
return output
示例7: test_print_diff
def test_print_diff(self):
"""Test summary can be printed."""
try:
self._stdout = sys.stdout
sys.stdout = self.DevNull()
sum1 = summary.summarize(muppy.get_objects())
sum2 = summary.summarize(muppy.get_objects())
sumdiff = summary.get_diff(sum1, sum2)
summary.print_(sumdiff)
finally:
sys.stdout = self._stdout
示例8: print_diff
def print_diff(self, ignore=[]):
"""Print the diff to the last time the state of objects was measured.
keyword arguments
ignore -- list of objects to ignore
"""
# ignore this and the caller frame
ignore.append(inspect.currentframe()) #PYCHOK change ignore
diff = self.get_diff(ignore)
print("Added objects:")
summary.print_(summary.summarize(diff['+']))
print("Removed objects:")
summary.print_(summary.summarize(diff['-']))
# manual cleanup, see comment above
del ignore[:]
示例9: memusage_before_n_after
def memusage_before_n_after(fun, *args, **kwargs):
from pympler import muppy
from pympler import summary
from datetime import datetime
before = summary.summarize(muppy.get_objects())
before_time = datetime.now()
fun_ret = fun(*args, **kwargs)
after_time = datetime.now()
after = summary.summarize(muppy.get_objects())
diff = summary.get_diff(before, after)
print "execution time: ", after_time - before_time
summary.print_(diff)
return fun_ret, diff
示例10: format_diff
def format_diff(self, ignore=()):
"""Format the diff to the last time the state of objects was measured.
keyword arguments
ignore -- list of objects to ignore
"""
# ignore this and the caller frame
lines = []
diff = self.get_diff(ignore+(inspect.currentframe(),))
lines.append("Added objects:")
for line in summary.format_(summary.summarize(diff['+'])):
lines.append(line)
lines.append("Removed objects:")
for line in summary.format_(summary.summarize(diff['-'])):
lines.append(line)
return lines
示例11: dump_objs
def dump_objs():
global TRACKER
if TRACKER is None:
TRACKER = tracker.SummaryTracker()
with open("obj_log.txt", "a") as fp:
fp.write("Memory at {}\n".format(str(datetime.datetime.now())))
try:
all_objects = muppy.get_objects()
sum1 = summary.summarize(all_objects)
str_sum = summary.format_(sum1)
fp.write("Summary:\n")
for line in str_sum:
fp.write(" {}\n".format(line))
except Exception:
err = traceback.format_exc()
fp.write("Error: \n")
fp.write(err)
try:
str_diff = TRACKER.format_diff()
fp.write("Diff:\n")
for line in str_diff:
fp.write(" {}\n".format(line))
except Exception:
err = traceback.format_exc()
fp.write("Error: \n")
fp.write(err)
fp.write("\n")
示例12: test_sweep
def test_sweep(self):
"""Test that all and only empty entries are removed from a summary."""
objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}]
summ = summary.summarize(objects)
# correct removal of rows when sizes are empty
summary._subtract(summ, {})
summary._subtract(summ, [])
summ = summary._sweep(summ)
found_dict = found_tuple = False
for row in summ:
if row[0] == "<type 'dict'>":
found_dict = True
if row[0] == "<type 'tuple'>":
found_tuple = True
self.assert_(found_dict == False)
self.assert_(found_tuple == False)
# do not remove row if one of the sizes is not empty
# e.g. if the number of objects of a type did not change, but the
# total size did
summ = summary._subtract(summ, 'the')
summ = summary._subtract(summ, 'quick')
summ = summary._subtract(summ, 'brown')
summ = summary._subtract(summ, '42')
summ = summary._sweep(summ)
found_string = False
for row in summ:
if row[0] == summary._repr(''):
found_string = True
self.assert_(row[1] == 0)
totalsize = _getsizeof('fox') - _getsizeof('42')
self.assert_(row[2] == totalsize)
self.assert_(found_string == True)
示例13: format_diff
def format_diff(self, ignore=[]):
"""Format the diff to the last time the state of objects was measured.
keyword arguments
ignore -- list of objects to ignore
"""
# ignore this and the caller frame
ignore.append(inspect.currentframe()) # PYCHOK change ignore
diff = self.get_diff(ignore)
yield "Added objects:"
for line in summary.format_(summary.summarize(diff["+"])):
yield line
yield "Removed objects:"
for line in summary.format_(summary.summarize(diff["-"])):
yield line
# manual cleanup, see comment above
del ignore[:]
示例14: printListingUsage
def printListingUsage(self, args):
all_objects = muppy.get_objects()
sum1 = summary.summarize(all_objects)
summary.print_(sum1)
print(" ")
print("Summary: ")
tr = tracker.SummaryTracker()
tr.print_diff()
示例15: memory_usage
def memory_usage(where):
"""
Print out a basic summary of memory usage.
"""
mem_summary = summary.summarize(muppy.get_objects())
print("Memory summary:", where)
summary.print_(mem_summary, limit=2)
print("VM: %.2fMb" % (get_virtual_memory_usage_kb() / 1024.0))