Browse Source

added postgres test for servlet

Patrick Falls 12 years ago
parent
commit
b4d0ea106a

+ 8 - 0
servlet/benchmark_config

@@ -15,6 +15,14 @@
       "update_url": "/servlet/update?queries=",
       "update_url": "/servlet/update?queries=",
       "port": 8080,
       "port": 8080,
       "sort": 19
       "sort": 19
+    },
+    "postgres-raw": {
+      "setup_file": "setup",
+      "db_url": "/servlet/postgres",
+      "query_url": "/servlet/postgres?queries=",
+      "update_url": "/servlet/postgres-update?queries=",
+      "port": 8080,
+      "sort": 99
     }
     }
   }]
   }]
 }
 }

+ 7 - 0
servlet/pom.xml

@@ -17,6 +17,13 @@
             <version>5.1.23</version>
             <version>5.1.23</version>
         </dependency>
         </dependency>
 
 
+	<dependency>
+	    <groupId>postgresql</groupId>
+	    <artifactId>postgresql</artifactId>
+	    <version>9.1-901.jdbc4</version>
+	</dependency>
+            
+
         <dependency>
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
             <artifactId>jackson-databind</artifactId>

+ 1 - 1
servlet/setup.py

@@ -4,7 +4,7 @@ import sys
 import setup_util
 import setup_util
 
 
 def start(args):
 def start(args):
-  setup_util.replace_text("servlet/src/main/webapp/WEB-INF/resin-web.xml", "mysql:\/\/.*:3306", "mysql://" + args.database_host + ":3306")
+  setup_util.replace_text("servlet/src/main/webapp/WEB-INF/resin-web.xml", "localhost", args.database_host)
 
 
   try:
   try:
     subprocess.check_call("mvn clean compile war:war", shell=True, cwd="servlet")
     subprocess.check_call("mvn clean compile war:war", shell=True, cwd="servlet")

+ 99 - 0
servlet/src/main/java/hello/PostgresServlet.java

@@ -0,0 +1,99 @@
+package hello;
+
+import java.io.*;
+import java.sql.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+import javax.annotation.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import javax.sql.*;
+
+/**
+ * Database connectivity (with a Servlet-container managed pool) test.
+ */
+@SuppressWarnings("serial")
+public class PostgresServlet extends HttpServlet
+{
+  // Database details.
+  private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
+  private static final int    DB_ROWS  = 10000;
+
+  // Database connection pool.
+  @Resource(name="jdbc/postgres_hello_world")
+  private DataSource postgresDataSource;
+    
+  @Override
+  protected void doGet(HttpServletRequest req, HttpServletResponse res)
+      throws ServletException, IOException
+  {
+    // Set content type to JSON
+    res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
+
+    // Reference the data source.
+    final DataSource source = postgresDataSource;
+    
+    // Get the count of queries to run.
+    int count = 1;
+    try
+    {
+      count = Integer.parseInt(req.getParameter("queries"));
+      
+      // Bounds check.
+      if (count > 500)
+      {
+        count = 500;
+      }
+      if (count < 1)
+      {
+        count = 1;
+      }
+    }
+    catch (NumberFormatException nfexc)
+    {
+      // Do nothing.
+    }
+
+    // Fetch some rows from the database.
+    final World[] worlds = new World[count];
+    final Random random = ThreadLocalRandom.current();
+    
+    try (Connection conn = source.getConnection())
+    {
+      try (PreparedStatement statement = conn.prepareStatement(DB_QUERY, 
+          ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY))
+      {
+        // Run the query the number of times requested.
+        for (int i = 0; i < count; i++)
+        {
+          final int id = random.nextInt(DB_ROWS) + 1;
+          statement.setInt(1, id);
+          
+          try (ResultSet results = statement.executeQuery())
+          {
+            if (results.next())
+            {
+              worlds[i] = new World(id, results.getInt("randomNumber"));
+            }
+          }
+        }
+      }
+    }
+    catch (SQLException sqlex)
+    {
+      System.err.println("SQL Exception: " + sqlex);
+    }
+    
+    // Write JSON encoded message to the response.
+    try
+    {
+      Common.MAPPER.writeValue(res.getOutputStream(), worlds);
+    }
+    catch (IOException ioe) 
+    {
+      // do nothing
+    }
+  }
+}
+

+ 109 - 0
servlet/src/main/java/hello/PostgresUpdateServlet.java

@@ -0,0 +1,109 @@
+package hello;
+
+import java.io.*;
+import java.sql.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+import javax.annotation.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import javax.sql.*;
+
+/**
+ * Database connectivity (with a Servlet-container managed pool) test.
+ */
+@SuppressWarnings("serial")
+public class PostgresUpdateServlet extends HttpServlet
+{
+  // Database details.
+  private static final String DB_QUERY      = "SELECT * FROM World WHERE id = ?";
+  private static final String UPDATE_QUERY  = "UPDATE World SET randomNumber = ? WHERE id = ?";
+  private static final int    DB_ROWS       = 10000;
+
+  // Database connection pool.
+  @Resource(name="jdbc/postgres_hello_world")
+  private DataSource postgresDataSource;
+    
+  @Override
+  protected void doGet(HttpServletRequest req, HttpServletResponse res)
+      throws ServletException, IOException
+  {
+    // Set content type to JSON
+    res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
+
+    // Reference the data source.
+    final DataSource source = postgresDataSource;
+    
+    // Get the count of queries to run.
+    int count = 1;
+    try
+    {
+      count = Integer.parseInt(req.getParameter("queries"));
+      
+      // Bounds check.
+      if (count > 500)
+      {
+        count = 500;
+      }
+      if (count < 1)
+      {
+        count = 1;
+      }
+    }
+    catch (NumberFormatException nfexc)
+    {
+      // Do nothing.
+    }
+
+    // Fetch some rows from the database.
+    final World[] worlds = new World[count];
+    final Random random = ThreadLocalRandom.current();
+    
+    try (Connection conn = source.getConnection())
+    {
+      try (PreparedStatement statement = conn.prepareStatement(DB_QUERY, 
+          ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY))
+      {
+        // Run the query the number of times requested.
+        for (int i = 0; i < count; i++)
+        {
+          final int id = random.nextInt(DB_ROWS) + 1;
+          statement.setInt(1, id);
+          
+          try (ResultSet results = statement.executeQuery())
+          {
+            if (results.next())
+            {
+              worlds[i] = new World(id, results.getInt("randomNumber"));
+
+              // Update row
+              try (PreparedStatement statement2 = conn.prepareStatement(UPDATE_QUERY))
+              {
+		worlds[i].setRandomNumber(random.nextInt(DB_ROWS) + 1);
+                statement2.setInt(1, worlds[i].getRandomNumber());
+                statement2.setInt(2, id);
+
+                statement2.execute();
+              }
+            }
+          }
+        }
+      }
+    }
+    catch (SQLException sqlex)
+    {
+      System.err.println("SQL Exception: " + sqlex);
+    }
+    
+    // Write JSON encoded message to the response.
+    try
+    {
+      Common.MAPPER.writeValue(res.getOutputStream(), worlds);
+    }
+    catch (IOException ioe) 
+    {
+      // do nothing
+    }
+  }
+}

+ 11 - 2
servlet/src/main/webapp/WEB-INF/resin-web.xml

@@ -3,11 +3,20 @@
 <database jndi-name='jdbc/hello_world'>
 <database jndi-name='jdbc/hello_world'>
   <driver>
   <driver>
     <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type>
     <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type>
-    <url>jdbc:mysql://172.16.98.98:3306/hello_world?jdbcCompliantTruncation=false&amp;elideSetAutoCommits=true&amp;useLocalSessionState=true&amp;cachePrepStmts=true&amp;cacheCallableStmts=true&amp;alwaysSendSetIsolation=false&amp;prepStmtCacheSize=4096&amp;cacheServerConfiguration=true&amp;prepStmtCacheSqlLimit=2048&amp;zeroDateTimeBehavior=convertToNull&amp;traceProtocol=false&amp;useUnbufferedInput=false&amp;useReadAheadInput=false&amp;maintainTimeStats=false&amp;useServerPrepStmts&amp;cacheRSMetadata=true</url>
+    <url>jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&amp;elideSetAutoCommits=true&amp;useLocalSessionState=true&amp;cachePrepStmts=true&amp;cacheCallableStmts=true&amp;alwaysSendSetIsolation=false&amp;prepStmtCacheSize=4096&amp;cacheServerConfiguration=true&amp;prepStmtCacheSqlLimit=2048&amp;zeroDateTimeBehavior=convertToNull&amp;traceProtocol=false&amp;useUnbufferedInput=false&amp;useReadAheadInput=false&amp;maintainTimeStats=false&amp;useServerPrepStmts&amp;cacheRSMetadata=true</url>
     <user>benchmarkdbuser</user>
     <user>benchmarkdbuser</user>
     <password>benchmarkdbpass</password>
     <password>benchmarkdbpass</password>
     <useUnicode/>
     <useUnicode/>
   </driver>
   </driver>
 </database>
 </database>
 
 
-</web-app>
+<database jndi-name='jdbc/postgres_hello_world'>
+  <driver>
+    <type>org.postgresql.Driver</type>
+    <url>jdbc:postgresql://localhost:5432/hello_world?jdbcCompliantTruncation=false&amp;elideSetAutoCommits=true&amp;useLocalSessionState=true&amp;cachePrepStmts=true&amp;cacheCallableStmts=true&amp;alwaysSendSetIsolation=false&amp;prepStmtCacheSize=4096&amp;cacheServerConfiguration=true&amp;prepStmtCacheSqlLimit=2048&amp;zeroDateTimeBehavior=convertToNull&amp;traceProtocol=false&amp;useUnbufferedInput=false&amp;useReadAheadInput=false&amp;maintainTimeStats=false&amp;useServerPrepStmts&amp;cacheRSMetadata=true</url>
+    <user>benchmarkdbuser</user>
+    <password>benchmarkdbpass</password>
+  </driver>
+</database>
+
+</web-app>

+ 12 - 0
servlet/src/main/webapp/WEB-INF/web.xml

@@ -23,4 +23,16 @@
     <load-on-startup/>
     <load-on-startup/>
   </servlet>
   </servlet>
   <servlet-mapping url-regexp='^/update$' servlet-name='update'/>
   <servlet-mapping url-regexp='^/update$' servlet-name='update'/>
+  <servlet>
+    <servlet-name>postgres</servlet-name>
+    <servlet-class>hello.PostgresServlet</servlet-class>
+    <load-on-startup/>
+  </servlet>
+  <servlet-mapping url-regexp='^/postgres$' servlet-name='postgres'/>
+  <servlet>
+    <servlet-name>postgres-update</servlet-name>
+    <servlet-class>hello.PostgresUpdateServlet</servlet-class>
+    <load-on-startup/>
+  </servlet>
+  <servlet-mapping url-regexp='^/postgres-update$' servlet-name='postgres-update'/>
 </web-app>
 </web-app>