From 8dc18f91ea96e3493c6ad80e4aae3dd7beb61e15 Mon Sep 17 00:00:00 2001 From: mattip Date: Sun, 10 May 2026 06:47:03 +0300 Subject: [PATCH 1/2] add db cleanup for PyPy --- .../benchmarks/bm_sqlalchemy_declarative/run_benchmark.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py index a977775a..34f82ce7 100644 --- a/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py @@ -62,6 +62,7 @@ def bench_sqlalchemy(loops, npeople): # drop rows created by the previous benchmark session.query(Person).delete(synchronize_session=False) session.query(Address).delete(synchronize_session=False) + session.expunge_all() # Run the benchmark once t0 = pyperf.perf_counter() From dedaa04ea365f7d3f4878091578e87ba5e3fdab4 Mon Sep 17 00:00:00 2001 From: mattip Date: Sun, 10 May 2026 17:25:23 +0300 Subject: [PATCH 2/2] do session resetting differently, from review --- .../run_benchmark.py | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py index 34f82ce7..db79708d 100644 --- a/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_sqlalchemy_declarative/run_benchmark.py @@ -50,7 +50,6 @@ class Address(Base): # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() -session = DBSession() # add 'npeople' people to the database @@ -59,30 +58,30 @@ def bench_sqlalchemy(loops, npeople): total_dt = 0.0 for loops in range(loops): + with DBSession() as session: # drop rows created by the previous benchmark - session.query(Person).delete(synchronize_session=False) - session.query(Address).delete(synchronize_session=False) - session.expunge_all() + session.query(Person).delete(synchronize_session=False) + session.query(Address).delete(synchronize_session=False) - # Run the benchmark once - t0 = pyperf.perf_counter() + # Run the benchmark once + t0 = pyperf.perf_counter() - for i in range(npeople): - # Insert a Person in the person table - new_person = Person(name="name %i" % i) - session.add(new_person) - session.commit() + for i in range(npeople): + # Insert a Person in the person table + new_person = Person(name="name %i" % i) + session.add(new_person) + session.commit() - # Insert an Address in the address table - new_address = Address(post_code='%05i' % i, person=new_person) - session.add(new_address) - session.commit() + # Insert an Address in the address table + new_address = Address(post_code='%05i' % i, person=new_person) + session.add(new_address) + session.commit() - # do 100 queries per insert - for i in range(npeople): - session.query(Person).all() + # do 100 queries per insert + for i in range(npeople): + session.query(Person).all() - total_dt += (pyperf.perf_counter() - t0) + total_dt += (pyperf.perf_counter() - t0) return total_dt