Skip to content

Commit 054187d

Browse files
authored
Merge pull request #35 from xybaby/master
Ndd new function: growth().
2 parents d717856 + d3939f2 commit 054187d

3 files changed

Lines changed: 60 additions & 20 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Changes
66
3.2.1 (unreleased)
77
------------------
88

9-
- Nothing changed yet.
9+
- New function: :func:`growth`.
1010

1111

1212
3.2.0 (2017-12-20)

objgraph.py

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ def show_most_common_types(
270270
file.write('%-*s %i\n' % (width, name, count))
271271

272272

273-
def show_growth(limit=10, peak_stats={}, shortnames=True, file=None,
274-
filter=None):
275-
"""Show the increase in peak object counts since last call.
273+
def growth(limit=10, peak_stats={}, shortnames=True, filter=None):
274+
"""Count the increase in peak object since last call.
275+
276+
Returns a list of (type_name, total_count, increase_delta),
277+
descending order by increase_delta.
276278
277279
Limits the output to ``limit`` largest deltas. You may set ``limit`` to
278280
None to see all of them.
@@ -287,6 +289,40 @@ def show_growth(limit=10, peak_stats={}, shortnames=True, file=None,
287289
288290
The caveats documented in :func:`typestats` apply.
289291
292+
Example:
293+
294+
>>> growth(2)
295+
[(tuple, 12282, 10), (dict, 1922, 7)]
296+
297+
.. versionadded:: 3.2.1
298+
299+
"""
300+
gc.collect()
301+
stats = typestats(shortnames=shortnames, filter=filter)
302+
deltas = {}
303+
for name, count in iteritems(stats):
304+
old_count = peak_stats.get(name, 0)
305+
if count > old_count:
306+
deltas[name] = count - old_count
307+
peak_stats[name] = count
308+
deltas = sorted(deltas.items(), key=operator.itemgetter(1),
309+
reverse=True)
310+
if limit:
311+
deltas = deltas[:limit]
312+
313+
return [(name, stats[name], delta) for name, delta in deltas]
314+
315+
316+
def show_growth(limit=10, peak_stats=None, shortnames=True, file=None,
317+
filter=None):
318+
"""Show the increase in peak object counts since last call.
319+
320+
if ``peak_stats`` is None, peak object counts will recorded in
321+
func `growth`, and your can record the counts by yourself with set
322+
``peak_stats`` to a dictionary.
323+
324+
The caveats documented in :func:`growth` apply.
325+
290326
Example:
291327
292328
>>> show_growth()
@@ -307,24 +343,16 @@ def show_growth(limit=10, peak_stats={}, shortnames=True, file=None,
307343
New parameter: ``filter``.
308344
309345
"""
310-
gc.collect()
311-
stats = typestats(shortnames=shortnames, filter=filter)
312-
deltas = {}
313-
for name, count in iteritems(stats):
314-
old_count = peak_stats.get(name, 0)
315-
if count > old_count:
316-
deltas[name] = count - old_count
317-
peak_stats[name] = count
318-
deltas = sorted(deltas.items(), key=operator.itemgetter(1),
319-
reverse=True)
320-
if limit:
321-
deltas = deltas[:limit]
322-
if deltas:
346+
if peak_stats is None:
347+
result = growth(limit, shortnames=shortnames, filter=filter)
348+
else:
349+
result = growth(limit, peak_stats, shortnames, filter)
350+
if result:
323351
if file is None:
324352
file = sys.stdout
325-
width = max(len(name) for name, count in deltas)
326-
for name, delta in deltas:
327-
file.write('%-*s%9d %+9d\n' % (width, name, stats[name], delta))
353+
width = max(len(name) for name, _, _ in result)
354+
for name, count, delta in result:
355+
file.write('%-*s%9d %+9d\n' % (width, name, count, delta))
328356

329357

330358
def get_leaking_objects(objects=None):

tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ def test_with_filter(self):
304304
self.assertEqual(1, stats['mymodule.MyClass'])
305305

306306

307+
class GrowthTest(GarbageCollectedMixin, unittest.TestCase):
308+
"""Tests for the growth function."""
309+
310+
def test_growth(self):
311+
objgraph.growth(limit=None)
312+
x = type('MyClass', (), {'__module__': 'mymodule'})() # noqa
313+
growth_info = objgraph.growth(limit=None)
314+
cared = [record for record in growth_info if record[0] == 'MyClass']
315+
self.assertEqual(1, len(cared))
316+
self.assertEqual(1, cared[0][2])
317+
318+
307319
class ByTypeTest(GarbageCollectedMixin, unittest.TestCase):
308320
"""Tests for the by_test function."""
309321

0 commit comments

Comments
 (0)