8000 Add benchmark to measure gc collection of a big chain of cycles (#243) · python/pyperformance@6d10e48 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d10e48

Browse files
authored
Add benchmark to measure gc collection of a big chain of cycles (#243)
1 parent bd386ad commit 6d10e48

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "pyperformance_bm_gc_collect"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "gc_collect"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pyperf
2+
import gc
3+
4+
CYCLES = 100
5+
LINKS = 20
6+
7+
8+
class Node:
9+
def __init__(self):
10+
self.next = None
11+
self.prev = None
12+
13+
def link_next(self, next):
14+
self.next = next
15+
self.next.prev = self
16+
17+
18+
def create_cycle(node, n_links):
19+
"""Create a cycle of n_links nodes, starting with node."""
20+
21+
if n_links == 0:
22+
return
23+
24+
current = node
25+
for i in range(n_links):
26+
next_node = Node()
27+
current.link_next(next_node)
28+
current = next_node
29+
30+
current.link_next(node)
31+
32+
33+
def create_gc_cycles(n_cycles, n_links):
34+
"""Create n_cycles cycles n_links+1 nodes each."""
35+
36+
cycles = []
37+
for _ in range(n_cycles):
38+
node = Node()
39+
cycles.append(node)
40+
create_cycle(node, n_links)
41+
return cycles
42+
43+
44+
def benchamark_collection(loops, cycles, links):
45+
total_time = 0
46+
for _ in range(loops):
47+
gc.collect()
48+
all_cycles = create_gc_cycles(cycles, links)
49+
50+
# Main loop to measure
51+
del all_cycles
52+
t0 = pyperf.perf_counter()
53+
collected = gc.collect()
54+
total_time += pyperf.perf_counter() - t0
55+
56+
assert collected >= cycles * (links + 1)
57+
58+
return total_time
59+
60+
61+
if __name__ == "__main__":
62+
runner = pyperf.Runner()
63+
runner.metadata["description"] = "GC link benchmark"
64+
runner.bench_time_func("create_gc_cycles", benchamark_collection, CYCLES, LINKS)

0 commit comments

Comments
 (0)
0