From 07facb7df1b246fba39a686cc2d59d63cba188f3 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Wed, 18 Sep 2013 11:11:51 +0200 Subject: [PATCH] Improve perf --- perf.py | 41 ++++++++++++++++++++++++++++++++++++++++- pygal/_compat.py | 14 ++++++++------ pygal/graph/base.py | 7 +------ pygal/graph/line.py | 2 +- pygal/svg.py | 6 +++--- 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/perf.py b/perf.py index 157877b..44fd767 100644 --- a/perf.py +++ b/perf.py @@ -42,8 +42,47 @@ def prt(s): sys.stdout.write(s) sys.stdout.flush() -charts = CHARTS_NAMES if '--all' in sys.argv else 'Line', +if '--profile' in sys.argv: + import cProfile + c = perf('Line', 500, 500) + cProfile.run("c.render()") + sys.exit(0) + +if '--mem' in sys.argv: + _TWO_20 = float(2 ** 20) + import os + import psutil + import linecache + pid = os.getpid() + process = psutil.Process(pid) + import gc + gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_INSTANCES | gc.DEBUG_OBJECTS) + def print_mem(): + mem = process.get_memory_info()[0] / _TWO_20 + f = sys._getframe(1) + line = linecache.getline(f.f_code.co_filename, f.f_lineno - 1).replace('\n', '') + print('%s:%d \t| %.6f \t| %s' % ( + f.f_code.co_name, f.f_lineno, mem, line)) + + c = perf('Line', 100, 500) + print_mem() + a = c.render() + print_mem() + import objgraph + objgraph.show_refs([c], filename='sample-graph.png') + gc.collect() + print_mem() + print(gc.garbage) + print_mem() + del a + print_mem() + del c + print_mem() + + sys.exit(0) + +charts = CHARTS_NAMES if '--all' in sys.argv else 'Line', for chart in charts: prt('%s\n' % chart) diff --git a/pygal/_compat.py b/pygal/_compat.py index 9d0efdf..944db27 100644 --- a/pygal/_compat.py +++ b/pygal/_compat.py @@ -19,13 +19,15 @@ import sys +if sys.version_info[0] == 3: + base = (str, bytes) + coerce = str +else: + base = basestring + coerce = unicode + + def to_str(string): - if sys.version_info[0] == 3: - base = (str, bytes) - coerce = str - else: - base = basestring - coerce = unicode if not isinstance(string, base): return coerce(string) return string diff --git a/pygal/graph/base.py b/pygal/graph/base.py index 6cacbeb..7fd291a 100644 --- a/pygal/graph/base.py +++ b/pygal/graph/base.py @@ -38,6 +38,7 @@ class BaseGraph(object): def __init__(self, config, series, secondary_series, uuid): """Init the graph""" self.uuid = uuid + self.__dict__.update(config.to_dict()) self.config = config self.series = series or [] self.secondary_series = secondary_series or [] @@ -68,12 +69,6 @@ class BaseGraph(object): self._draw() self.svg.pre_render() - def __getattr__(self, attr): - """Search in config, then in self""" - if hasattr(self.config, attr): - return object.__getattribute__(self.config, attr) - return object.__getattribute__(self, attr) - @property def all_series(self): return self.series + self.secondary_series diff --git a/pygal/graph/line.py b/pygal/graph/line.py index e934372..88366ad 100644 --- a/pygal/graph/line.py +++ b/pygal/graph/line.py @@ -78,7 +78,6 @@ class Line(Graph): if y > self.view.height / 2: classes.append('top') classes = ' '.join(classes) - dots = decorate( self.svg, self.svg.node(serie_node['overlay'], class_="dots"), @@ -132,5 +131,6 @@ class Line(Graph): def _plot(self): for index, serie in enumerate(self.series): self.line(self._serie(index), serie) + for index, serie in enumerate(self.secondary_series, len(self.series)): self.line(self._serie(index), serie, True) diff --git a/pygal/svg.py b/pygal/svg.py index e63fff2..a0410a7 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -66,7 +66,7 @@ class Svg(object): def add_styles(self): """Add the css to the svg""" - colors = self.graph.style.get_colors(self.id) + colors = self.graph.config.style.get_colors(self.id) all_css = [] for css in ['base.css'] + list(self.graph.css): if '://' in css: @@ -80,9 +80,9 @@ class Svg(object): with io.open(css, encoding='utf-8') as f: css_text = template( f.read(), - style=self.graph.style, + style=self.graph.config.style, colors=colors, - font_sizes=self.graph.font_sizes(), + font_sizes=self.graph.config.font_sizes(), id=self.id) if not self.graph.pretty_print: css_text = minify_css(css_text)