From 5b311986e47d580fb149085be2bb524952ead319 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 16 Nov 2025 00:45:22 +0100 Subject: speed up track insertion It turns out that adding multiple thousands of track points via the ORM is quite slow, which is especially noticable in the browse_paged test (as 50 tracks are added there). This provides the fast_set_path method, which goes directly to the database, utilizing the executemany() capability. On the CI, the gpx-influx decreased test runtime from ~4min to ~18min. This change should make it much faster again. --- tests/integration/test_browse.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'tests/integration/test_browse.py') diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py index 4b38ddf..1994186 100644 --- a/tests/integration/test_browse.py +++ b/tests/integration/test_browse.py @@ -3,6 +3,8 @@ import zipfile from contextlib import contextmanager from datetime import datetime +from sqlalchemy import delete, inspect, text + from testutils import load_gpx_asset from fietsboek import convert, models from fietsboek.models.track import Visibility @@ -87,6 +89,7 @@ def a_lot_of_tracks(tm, dbsession, owner, data_manager): gpx_data = load_gpx_asset("MyTourbook_1.gpx.gz") skel = convert.smart_convert(gpx_data) + path = skel.path() tracks = [] track_ids = [] @@ -102,12 +105,12 @@ def a_lot_of_tracks(tm, dbsession, owner, data_manager): tagged_people=[], ) track.date = datetime(2022 - index, 3, 14, 9, 26, 59) - track.set_path(skel.path()) dbsession.add(track) dbsession.flush() - data_manager.initialize(track.id) + track.fast_set_path(path) tracks.append(track) track_ids.append(track.id) + data_manager.initialize(track.id) tm.begin() tm.doom() @@ -116,9 +119,11 @@ def a_lot_of_tracks(tm, dbsession, owner, data_manager): yield track_ids finally: tm.abort() + table = inspect(models.track.TrackPoint).tables[0] with tm: - for track in tracks: - dbsession.delete(track) + for track_id in track_ids: + dbsession.execute(delete(table).where(table.c.track_id == track_id)) + dbsession.execute(delete(models.Track).where(models.Track.id == track_id)) tm.begin() tm.doom() @@ -128,7 +133,7 @@ def test_browse(testapp, dbsession, route_path, logged_in, tm, data_manager): # Ensure there are some tracks in the database with added_tracks(tm, dbsession, logged_in, data_manager): # Now go to the browse page - browse = testapp.get(route_path('browse')) + browse = testapp.get(route_path("browse")) assert "Foobar" in browse.text assert "Barfoo" in browse.text @@ -164,13 +169,13 @@ def test_archive(testapp, dbsession, route_path, logged_in, tm, data_manager): with added_tracks(tm, dbsession, logged_in, data_manager) as tracks: archive = testapp.get( route_path( - 'track-archive', + "track-archive", _query=[("track_id[]", tracks[0]), ("track_id[]", tracks[1])], ) ) result = io.BytesIO(archive.body) - with zipfile.ZipFile(result, 'r') as zipped: + with zipfile.ZipFile(result, "r") as zipped: assert len(zipped.namelist()) == 2 assert f"track_{tracks[0]}.gpx" in zipped.namelist() assert f"track_{tracks[1]}.gpx" in zipped.namelist() -- cgit v1.2.3