Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added hybrid test.

  • Loading branch information...
commit 8fc11dff934164f5881f16ac596f3d4cbdd54b54 1 parent 382f7e4
authored July 16, 2010
120  web/src/main/java/org/adbcj/web/Hybrid.java
... ...
@@ -0,0 +1,120 @@
  1
+//*********************************************************************
  2
+//Copyright 2010 Intellectual Reserve, Inc. All rights reserved.
  3
+//This notice may not be removed.
  4
+//*********************************************************************
  5
+package org.adbcj.web;
  6
+
  7
+import freemarker.template.Configuration;
  8
+import freemarker.template.ObjectWrapper;
  9
+import freemarker.template.Template;
  10
+import freemarker.template.TemplateException;
  11
+import org.springframework.jdbc.core.JdbcTemplate;
  12
+import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  13
+import org.springframework.web.HttpRequestHandler;
  14
+import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
  15
+
  16
+import javax.servlet.ServletException;
  17
+import javax.servlet.http.HttpServletRequest;
  18
+import javax.servlet.http.HttpServletResponse;
  19
+import java.io.IOException;
  20
+import java.io.Writer;
  21
+import java.util.ArrayList;
  22
+import java.util.HashMap;
  23
+import java.util.List;
  24
+import java.util.Map;
  25
+import java.util.Random;
  26
+import java.util.concurrent.Callable;
  27
+import java.util.concurrent.ExecutionException;
  28
+import java.util.concurrent.ExecutorService;
  29
+import java.util.concurrent.Future;
  30
+
  31
+/**
  32
+ * @author Mike Heath <heathma@ldschurch.org>
  33
+ */
  34
+public class Hybrid implements HttpRequestHandler {
  35
+
  36
+	private FreeMarkerConfig freeMarkerConfig;
  37
+
  38
+	private Map<String, SimpleJdbcTemplate> templates;
  39
+
  40
+	private ExecutorService executorService;
  41
+
  42
+	@Override
  43
+	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  44
+		Configuration configuration = freeMarkerConfig.getConfiguration();
  45
+		configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
  46
+		final Template template = configuration.getTemplate("page.ftl");
  47
+
  48
+		final Writer writer = response.getWriter();
  49
+
  50
+		final Random random = new Random();
  51
+
  52
+		final Map<String, Object> root = new HashMap<String, Object>();
  53
+
  54
+		final int maxTime = 1247052675;
  55
+		int time = random.nextInt(maxTime);
  56
+		final String query = "select count(*) from access_log where time > " + time + " and time < " + (time + 10000) + ";";
  57
+		Future<Integer> count1Future = executorService.submit(new Callable<Integer>() {
  58
+			@Override
  59
+			public Integer call() throws Exception {
  60
+				return templates.get("mysql_logs").queryForInt(query);
  61
+			}
  62
+		});
  63
+		Future<Integer> count2Future = executorService.submit(new Callable<Integer>() {
  64
+			@Override
  65
+			public Integer call() throws Exception {
  66
+				return templates.get("pg_logs").queryForInt(query);
  67
+			}
  68
+		});
  69
+		int[] numbers = {random.nextInt(40000), random.nextInt(40000), random.nextInt(40000), random.nextInt(40000), random.nextInt(40000)};
  70
+		Future<List<List<Map<String, Object>>>> contacts1Future = executorService.submit(new Select5Callable(templates.get("mysql_contacts"), numbers));
  71
+		Future<List<List<Map<String, Object>>>> contacts2Future = executorService.submit(new Select5Callable(templates.get("pg_contacts"), numbers));
  72
+
  73
+		try {
  74
+			root.put("count1", count1Future.get());
  75
+			root.put("count2", count2Future.get());
  76
+			root.put("contacts1", contacts1Future.get());
  77
+			root.put("contacts2", contacts2Future.get());
  78
+
  79
+			response.setContentType("text/html");
  80
+			template.process(root, writer);
  81
+		} catch (Throwable e) {
  82
+			e.printStackTrace();
  83
+			throw new ServletException(e);
  84
+		}
  85
+
  86
+	}
  87
+
  88
+	public void setFreeMarkerConfig(FreeMarkerConfig freeMarkerConfig) {
  89
+		this.freeMarkerConfig = freeMarkerConfig;
  90
+	}
  91
+
  92
+	public void setTemplates(Map<String, SimpleJdbcTemplate> templates) {
  93
+		this.templates = templates;
  94
+	}
  95
+
  96
+	public void setExecutorService(ExecutorService executorService) {
  97
+		this.executorService = executorService;
  98
+	}
  99
+
  100
+	private class Select5Callable implements Callable<List<List<Map<String, Object>>>> {
  101
+
  102
+		private final SimpleJdbcTemplate jdbcTemplate;
  103
+		private final int[] numbers;
  104
+
  105
+		private Select5Callable(SimpleJdbcTemplate jdbcTemplate, int[] numbers) {
  106
+			this.jdbcTemplate = jdbcTemplate;
  107
+			this.numbers = numbers;
  108
+		}
  109
+
  110
+		@Override
  111
+		public List<List<Map<String, Object>>> call() throws Exception {
  112
+			List<List<Map<String, Object>>> contacts = new ArrayList<List<Map<String, Object>>>();
  113
+			for (int i : numbers) {
  114
+				String query = "select name, phone from contacts where id = " + i + ";";
  115
+				contacts.add(jdbcTemplate.queryForList(query));
  116
+			}
  117
+			return contacts;
  118
+		}
  119
+	}
  120
+}
117  web/src/main/resources/META-INF/spring/applicationContext.xml
@@ -49,7 +49,7 @@
49 49
 	</bean>
50 50
 
51 51
 	<bean id="jdbcTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
52  
-		<property name="maxPoolSize" value="1" />
  52
+		<property name="maxPoolSize" value="1200" />
53 53
 	</bean>
54 54
 	<bean id="jdbcExecutorService" class="org.springframework.core.task.support.ExecutorServiceAdapter">
55 55
 		<constructor-arg ref="jdbcTaskExecutor" />
@@ -164,64 +164,71 @@
164 164
 		<property name="pools" ref="pools" />
165 165
 	</bean>
166 166
 
167  
-	<bean id="jdbc" class="org.adbcj.web.Jdbc">
168  
-		<property name="freeMarkerConfig" ref="freemarkerConfig" />
169  
-		<property name="templates">
170  
-			<util:map>
171  
-				<entry key="mysql_contacts">
172  
-					<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
173  
-						<constructor-arg>
174  
-							<bean class="org.apache.commons.dbcp.BasicDataSource">
175  
-								<property name="driverClassName" value="com.mysql.jdbc.Driver" />
176  
-								<property name="url" value="jdbc:mysql://localhost/adbcjtck" />
177  
-								<property name="username" value="adbcjtck" />
178  
-								<property name="password" value="adbcjtck" />
179  
-								<property name="maxActive" ref="count" />
180  
-							</bean>
181  
-						</constructor-arg>
  167
+
  168
+	<util:map id="templates">
  169
+		<entry key="mysql_contacts">
  170
+			<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  171
+				<constructor-arg>
  172
+					<bean class="org.apache.commons.dbcp.BasicDataSource">
  173
+						<property name="driverClassName" value="com.mysql.jdbc.Driver" />
  174
+						<property name="url" value="jdbc:mysql://localhost/adbcjtck" />
  175
+						<property name="username" value="adbcjtck" />
  176
+						<property name="password" value="adbcjtck" />
  177
+						<property name="maxActive" ref="count" />
182 178
 					</bean>
183  
-				</entry>
184  
-				<entry key="mysql_logs">
185  
-					<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
186  
-						<constructor-arg>
187  
-							<bean class="org.apache.commons.dbcp.BasicDataSource">
188  
-								<property name="driverClassName" value="com.mysql.jdbc.Driver" />
189  
-								<property name="url" value="jdbc:mysql://localhost/adbcjtck" />
190  
-								<property name="username" value="adbcjtck" />
191  
-								<property name="password" value="adbcjtck" />
192  
-								<property name="maxActive" ref="count" />
193  
-							</bean>
194  
-						</constructor-arg>
  179
+				</constructor-arg>
  180
+			</bean>
  181
+		</entry>
  182
+		<entry key="mysql_logs">
  183
+			<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  184
+				<constructor-arg>
  185
+					<bean class="org.apache.commons.dbcp.BasicDataSource">
  186
+						<property name="driverClassName" value="com.mysql.jdbc.Driver" />
  187
+						<property name="url" value="jdbc:mysql://localhost/adbcjtck" />
  188
+						<property name="username" value="adbcjtck" />
  189
+						<property name="password" value="adbcjtck" />
  190
+						<property name="maxActive" ref="count" />
195 191
 					</bean>
196  
-				</entry>
197  
-				<entry key="pg_contacts">
198  
-					<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
199  
-						<constructor-arg>
200  
-							<bean class="org.apache.commons.dbcp.BasicDataSource">
201  
-								<property name="driverClassName" value="org.postgresql.Driver" />
202  
-								<property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
203  
-								<property name="username" value="adbcjtck" />
204  
-								<property name="password" value="adbcjtck" />
205  
-								<property name="maxActive" ref="count" />
206  
-							</bean>
207  
-						</constructor-arg>
  192
+				</constructor-arg>
  193
+			</bean>
  194
+		</entry>
  195
+		<entry key="pg_contacts">
  196
+			<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  197
+				<constructor-arg>
  198
+					<bean class="org.apache.commons.dbcp.BasicDataSource">
  199
+						<property name="driverClassName" value="org.postgresql.Driver" />
  200
+						<property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
  201
+						<property name="username" value="adbcjtck" />
  202
+						<property name="password" value="adbcjtck" />
  203
+						<property name="maxActive" ref="count" />
208 204
 					</bean>
209  
-				</entry>
210  
-				<entry key="pg_logs">
211  
-					<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
212  
-						<constructor-arg>
213  
-							<bean class="org.apache.commons.dbcp.BasicDataSource">
214  
-								<property name="driverClassName" value="org.postgresql.Driver" />
215  
-								<property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
216  
-								<property name="username" value="adbcjtck" />
217  
-								<property name="password" value="adbcjtck" />
218  
-								<property name="maxActive" ref="count" />
219  
-							</bean>
220  
-						</constructor-arg>
  205
+				</constructor-arg>
  206
+			</bean>
  207
+		</entry>
  208
+		<entry key="pg_logs">
  209
+			<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  210
+				<constructor-arg>
  211
+					<bean class="org.apache.commons.dbcp.BasicDataSource">
  212
+						<property name="driverClassName" value="org.postgresql.Driver" />
  213
+						<property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
  214
+						<property name="username" value="adbcjtck" />
  215
+						<property name="password" value="adbcjtck" />
  216
+						<property name="maxActive" ref="count" />
221 217
 					</bean>
222  
-				</entry>
223  
-			</util:map>
224  
-		</property>
  218
+				</constructor-arg>
  219
+			</bean>
  220
+		</entry>
  221
+	</util:map>
  222
+
  223
+	<bean id="jdbc" class="org.adbcj.web.Jdbc">
  224
+		<property name="freeMarkerConfig" ref="freemarkerConfig" />
  225
+		<property name="templates" ref="templates" />
  226
+	</bean>
  227
+
  228
+	<bean id="hybrid" class="org.adbcj.web.Hybrid">
  229
+		<property name="executorService" ref="jdbcExecutorService" />
  230
+		<property name="freeMarkerConfig" ref="freemarkerConfig" />
  231
+		<property name="templates" ref="templates" />
225 232
 	</bean>
226 233
 
227 234
 </beans>
10  web/src/main/webapp/WEB-INF/web.xml
@@ -68,6 +68,16 @@
68 68
 
69 69
 	<servlet>
70 70
 		<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
  71
+		<servlet-name>hybrid</servlet-name>
  72
+	</servlet>
  73
+
  74
+	<servlet-mapping>
  75
+		<servlet-name>hybrid</servlet-name>
  76
+		<url-pattern>/hybrid</url-pattern>
  77
+	</servlet-mapping>
  78
+	
  79
+	<servlet>
  80
+		<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
71 81
 		<servlet-name>remotePoolSizeManager</servlet-name>
72 82
 	</servlet>
73 83
 	<servlet-mapping>

0 notes on commit 8fc11df

Please sign in to comment.
Something went wrong with that request. Please try again.