Add measurement of execution time for servlets
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Tue, 24 Sep 2013 14:10:38 +0000 (15:10 +0100)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Tue, 24 Sep 2013 14:10:38 +0000 (15:10 +0100)
statpages/MonthlySummary.jsp
statpages/ServicesStatus.jsp
webservices/compbio/stat/collector/StatDB.java
webservices/compbio/stat/servlet/AnnualStat.java
webservices/compbio/stat/servlet/ServiceStatus.java

index ec06abf..ec99204 100644 (file)
@@ -13,7 +13,15 @@ Date: May 2011
 \r
 <div style="margin: 20px ">  \r
 \r
-<h1>JABAWS Usage Statistics</h1>\r
+<h1>JABAWS Usage Statistics. Time of execusion: ${timeexec} msec</h1>\r
+\r
+<ul style="font-weight: bold;">\r
+       <li>Server: <a href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}">\r
+       ${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}\r
+       </a> </li>\r
+       <li>Time of execusion: ${timeexec} msec</li>\r
+</ul>\r
+\r
 <table class="center its" style="width: 600px "> \r
 <thead>\r
 <tr>\r
index b3b60f0..bc1d873 100644 (file)
@@ -18,12 +18,13 @@ TODO refactor
 <h2 style="text-align: center;">JABAWS Services Status</h2>\r
 \r
 <ul style="font-weight: bold;">\r
-  <li>All the services are tested while this page is being loaded</li>\r
-  <li>If you want to test the services again, just reload this page.</li>\r
-  <li>Click on the service status to see the results of the testing.</li>\r
-  <li>Server tested: <a href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}">\r
+       <li>All the services are tested while this page is being loaded</li>\r
+       <li>If you want to test the services again, just reload this page.</li>\r
+       <li>Click on the service status to see the results of the testing.</li>\r
+       <li>Server tested: <a href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}">\r
        ${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}\r
        </a> </li>\r
+       <li>Time of execusion: ${timeexec} msec</li>\r
 </ul>\r
 \r
 <br/>\r
index 3af1b14..32b7fde 100644 (file)
@@ -67,11 +67,6 @@ public class StatDB {
                                conn = DriverManager.getConnection(protocol + statDBName + ";create=false");\r
 \r
                                conn.setAutoCommit(true);\r
-                               /*\r
-                                * Runtime.getRuntime().addShutdownHook(new Thread() {\r
-                                * \r
-                                * @Override public void run() { shutdownDBServer(); } });\r
-                                */\r
                        } catch (ClassNotFoundException e) {\r
                                log.error(e.getMessage(), e);\r
                        }\r
@@ -111,11 +106,8 @@ public class StatDB {
         * @throws SQLException\r
         */\r
        private void createStatTable() throws SQLException {\r
-\r
-               /*\r
-                * Creating a statement object that we can use for running various SQL\r
-                * statements commands against the database.\r
-                */\r
+               // Creating a statement object that we can use for running various SQL\r
+               // statements commands against the database.\r
                Statement s = conn.createStatement();\r
                String create = "create table exec_stat("\r
                                + "number INT GENERATED ALWAYS AS IDENTITY,"\r
@@ -199,7 +191,6 @@ public class StatDB {
                if (exist) {\r
                        date = res.getDate(1);\r
                }\r
-\r
                res.close();\r
                st.close();\r
                return date;\r
index 3ec55ce..26150de 100644 (file)
@@ -40,6 +40,7 @@ public class AnnualStat extends HttpServlet {
                        throws ServletException, IOException {\r
 \r
                try {\r
+                       long startTime = System.nanoTime();\r
                        StatDB db = new StatDB();\r
                        Date earliestRec = db.getEarliestRecord();\r
                        if (earliestRec == null) {\r
@@ -50,13 +51,13 @@ public class AnnualStat extends HttpServlet {
                                writer.close();\r
                                return;\r
                        }\r
-                       Map<Date, Totals> monthlyTotals = StatCollection\r
-                                       .getStats(earliestRec);\r
+                       Map<Date, Totals> monthlyTotals = StatCollection.getStats(earliestRec);\r
+                       long endTime = System.nanoTime();\r
                        req.setAttribute("stat", monthlyTotals);\r
                        req.setAttribute("total", Totals.sumOfTotals(monthlyTotals));\r
+                       req.setAttribute("timeexec", (endTime - startTime) / 1000000);\r
 \r
-                       RequestDispatcher dispatcher = req\r
-                                       .getRequestDispatcher("statpages/MonthlySummary.jsp");\r
+                       RequestDispatcher dispatcher = req.getRequestDispatcher("statpages/MonthlySummary.jsp");\r
 \r
                        req.setAttribute("isAdmin", isAdmin(req));\r
                        dispatcher.forward(req, resp);\r
index c31f15d..a0f5516 100644 (file)
@@ -5,15 +5,18 @@ import java.io.PrintWriter;
 import java.io.StringWriter;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
+import java.util.Collections;\r
 \r
 import javax.servlet.RequestDispatcher;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.http.HttpServlet;\r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
+import javax.servlet.ServletContext;\r
 \r
 import org.apache.log4j.Logger;\r
 \r
+import compbio.stat.servlet.util.Totals;\r
 import compbio.ws.client.Services;\r
 import compbio.ws.client.WSTester;\r
 \r
@@ -30,20 +33,64 @@ public class ServiceStatus extends HttpServlet {
 \r
        private final static Logger log = Logger.getLogger(ServiceStatus.class);\r
 \r
+\r
+       private static String getURL(HttpServletRequest req) {\r
+               String scheme = req.getScheme();             // http\r
+               String serverName = req.getServerName();     // hostname.com\r
+               int serverPort = req.getServerPort();        // 80\r
+               String contextPath = req.getContextPath();   // /mywebapp\r
+               String servletPath = req.getServletPath();   // /servlet/MyServlet\r
+               String pathInfo = req.getPathInfo();         // /a/b;c=123\r
+               String queryString = req.getQueryString();          // d=789\r
+\r
+               // Reconstruct original requesting URL\r
+               StringBuffer url =  new StringBuffer();\r
+               url.append(scheme).append("://").append(serverName);\r
+\r
+               if ((serverPort != 80) && (serverPort != 443)) {\r
+                       url.append(":").append(serverPort);\r
+               }\r
+\r
+               url.append(contextPath).append(servletPath);\r
+\r
+               if (pathInfo != null) {\r
+                       url.append(pathInfo);\r
+               }\r
+               if (queryString != null) {\r
+                       url.append("?").append(queryString);\r
+               }\r
+               return url.toString();\r
+       }\r
+\r
        @Override\r
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
                        throws ServletException, IOException {\r
                StringBuffer jabawspath = req.getRequestURL();\r
                jabawspath = jabawspath.delete(jabawspath.lastIndexOf("/"), jabawspath.length());\r
                String serverPath = jabawspath.toString();\r
-               System.out.println("Testing services at " + serverPath); \r
+/**\r
+               String host = java.net.InetAddress.getLocalHost().getCanonicalHostName();\r
+               System.out.println("Testing services at " + req.getRequestURL()); \r
+               System.out.println("Really: Testing services at " + serverPath); \r
+               System.out.println("      serverPath2 is " + getURL(req)); \r
                System.out.println("      Context Path is " + req.getContextPath()); \r
+               System.out.println("      Host is " + host); \r
                System.out.println("      Request URI is " + req.getRequestURI());\r
                System.out.println("      Servlet Path is " + req.getServletPath());\r
                System.out.println("      Path Info is " + req.getPathInfo());\r
-\r
+               System.out.println("      Local Port is " + req.getLocalPort());\r
+               System.out.println("      Remote Port is " + req.getRemotePort());\r
+               System.out.println("      Local address is " + req.getLocalAddr());\r
+               System.out.println("      Remote address is " + req.getRemoteAddr());\r
+               @SuppressWarnings("unchecked")\r
+               List<String> list = Collections.list(req.getHeaderNames());\r
+               System.out.println("      HTTP header names are " + list.toString());\r
+               for (String hdr : list) {\r
+                       System.out.println("      " + hdr + " header is " + req.getHeader(hdr));\r
+               }\r
+*/\r
                List<ServiceTestResult> testResults = new ArrayList<ServiceTestResult>();\r
-\r
+               long startTime = System.nanoTime();\r
                for (Services service : Services.values()) {\r
                        StringWriter testres = new StringWriter();\r
                        PrintWriter writer = new PrintWriter(testres, true);\r
@@ -63,6 +110,8 @@ public class ServiceStatus extends HttpServlet {
                        testResults.add(result);\r
                }\r
                req.setAttribute("results", testResults);\r
+               long endTime = System.nanoTime();\r
+               req.setAttribute("timeexec", (endTime - startTime) / 1000000);\r
                RequestDispatcher rd = req.getRequestDispatcher("statpages/ServicesStatus.jsp");\r
                rd.forward(req, resp);\r
        }\r