You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sirona.apache.org by rm...@apache.org on 2013/10/29 09:10:03 UTC

svn commit: r1536600 - in /incubator/sirona/trunk: ./ aop/src/main/java/org/apache/sirona/aop/ reporting/ web/ web/src/main/java/org/apache/sirona/web/registration/ web/src/main/java/org/apache/sirona/web/servlet/ web/src/test/java/org/ web/src/test/ja...

Author: rmannibucau
Date: Tue Oct 29 08:10:02 2013
New Revision: 1536600

URL: http://svn.apache.org/r1536600
Log:
using aop in web module to reuse the existing logic + monitoring http status

Added:
    incubator/sirona/trunk/web/src/test/java/org/
    incubator/sirona/trunk/web/src/test/java/org/apache/
    incubator/sirona/trunk/web/src/test/java/org/apache/sirona/
    incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/
    incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/
    incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/HittableServlet.java
    incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/RequestPerformanceTest.java
Modified:
    incubator/sirona/trunk/aop/src/main/java/org/apache/sirona/aop/AbstractPerformanceInterceptor.java
    incubator/sirona/trunk/pom.xml
    incubator/sirona/trunk/reporting/pom.xml
    incubator/sirona/trunk/web/pom.xml
    incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/registration/WebMonitoringInitializer.java
    incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/servlet/MonitoringFilter.java

Modified: incubator/sirona/trunk/aop/src/main/java/org/apache/sirona/aop/AbstractPerformanceInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/aop/src/main/java/org/apache/sirona/aop/AbstractPerformanceInterceptor.java?rev=1536600&r1=1536599&r2=1536600&view=diff
==============================================================================
--- incubator/sirona/trunk/aop/src/main/java/org/apache/sirona/aop/AbstractPerformanceInterceptor.java (original)
+++ incubator/sirona/trunk/aop/src/main/java/org/apache/sirona/aop/AbstractPerformanceInterceptor.java Tue Oct 29 08:10:02 2013
@@ -82,7 +82,7 @@ public abstract class AbstractPerformanc
         final ActivationContext context = doFindContext(invocation);
 
         final StopWatch stopwatch;
-        if (context.isActive() || context.isForcedIteration()) {
+        if (context.shouldExecute()) {
             final Counter monitor = Repository.INSTANCE.getCounter(new Counter.Key(getRole(), name));
             stopwatch = Repository.INSTANCE.start(monitor);
         } else {
@@ -107,9 +107,7 @@ public abstract class AbstractPerformanc
                     Repository.INSTANCE.getCounter(new Counter.Key(Role.FAILURES, writer.toString())).add(elapsedTime);
                 }
 
-                if (context.isThresholdActive() && elapsedTime < context.getThreshold()) {
-                    context.reset();
-                }
+                context.elapsedTime(elapsedTime);
             }
         }
     }
@@ -290,5 +288,15 @@ public abstract class AbstractPerformanc
             active = false;
             iteration.set(0);
         }
+
+        public boolean shouldExecute() {
+            return isActive() || isForcedIteration();
+        }
+
+        public void elapsedTime(final long elapsedTime) {
+            if (isThresholdActive() && elapsedTime < getThreshold()) {
+                reset();
+            }
+        }
     }
 }
\ No newline at end of file

Modified: incubator/sirona/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/pom.xml?rev=1536600&r1=1536599&r2=1536600&view=diff
==============================================================================
--- incubator/sirona/trunk/pom.xml (original)
+++ incubator/sirona/trunk/pom.xml Tue Oct 29 08:10:02 2013
@@ -51,6 +51,8 @@
     <maven.compiler.source>1.6</maven.compiler.source>
     <maven.compiler.target>1.6</maven.compiler.target>
 
+    <tomcat.version>7.0.47</tomcat.version>
+    <arquillian.version>1.0.3.Final</arquillian.version>
   </properties>
 
   <modules>
@@ -309,6 +311,68 @@
         <version>4.0.11.Final</version>
         <scope>test</scope>
       </dependency>
+
+      <dependency>
+        <groupId>org.jboss.shrinkwrap.descriptors</groupId>
+        <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
+        <version>2.0.0-alpha-5</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.jboss.shrinkwrap.resolver</groupId>
+        <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
+        <version>2.0.0</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.jboss.arquillian.junit</groupId>
+        <artifactId>arquillian-junit-container</artifactId>
+        <version>${arquillian.version}</version>
+        <!-- sadly tomcat7 embedded is not compatible with later impl in v1.0.0.CR5 -->
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.jboss.arquillian.protocol</groupId>
+        <artifactId>arquillian-protocol-servlet</artifactId>
+        <version>${arquillian.version}</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.jboss.arquillian.container</groupId>
+        <artifactId>arquillian-tomcat-embedded-7</artifactId>
+        <version>1.0.0.CR5</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.tomcat.embed</groupId>
+        <artifactId>tomcat-embed-core</artifactId>
+        <version>${tomcat.version}</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.tomcat.embed</groupId>
+        <artifactId>tomcat-embed-jasper</artifactId>
+        <version>${tomcat.version}</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.tomcat.embed</groupId>
+        <artifactId>tomcat-embed-logging-juli</artifactId>
+        <version>${tomcat.version}</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.eclipse.jdt.core.compiler</groupId>
+        <artifactId>ecj</artifactId>
+        <version>4.2.1</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>net.sourceforge.htmlunit</groupId>
+        <artifactId>htmlunit</artifactId>
+        <version>2.12</version>
+        <scope>test</scope>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 

Modified: incubator/sirona/trunk/reporting/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/reporting/pom.xml?rev=1536600&r1=1536599&r2=1536600&view=diff
==============================================================================
--- incubator/sirona/trunk/reporting/pom.xml (original)
+++ incubator/sirona/trunk/reporting/pom.xml Tue Oct 29 08:10:02 2013
@@ -32,9 +32,6 @@
   <properties>
     <tomcatRunPort>8080</tomcatRunPort>
     <tomcatRunPath>/monitoring</tomcatRunPath>
-
-    <tomcat.version>7.0.42</tomcat.version>
-    <arquillian.version>1.0.3.Final</arquillian.version>
   </properties>
 
   <dependencies>
@@ -75,63 +72,42 @@
     <dependency>
       <groupId>org.jboss.shrinkwrap.descriptors</groupId>
       <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
-      <version>2.0.0-alpha-5</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.jboss.shrinkwrap.resolver</groupId>
       <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
-      <version>2.0.0</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.jboss.arquillian.junit</groupId>
       <artifactId>arquillian-junit-container</artifactId>
-      <version>${arquillian.version}</version>
-      <!-- sadly tomcat7 embedded is not compatible with later impl in v1.0.0.CR5 -->
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.jboss.arquillian.protocol</groupId>
       <artifactId>arquillian-protocol-servlet</artifactId>
-      <version>${arquillian.version}</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.jboss.arquillian.container</groupId>
       <artifactId>arquillian-tomcat-embedded-7</artifactId>
-      <version>1.0.0.CR5</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-core</artifactId>
-      <version>${tomcat.version}</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-jasper</artifactId>
-      <version>${tomcat.version}</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-logging-juli</artifactId>
-      <version>${tomcat.version}</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.eclipse.jdt.core.compiler</groupId>
       <artifactId>ecj</artifactId>
-      <version>4.2.1</version>
-      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>net.sourceforge.htmlunit</groupId>
       <artifactId>htmlunit</artifactId>
-      <version>2.12</version>
-      <scope>test</scope>
     </dependency>
   </dependencies>
 

Modified: incubator/sirona/trunk/web/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/web/pom.xml?rev=1536600&r1=1536599&r2=1536600&view=diff
==============================================================================
--- incubator/sirona/trunk/web/pom.xml (original)
+++ incubator/sirona/trunk/web/pom.xml Tue Oct 29 08:10:02 2013
@@ -45,6 +45,55 @@
       <groupId>org.apache.sirona</groupId>
       <artifactId>sirona-core</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sirona</groupId>
+      <artifactId>sirona-aop</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.shrinkwrap.descriptors</groupId>
+      <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.shrinkwrap.resolver</groupId>
+      <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.arquillian.junit</groupId>
+      <artifactId>arquillian-junit-container</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.arquillian.protocol</groupId>
+      <artifactId>arquillian-protocol-servlet</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.arquillian.container</groupId>
+      <artifactId>arquillian-tomcat-embedded-7</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomcat.embed</groupId>
+      <artifactId>tomcat-embed-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomcat.embed</groupId>
+      <artifactId>tomcat-embed-jasper</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomcat.embed</groupId>
+      <artifactId>tomcat-embed-logging-juli</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.jdt.core.compiler</groupId>
+      <artifactId>ecj</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>net.sourceforge.htmlunit</groupId>
+      <artifactId>htmlunit</artifactId>
+    </dependency>
   </dependencies>
 
 </project>

Modified: incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/registration/WebMonitoringInitializer.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/registration/WebMonitoringInitializer.java?rev=1536600&r1=1536599&r2=1536600&view=diff
==============================================================================
--- incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/registration/WebMonitoringInitializer.java (original)
+++ incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/registration/WebMonitoringInitializer.java Tue Oct 29 08:10:02 2013
@@ -17,11 +17,14 @@
 package org.apache.sirona.web.registration;
 
 import org.apache.sirona.configuration.Configuration;
+import org.apache.sirona.repositories.Repository;
 import org.apache.sirona.web.discovery.GaugeDiscoveryListener;
+import org.apache.sirona.web.lifecycle.SironaLifecycle;
 import org.apache.sirona.web.servlet.MonitoringFilter;
 import org.apache.sirona.web.session.MonitoringSessionListener;
 
 import javax.servlet.DispatcherType;
+import javax.servlet.FilterRegistration;
 import javax.servlet.ServletContainerInitializer;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -38,18 +41,28 @@ public class WebMonitoringInitializer im
 
         ctx.addListener(MonitoringSessionListener.class);
         ctx.addListener(GaugeDiscoveryListener.class);
+        if (ctx.getClassLoader().equals(Repository.class.getClassLoader())) {
+            ctx.addListener(SironaLifecycle.class);
+        }
 
+        final String monStatus = Boolean.toString(!"false".equalsIgnoreCase(ctx.getInitParameter(MonitoringFilter.MONITOR_STATUS)));
         String monitoredUrls = ctx.getInitParameter(Configuration.CONFIG_PROPERTY_PREFIX + "web.monitored-urls");
-        if (monitoredUrls == null) {
-            monitoredUrls = "/*";
-        }
-        if (monitoredUrls.contains(",")) {
-            final String[] split = monitoredUrls.split(",");
-            for (int i = 0; i < split.length; i++) {
-                ctx.addFilter("monitoring-filter-" + i, MonitoringFilter.class).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, split[i]);
+        if (!"false".equalsIgnoreCase(monitoredUrls)) {
+            if (monitoredUrls == null) {
+                monitoredUrls = "/*";
+            }
+            if (monitoredUrls.contains(",")) {
+                final String[] split = monitoredUrls.split(",");
+                for (int i = 0; i < split.length; i++) {
+                    final FilterRegistration.Dynamic filter = ctx.addFilter("monitoring-filter-" + i, MonitoringFilter.class);
+                    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, split[i]);
+                    filter.setInitParameter(MonitoringFilter.MONITOR_STATUS, monStatus);
+                }
+            } else {
+                final FilterRegistration.Dynamic filter = ctx.addFilter("monitoring-filter", MonitoringFilter.class);
+                filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, monitoredUrls);
+                filter.setInitParameter(MonitoringFilter.MONITOR_STATUS, monStatus);
             }
-        } else {
-            ctx.addFilter("monitoring-filter", MonitoringFilter.class).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, monitoredUrls);
         }
     }
 }

Modified: incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/servlet/MonitoringFilter.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/servlet/MonitoringFilter.java?rev=1536600&r1=1536599&r2=1536600&view=diff
==============================================================================
--- incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/servlet/MonitoringFilter.java (original)
+++ incubator/sirona/trunk/web/src/main/java/org/apache/sirona/web/servlet/MonitoringFilter.java Tue Oct 29 08:10:02 2013
@@ -17,6 +17,8 @@
 package org.apache.sirona.web.servlet;
 
 import org.apache.sirona.Role;
+import org.apache.sirona.aop.AbstractPerformanceInterceptor;
+import org.apache.sirona.configuration.Configuration;
 import org.apache.sirona.counters.Counter;
 import org.apache.sirona.repositories.Repository;
 import org.apache.sirona.stopwatches.StopWatch;
@@ -30,11 +32,37 @@ import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.net.HttpURLConnection;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+public class MonitoringFilter extends AbstractPerformanceInterceptor<MonitoringFilter.Invocation> implements Filter {
+    public static final String MONITOR_STATUS = Configuration.CONFIG_PROPERTY_PREFIX + "web.monitored-status";
+
+    private static final ConcurrentMap<Integer, Counter.Key> STATUS_KEYS = new ConcurrentHashMap<Integer, Counter.Key>();
+
+    private boolean monitorStatus;
 
-public class MonitoringFilter implements Filter {
     @Override
     public void init(final FilterConfig filterConfig) throws ServletException {
-        // no-op
+        final String monStatus = filterConfig.getInitParameter(MONITOR_STATUS);
+        monitorStatus = monStatus == null || "true".equalsIgnoreCase(monStatus);
+
+        for (final Field f : HttpURLConnection.class.getDeclaredFields()) {
+            final int modifiers = f.getModifiers();
+            if (f.getName().startsWith("HTTP_")
+                && Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) && Modifier.isFinal(modifiers)) {
+                try {
+                    final int status = (Integer) f.get(null);
+                    STATUS_KEYS.put(status, statusKey((Integer) f.get(null)));
+                } catch (final IllegalAccessException e) {
+                    // no-op
+                }
+            }
+        }
     }
 
     @Override
@@ -42,7 +70,22 @@ public class MonitoringFilter implements
         if (HttpServletRequest.class.isInstance(request)) {
             final HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
             final HttpServletResponse httpResponse = HttpServletResponse.class.cast(response);
-            doFilter(httpRequest, httpResponse, chain);
+            try {
+                doInvoke(new Invocation(httpRequest, httpResponse, chain));
+            } catch (final Throwable throwable) {
+                if (IOException.class.isInstance(throwable)) {
+                    throw IOException.class.cast(throwable);
+                }
+                if (ServletException.class.isInstance(throwable)) {
+                    throw ServletException.class.cast(throwable);
+                }
+                throw new IOException(throwable);
+            } finally {
+                if (monitorStatus) {
+                    final int status = httpResponse.getStatus();
+                    Repository.INSTANCE.getCounter(statusKey(status)).add(1);
+                }
+            }
         } else {
             // Not an HTTP request...
             chain.doFilter(request, response);
@@ -50,26 +93,53 @@ public class MonitoringFilter implements
     }
 
     @Override
+    protected Object proceed(final Invocation invocation) throws Throwable {
+        invocation.proceed();
+        return null;
+    }
+
+    @Override
+    protected String getCounterName(final Invocation invocation) {
+        return invocation.request.getRequestURI();
+    }
+
+    @Override
+    protected Role getRole() {
+        return Role.WEB;
+    }
+
+    @Override
     public void destroy() {
         // no-op
     }
 
-    protected void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
-        final String uri = getRequestedUri(request);
-        final StopWatch stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getCounter(new Counter.Key(Role.WEB, uri)));
-        try {
-            chain.doFilter(request, response);
-        } finally {
-            stopWatch.stop();
+    private static Counter.Key statusKey(final int status) {
+        final Counter.Key key = STATUS_KEYS.get(status);
+        if (key != null) {
+            return key;
         }
+
+        final Counter.Key newKey = new Counter.Key(Role.WEB, "HTTP-" + Integer.toString(status));
+        final Counter.Key old = STATUS_KEYS.putIfAbsent(status, newKey);
+        if (old != null) {
+            return old;
+        }
+        return newKey;
     }
 
-    protected String getRequestedUri(final HttpServletRequest request) {
-        return request.getRequestURI();
-        /* if we want to remove webapp context we could do (but a bad idea in an app server since you can get multiple contexts):
-        final String uri = request.getRequestURI();
-        final String context = request.getContextPath();
-        return uri.substring(context.length());
-        */
+    protected static class Invocation {
+        protected final HttpServletRequest request;
+        protected final HttpServletResponse response;
+        protected final FilterChain chain;
+
+        public Invocation(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
+            this.request = request;
+            this.response = response;
+            this.chain = chain;
+        }
+
+        public void proceed() throws IOException, ServletException {
+            chain.doFilter(request, response);
+        }
     }
 }
\ No newline at end of file

Added: incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/HittableServlet.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/HittableServlet.java?rev=1536600&view=auto
==============================================================================
--- incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/HittableServlet.java (added)
+++ incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/HittableServlet.java Tue Oct 29 08:10:02 2013
@@ -0,0 +1,42 @@
+/*
+ * 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.sirona.test.web;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+
+@WebServlet(urlPatterns = "/hit")
+public class HittableServlet extends HttpServlet {
+    @Override
+    protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
+        final String statusStr = req.getParameter("status");
+        final int status;
+        if (statusStr != null) {
+            status = Integer.parseInt(statusStr);
+        } else {
+            status = HttpURLConnection.HTTP_OK;
+        }
+
+        resp.setStatus(status);
+        resp.getWriter().write("answered");
+    }
+}

Added: incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/RequestPerformanceTest.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/RequestPerformanceTest.java?rev=1536600&view=auto
==============================================================================
--- incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/RequestPerformanceTest.java (added)
+++ incubator/sirona/trunk/web/src/test/java/org/apache/sirona/test/web/RequestPerformanceTest.java Tue Oct 29 08:10:02 2013
@@ -0,0 +1,103 @@
+/*
+ * 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.sirona.test.web;
+
+import com.gargoylesoftware.htmlunit.TextPage;
+import com.gargoylesoftware.htmlunit.WebAssert;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import org.apache.catalina.startup.Constants;
+import org.apache.sirona.Role;
+import org.apache.sirona.counters.Counter;
+import org.apache.sirona.repositories.Repository;
+import org.apache.sirona.web.registration.WebMonitoringInitializer;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.impl.base.io.IOUtil;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.servlet.ServletContainerInitializer;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(Arquillian.class)
+public class RequestPerformanceTest {
+    static { // to start faster
+        System.setProperty(Constants.DEFAULT_JARS_TO_SKIP, "a*,c*,d*,e*,g*,h*,i*,j*,l*,m*,n*,p*,r*,sa*,se*,sh*,su*,t*,v*,w*,x*,z*");
+    }
+
+    @Deployment(testable = false)
+    public static Archive<?> war() { // note: we depend on tomcat embedded adapter since we don't add dependencies + we use the fact Repository.INSTANCE is in the same JVM
+        return ShrinkWrap.create(WebArchive.class, "sirona-test.war")
+            .addPackages(true, "org.apache.sirona.web")
+            .addClasses(HittableServlet.class)
+            .addAsLibraries(
+                ShrinkWrap.create(JavaArchive.class, "sci.jar")
+                    .addAsServiceProvider(ServletContainerInitializer.class, WebMonitoringInitializer.class));
+    }
+
+    @ArquillianResource
+    private URL base;
+
+    @Before
+    public void resetCounters() {
+        Repository.INSTANCE.clear();
+    }
+
+    @Test
+    public void monitorRequest() throws IOException {
+        final TextPage page = newClient().getPage(base.toExternalForm() + "hit");
+        assertEquals(HttpURLConnection.HTTP_OK, page.getWebResponse().getStatusCode());
+
+        final Counter hitCounter = Repository.INSTANCE.getCounter(new Counter.Key(Role.WEB, "/sirona-test/hit"));
+        assertEquals(1, hitCounter.getHits());
+    }
+
+    @Test
+    public void monitorStatus200() throws IOException {
+        final TextPage page = newClient().getPage(base.toExternalForm() + "hit");
+        assertEquals(HttpURLConnection.HTTP_OK, page.getWebResponse().getStatusCode());
+        assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.WEB, "HTTP-200")).getHits());
+    }
+
+    @Test
+    public void monitorStatus4567() throws IOException {
+        final TextPage page = newClient().getPage(base.toExternalForm() + "hit?status=4567");
+        assertEquals(4567, page.getWebResponse().getStatusCode());
+        assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.WEB, "HTTP-4567")).getHits());
+    }
+
+    private static WebClient newClient() {
+        final WebClient webClient = new WebClient();
+        webClient.getOptions().setJavaScriptEnabled(false);
+        webClient.getOptions().setCssEnabled(false);
+        webClient.getOptions().setAppletEnabled(false);
+        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
+        return webClient;
+    }
+}