diff --git a/src/moin/app.py b/src/moin/app.py index af9dd11e6..5ca0f420e 100644 --- a/src/moin/app.py +++ b/src/moin/app.py @@ -13,6 +13,7 @@ import os from os import path +import re import sys from flask import Flask, request, session @@ -292,6 +293,10 @@ def before_wiki(): """ Setup environment for wiki requests, start timers. """ + if request and is_static_content(request.path): + logging.debug(f"skipping before_wiki for {request.path}") + return + logging.debug("running before_wiki") flaskg.clock = Clock() flaskg.clock.start("total") @@ -323,19 +328,22 @@ def teardown_wiki(response): """ Teardown environment of wiki requests, stop timers. """ + if request: + request_path = request.path + if is_static_content(request_path): + return response + else: + request_path = "" + logging.debug("running teardown_wiki") - try: - flaskg.clock.stop("total") - del flaskg.clock - except AttributeError: - # can happen if teardown_wiki() is called twice, e.g. by unit tests. - pass + if hasattr(flaskg, "edit_utils"): # if transaction fails with sql file locked, we try to free it here try: flaskg.edit_utils.conn.close() except AttributeError: pass + try: # whoosh cache performance for cache in ( @@ -353,4 +361,22 @@ def teardown_wiki(response): # moin commands may not have flaskg.storage pass + try: + flaskg.clock.stop("total", comment=request_path) + del flaskg.clock + except AttributeError: + # can happen if teardown_wiki() is called twice, e.g. by unit tests. + pass + return response + + +def is_static_content(request_path): + """ + Check if content is static and does not need usual wiki handling + """ + + if request_path.startswith(("/static/", "/+serve/", "/+template/")) or re.match(r"/_themes/\w+/css/", request_path): + return True + else: + return False diff --git a/src/moin/utils/clock.py b/src/moin/utils/clock.py index 1db538f0c..2b687778a 100644 --- a/src/moin/utils/clock.py +++ b/src/moin/utils/clock.py @@ -1,5 +1,6 @@ # Copyright: 2001-2003 Juergen Hermann # Copyright: 2003-2006 MoinMoin:ThomasWaldmann +# Copyright: 2024 MoinMoin:UlrichB # License: GNU GPL v2 (or any later version), see LICENSE.txt for details. """ @@ -22,11 +23,12 @@ class Clock: Helper class for measuring the time needed to run code. Usage: - flaskg.clock.start('mytimer') + flaskg.clock.start("mytimer") # do something - flaskg.clock.stop('mytimer') + flaskg.clock.stop("mytimer", comment="add this to the log message") + # the optional comment field is added to the log message # or if you want to use its value later - timerval = flaskg.clock.stop('mytimer') + timerval = flaskg.clock.stop("mytimer") Starting a timer multiple times is supported but the one started last has to be stopped first. @@ -40,10 +42,10 @@ def start(self, timer): self.timers[timer] = [] self.timers[timer].append(time.time()) - def stop(self, timer): + def stop(self, timer, comment=""): if timer in self.timers: value = time.time() - self.timers[timer].pop() - logging.debug(f"timer {timer}({len(self.timers[timer])}): {value * 1000:.2f}ms") + logging.debug(f"timer {timer}({len(self.timers[timer])}): {value * 1000:.2f}ms {comment}") if not self.timers[timer]: del self.timers[timer] return value