aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2025-11-16 12:07:34 +0100
committerDaniel Schadt <kingdread@gmx.de>2025-11-16 12:07:34 +0100
commite72efd61c391b2fac08e835908cfa3b82c8f7fe1 (patch)
treeac8ee7355bd0c50cf7581a4757bd5c2f698a2343
parent41c6b4f55ad80751a7498ab06ee7191997d5ad23 (diff)
downloadfietsboek-e72efd61c391b2fac08e835908cfa3b82c8f7fe1.tar.gz
fietsboek-e72efd61c391b2fac08e835908cfa3b82c8f7fe1.tar.bz2
fietsboek-e72efd61c391b2fac08e835908cfa3b82c8f7fe1.zip
further speed up tests
A lot of time in test_browse was spent retrieving path points and computing metadata. Why not save it while we still have the path ready?
-rw-r--r--fietsboek/actions.py4
-rw-r--r--fietsboek/models/track.py16
-rw-r--r--tests/integration/test_browse.py8
3 files changed, 22 insertions, 6 deletions
diff --git a/fietsboek/actions.py b/fietsboek/actions.py
index bb6cafd..2d23d97 100644
--- a/fietsboek/actions.py
+++ b/fietsboek/actions.py
@@ -108,11 +108,11 @@ def add_track(
# Best time to build the cache is right after the upload, but *after* the
# transformers have been applied!
- track.ensure_cache()
+ track.ensure_cache(path)
dbsession.add(track.cache)
LOGGER.debug("Building preview image for %s", track.id)
- preview_image = trackmap.render(track.path(), layer, tile_requester)
+ preview_image = trackmap.render(path, layer, tile_requester)
image_io = io.BytesIO()
preview_image.save(image_io, "PNG")
manager.set_preview(image_io.getvalue())
diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py
index ab9d698..3bf7eee 100644
--- a/fietsboek/models/track.py
+++ b/fietsboek/models/track.py
@@ -522,12 +522,22 @@ class Track(Base):
result = ACLHelper().permits(self, principals, "track.view")
return isinstance(result, ACLAllowed)
- def ensure_cache(self):
- """Ensure that a cached version of this track's metadata exists."""
+ def ensure_cache(self, path: geo.Path | None = None):
+ """Ensure that a cached version of this track's metadata exists.
+
+ If ``path`` is given, it is used as the basis for the calculation. This
+ is useful if you have already loaded the path, to avoid a database
+ access.
+
+ :param path: The path, if preloaded. Otherwise, ``self.path()`` will be
+ used.
+ """
if self.cache is not None:
return
self.cache = TrackCache()
- meta = self.path().movement_data()
+ if path is None:
+ path = self.path()
+ meta = path.movement_data()
self.cache.length = meta.length
self.cache.uphill = meta.uphill
self.cache.downhill = meta.downhill
diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py
index 68ca1e7..c48ea0e 100644
--- a/tests/integration/test_browse.py
+++ b/tests/integration/test_browse.py
@@ -23,6 +23,8 @@ def added_tracks(tm, dbsession, owner, data_manager):
# objects to the database.
tm.abort()
+ path = convert.smart_convert(load_gpx_asset("Teasi_1.gpx.gz")).path()
+
tracks = []
track_ids = []
with tm:
@@ -53,9 +55,11 @@ def added_tracks(tm, dbsession, owner, data_manager):
tagged_people=[],
)
track.date = datetime(2022, 10, 29, 13, 37, 11)
- track.set_path(convert.smart_convert(load_gpx_asset("Teasi_1.gpx.gz")).path())
dbsession.add(track)
dbsession.flush()
+ track.fast_set_path(path)
+ track.ensure_cache(path)
+ dbsession.add(track.cache)
data_manager.initialize(track.id)
tracks.append(track)
track_ids.append(track.id)
@@ -108,6 +112,8 @@ def a_lot_of_tracks(tm, dbsession, owner, data_manager):
dbsession.add(track)
dbsession.flush()
track.fast_set_path(path)
+ track.ensure_cache(path)
+ dbsession.add(track.cache)
tracks.append(track)
track_ids.append(track.id)
data_manager.initialize(track.id)