This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch maintenance-branch
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 3ab0d5f93bba2f8f02904e2b0a00dc16d2add0a5
Author: Dan Haywood <[email protected]>
AuthorDate: Tue Jun 25 17:34:55 2024 +0200

    CAUSEWAY-3751: adds logging to WebRequestCycleForCauseway
---
 .../viewer/wicket/viewer/integration/Timing.java   | 31 ++++++++++++++++++++++
 .../integration/WebRequestCycleForCauseway.java    | 30 ++++++++++++++++++---
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git 
a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/Timing.java
 
b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/Timing.java
new file mode 100644
index 0000000000..d3317d6ea9
--- /dev/null
+++ 
b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/Timing.java
@@ -0,0 +1,31 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.viewer.wicket.viewer.integration;
+
+class Timing {
+    private final long startTime;
+
+    Timing() {
+        this.startTime = System.currentTimeMillis();
+    }
+
+    long took() {
+        return System.currentTimeMillis() - startTime;
+    }
+}
diff --git 
a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
 
b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
index c0e6e2b7ff..e5ae7b0f6c 100644
--- 
a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
+++ 
b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
@@ -25,6 +25,9 @@ import java.util.Optional;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.causeway.applib.services.metrics.MetricsService;
+import org.apache.causeway.applib.services.publishing.log.Timing;
+
 import org.apache.wicket.Application;
 import org.apache.wicket.IPageFactory;
 import org.apache.wicket.MetaDataKey;
@@ -121,10 +124,14 @@ implements
     @Setter
     private PageClassRegistry pageClassRegistry;
 
+    private static ThreadLocal<Timing> timings = 
ThreadLocal.withInitial(Timing::new);
+
     @Override
     public synchronized void onBeginRequest(final RequestCycle requestCycle) {
 
-        log.debug("onBeginRequest in");
+        if(log.isDebugEnabled()) {
+            log.debug("onBeginRequest in");
+        }
 
         if (!Session.exists()) {
             // Track if session was created from an expired one to notify user 
of the refresh.
@@ -174,7 +181,11 @@ implements
         // Note: this is a no-op if an interactionContext layer was already 
opened and is unchanged.
         interactionService.openInteraction(interactionContext1);
 
-        log.debug("onBeginRequest out - session was opened");
+        if(log.isDebugEnabled()) {
+            timings.set(new Timing());
+            log.debug("onBeginRequest out - session was opened");
+        }
+
     }
 
     @Override
@@ -249,7 +260,15 @@ implements
     @Override
     public synchronized void onEndRequest(final RequestCycle requestCycle) {
 
-        log.debug("onEndRequest");
+        if(log.isDebugEnabled()) {
+            val metricsServiceIfAny = 
getMetaModelContext().lookupService(MetricsService.class);
+            if(metricsServiceIfAny.isPresent()) {
+                val metricsService = metricsServiceIfAny.get();
+                log.debug("onEndRequest  took: {}ms  numberEntitiesLoaded: {}, 
numberEntitiesDirtied: {}", timings.get().took(), 
metricsService.numberEntitiesLoaded(), metricsService.numberEntitiesDirtied());
+            } else {
+                log.debug("onEndRequest  took: {}ms", timings.get().took());
+            }
+        }
 
         
getMetaModelContext().lookupService(InteractionService.class).ifPresent(
             InteractionService::closeInteractionLayers
@@ -267,7 +286,10 @@ implements
     @Override
     public IRequestHandler onException(final RequestCycle cycle, final 
Exception ex) {
 
-        log.debug("onException {}", ex.getClass().getSimpleName());
+        if(log.isDebugEnabled()) {
+            log.debug("onException {}  took: {}ms", 
ex.getClass().getSimpleName(), timings.get().took());
+        }
+
 
         // using side-effect free access to MM validation result
         val validationResult = 
getMetaModelContext().getSpecificationLoader().getValidationResult()

Reply via email to