Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add benchmark to measure gc collection of a big chain of cycles #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "pyperformance_bm_gc_collect"
requires-python = ">=3.8"
dependencies = ["pyperf"]
urls = {repository = "https://github.com/python/pyperformance"}
dynamic = ["version"]

[tool.pyperformance]
name = "gc_collect"
64 changes: 64 additions & 0 deletions pyperformance/data-files/benchmarks/bm_gc_collect/run_benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import pyperf
import gc

CYCLES = 100
LINKS = 20


class Node:
def __init__(self):
self.next = None
self.prev = None

def link_next(self, next):
self.next = next
self.next.prev = self


def create_cycle(node, n_links):
"""Create a cycle of n_links nodes, starting with node."""

if n_links == 0:
return

current = node
for i in range(n_links):
next_node = Node()
current.link_next(next_node)
current = next_node

current.link_next(node)


def create_gc_cycles(n_cycles, n_links):
"""Create n_cycles cycles n_links+1 nodes each."""

cycles = []
for _ in range(n_cycles):
node = Node()
cycles.append(node)
create_cycle(node, n_links)
return cycles


def benchamark_collection(loops, cycles, links):
total_time = 0
for _ in range(loops):
gc.collect()
all_cycles = create_gc_cycles(cycles, links)

# Main loop to measure
del all_cycles
t0 = pyperf.perf_counter()
collected = gc.collect()
total_time += pyperf.perf_counter() - t0

assert collected >= cycles * (links + 1)

return total_time


if __name__ == "__main__":
runner = pyperf.Runner()
runner.metadata["description"] = "GC link benchmark"
runner.bench_time_func("create_gc_cycles", benchamark_collection, CYCLES, LINKS)