Permalink
Browse files

Added hybrid test.

  • Loading branch information...
1 parent 382f7e4 commit 8fc11dff934164f5881f16ac596f3d4cbdd54b54 Mike Heath committed Jul 17, 2010
@@ -0,0 +1,120 @@
+//*********************************************************************
+//Copyright 2010 Intellectual Reserve, Inc. All rights reserved.
+//This notice may not be removed.
+//*********************************************************************
+package org.adbcj.web;
+
+import freemarker.template.Configuration;
+import freemarker.template.ObjectWrapper;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
+import org.springframework.web.HttpRequestHandler;
+import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+
+/**
+ * @author Mike Heath <heathma@ldschurch.org>
+ */
+public class Hybrid implements HttpRequestHandler {
+
+ private FreeMarkerConfig freeMarkerConfig;
+
+ private Map<String, SimpleJdbcTemplate> templates;
+
+ private ExecutorService executorService;
+
+ @Override
+ public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ Configuration configuration = freeMarkerConfig.getConfiguration();
+ configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
+ final Template template = configuration.getTemplate("page.ftl");
+
+ final Writer writer = response.getWriter();
+
+ final Random random = new Random();
+
+ final Map<String, Object> root = new HashMap<String, Object>();
+
+ final int maxTime = 1247052675;
+ int time = random.nextInt(maxTime);
+ final String query = "select count(*) from access_log where time > " + time + " and time < " + (time + 10000) + ";";
+ Future<Integer> count1Future = executorService.submit(new Callable<Integer>() {
+ @Override
+ public Integer call() throws Exception {
+ return templates.get("mysql_logs").queryForInt(query);
+ }
+ });
+ Future<Integer> count2Future = executorService.submit(new Callable<Integer>() {
+ @Override
+ public Integer call() throws Exception {
+ return templates.get("pg_logs").queryForInt(query);
+ }
+ });
+ int[] numbers = {random.nextInt(40000), random.nextInt(40000), random.nextInt(40000), random.nextInt(40000), random.nextInt(40000)};
+ Future<List<List<Map<String, Object>>>> contacts1Future = executorService.submit(new Select5Callable(templates.get("mysql_contacts"), numbers));
+ Future<List<List<Map<String, Object>>>> contacts2Future = executorService.submit(new Select5Callable(templates.get("pg_contacts"), numbers));
+
+ try {
+ root.put("count1", count1Future.get());
+ root.put("count2", count2Future.get());
+ root.put("contacts1", contacts1Future.get());
+ root.put("contacts2", contacts2Future.get());
+
+ response.setContentType("text/html");
+ template.process(root, writer);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ throw new ServletException(e);
+ }
+
+ }
+
+ public void setFreeMarkerConfig(FreeMarkerConfig freeMarkerConfig) {
+ this.freeMarkerConfig = freeMarkerConfig;
+ }
+
+ public void setTemplates(Map<String, SimpleJdbcTemplate> templates) {
+ this.templates = templates;
+ }
+
+ public void setExecutorService(ExecutorService executorService) {
+ this.executorService = executorService;
+ }
+
+ private class Select5Callable implements Callable<List<List<Map<String, Object>>>> {
+
+ private final SimpleJdbcTemplate jdbcTemplate;
+ private final int[] numbers;
+
+ private Select5Callable(SimpleJdbcTemplate jdbcTemplate, int[] numbers) {
+ this.jdbcTemplate = jdbcTemplate;
+ this.numbers = numbers;
+ }
+
+ @Override
+ public List<List<Map<String, Object>>> call() throws Exception {
+ List<List<Map<String, Object>>> contacts = new ArrayList<List<Map<String, Object>>>();
+ for (int i : numbers) {
+ String query = "select name, phone from contacts where id = " + i + ";";
+ contacts.add(jdbcTemplate.queryForList(query));
+ }
+ return contacts;
+ }
+ }
+}
@@ -49,7 +49,7 @@
</bean>
<bean id="jdbcTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
- <property name="maxPoolSize" value="1" />
+ <property name="maxPoolSize" value="1200" />
</bean>
<bean id="jdbcExecutorService" class="org.springframework.core.task.support.ExecutorServiceAdapter">
<constructor-arg ref="jdbcTaskExecutor" />
@@ -164,64 +164,71 @@
<property name="pools" ref="pools" />
</bean>
- <bean id="jdbc" class="org.adbcj.web.Jdbc">
- <property name="freeMarkerConfig" ref="freemarkerConfig" />
- <property name="templates">
- <util:map>
- <entry key="mysql_contacts">
- <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
- <constructor-arg>
- <bean class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/adbcjtck" />
- <property name="username" value="adbcjtck" />
- <property name="password" value="adbcjtck" />
- <property name="maxActive" ref="count" />
- </bean>
- </constructor-arg>
+
+ <util:map id="templates">
+ <entry key="mysql_contacts">
+ <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
+ <constructor-arg>
+ <bean class="org.apache.commons.dbcp.BasicDataSource">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver" />
+ <property name="url" value="jdbc:mysql://localhost/adbcjtck" />
+ <property name="username" value="adbcjtck" />
+ <property name="password" value="adbcjtck" />
+ <property name="maxActive" ref="count" />
</bean>
- </entry>
- <entry key="mysql_logs">
- <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
- <constructor-arg>
- <bean class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/adbcjtck" />
- <property name="username" value="adbcjtck" />
- <property name="password" value="adbcjtck" />
- <property name="maxActive" ref="count" />
- </bean>
- </constructor-arg>
+ </constructor-arg>
+ </bean>
+ </entry>
+ <entry key="mysql_logs">
+ <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
+ <constructor-arg>
+ <bean class="org.apache.commons.dbcp.BasicDataSource">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver" />
+ <property name="url" value="jdbc:mysql://localhost/adbcjtck" />
+ <property name="username" value="adbcjtck" />
+ <property name="password" value="adbcjtck" />
+ <property name="maxActive" ref="count" />
</bean>
- </entry>
- <entry key="pg_contacts">
- <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
- <constructor-arg>
- <bean class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="org.postgresql.Driver" />
- <property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
- <property name="username" value="adbcjtck" />
- <property name="password" value="adbcjtck" />
- <property name="maxActive" ref="count" />
- </bean>
- </constructor-arg>
+ </constructor-arg>
+ </bean>
+ </entry>
+ <entry key="pg_contacts">
+ <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
+ <constructor-arg>
+ <bean class="org.apache.commons.dbcp.BasicDataSource">
+ <property name="driverClassName" value="org.postgresql.Driver" />
+ <property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
+ <property name="username" value="adbcjtck" />
+ <property name="password" value="adbcjtck" />
+ <property name="maxActive" ref="count" />
</bean>
- </entry>
- <entry key="pg_logs">
- <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
- <constructor-arg>
- <bean class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="org.postgresql.Driver" />
- <property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
- <property name="username" value="adbcjtck" />
- <property name="password" value="adbcjtck" />
- <property name="maxActive" ref="count" />
- </bean>
- </constructor-arg>
+ </constructor-arg>
+ </bean>
+ </entry>
+ <entry key="pg_logs">
+ <bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
+ <constructor-arg>
+ <bean class="org.apache.commons.dbcp.BasicDataSource">
+ <property name="driverClassName" value="org.postgresql.Driver" />
+ <property name="url" value="jdbc:postgresql://localhost/adbcjtck" />
+ <property name="username" value="adbcjtck" />
+ <property name="password" value="adbcjtck" />
+ <property name="maxActive" ref="count" />
</bean>
- </entry>
- </util:map>
- </property>
+ </constructor-arg>
+ </bean>
+ </entry>
+ </util:map>
+
+ <bean id="jdbc" class="org.adbcj.web.Jdbc">
+ <property name="freeMarkerConfig" ref="freemarkerConfig" />
+ <property name="templates" ref="templates" />
+ </bean>
+
+ <bean id="hybrid" class="org.adbcj.web.Hybrid">
+ <property name="executorService" ref="jdbcExecutorService" />
+ <property name="freeMarkerConfig" ref="freemarkerConfig" />
+ <property name="templates" ref="templates" />
</bean>
</beans>
@@ -68,6 +68,16 @@
<servlet>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
+ <servlet-name>hybrid</servlet-name>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>hybrid</servlet-name>
+ <url-pattern>/hybrid</url-pattern>
+ </servlet-mapping>
+
+ <servlet>
+ <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
<servlet-name>remotePoolSizeManager</servlet-name>
</servlet>
<servlet-mapping>

0 comments on commit 8fc11df

Please sign in to comment.