from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import tracemalloc import linecache from io import StringIO def display_top(snapshot, key_type='lineno', limit=10): snapshot = snapshot.filter_traces(( tracemalloc.Filter(False, ""), tracemalloc.Filter(False, ""), )) top_stats = snapshot.statistics(key_type) buf = StringIO() print("Top %s lines" % limit, file=buf) for index, stat in enumerate(top_stats[:limit], 1): frame = stat.traceback[0] print("#%s: %s:%s: %.1f KiB" % (index, frame.filename, frame.lineno, stat.size / 1024), file=buf) line = linecache.getline(frame.filename, frame.lineno).strip() if line: print(' %s' % line, file=buf) other = top_stats[limit:] if other: size = sum(stat.size for stat in other) print("%s other: %.1f KiB" % (len(other), size / 1024), file=buf) total = sum(stat.size for stat in top_stats) print("Total allocated size: %.1f KiB" % (total / 1024), file=buf) buf.seek(0) return buf.read() class Tracemalloc(Page): template_name = 'memory_debug/Page1.html' def vars_for_template(self): snapshot = tracemalloc.take_snapshot() tracemalloc_stats = display_top(snapshot) return dict(stats=tracemalloc_stats) class Pympler(Page): template_name = 'memory_debug/Page1.html' def vars_for_template(self): from pympler import muppy, summary from pympler.summary import format_ all_objects = muppy.get_objects() sum1 = summary.summarize(all_objects) pympler_stats = '\n'.join(format_(sum1, limit=15, sort='size', order='descending')) return dict(stats=pympler_stats) page_sequence = [Tracemalloc, Pympler]