Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java/servlet Caching implementation and updates #2906

Merged
merged 10 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions frameworks/Java/jetty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ This is the Jetty portion of a [benchmarking test suite](../) comparing a variet

## Handler

### JSON Encoding Test
These implementations use the Jetty's custom handler interface.
* [Plaintext test source](src/main/java/hello/handler/PlainTextHandler.java)
* [JSON test source](src/main/java/hello/handler/JsonHandler.java)

## Servlet

### JSON Encoding Test
* [Plaintext test source](src/main/java/hello/handler/PlaintextServlet.java)
These implementation are using the standart servlet API.
* [Plaintext test source](src/main/java/hello/servlet/PlaintextServlet.java)
* [JSON test source](src/main/java/hello/servlet/JsonServlet.java)

## Versions
Expand Down
48 changes: 36 additions & 12 deletions frameworks/Java/servlet/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
#Servlet Benchmarking Test
# Servlet Benchmarking Test

This is the Java Servlet portion of a [benchmarking test suite](../) comparing a variety of web development platforms.

### JSON Encoding Test
For raw Servlets there is no broad consensus on JSON encoding so we have selected the fastest available JSON encoder for Java: [Jackson](http://wiki.fasterxml.com/JacksonHome).
### Plaintext and JSON

* [Plaintext test source](src/main/java/hello/PlaintextServlet.java)
* [JSON test source](src/main/java/hello/JsonServlet.java)

### Data-Store/Database Mapping Test
### `MySQL` implementation

* [DB test source](src/main/java/hello/DbPoolServlet.java)
* [Queries test source](src/main/java/hello/DbPoolServlet.java)
* [Updates test source](src/main/java/hello/UpdateServlet.java) - using `batch updates`
* [Fortune test source](src/main/java/hello/FortunesServlet.java)

### `PostgreSQL` implementation

DB, Queries and Fortune use the same implementation as MySQL.

* [Updates test source](src/main/java/hello/PostgresUpdateServlet.java) - **not** using `batch updates` due to transaction deadlocks
* [Cache test source](src/main/java/hello/Cache2kPostgresServlet.java)

## Infrastructure Software Versions

The tests were run with:

* [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
* [Resin 4.0.34](http://www.caucho.com/)
* [Jackson 2.3.0](http://wiki.fasterxml.com/JacksonHome)
* [MySQL 5.5.29](https://dev.mysql.com/)
* [Java Oracle JDK 1.8.0](http://openjdk.java.net/)
* [Resin 4.0.53](http://www.caucho.com/)
* [Jackson 2.8.9](http://wiki.fasterxml.com/JacksonHome)
* [MySQL 5.7](https://dev.mysql.com/)
* [Postgres 9.3](http://www.postgresql.org/)
* [cache2k 1.0.0.CR4](https://cache2k.org/)

Please confirm the versions data with the latest install scripts of TFB project.

## Test URLs
### JSON Encoding Test

http://localhost:8080/servlet/json
### Default maven profile

* Plaintext - `http://localhost:8080/servlet/plaintext`
* JSON - `http://localhost:8080/servlet/json`

### Data-Store/Database Mapping Test
### `mysql` and `postgresql` Maven profiles

http://localhost:8080/servlet/db?queries=5
* DB - `http://localhost:8080/servlet/db`
* Queries - `http://localhost:8080/servlet/db?queries=`
* Updates - `http://localhost:8080/servlet/update?queries=`
* Fortune - `http://localhost:8080/servlet/fortunes`

### `postgresql` Maven profile

* Cache - `http://localhost:8080/servlet/cached-worlds`
9 changes: 5 additions & 4 deletions frameworks/Java/servlet/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
},
"postgres-raw": {
"setup_file": "setup_postgresql",
"db_url": "/servlet/postgres",
"query_url": "/servlet/postgres?queries=",
"fortune_url": "/servlet/postgres-fortunes",
"update_url": "/servlet/postgres-update?queries=",
"db_url": "/servlet/db",
"query_url": "/servlet/db?queries=",
"cached_query_url": "/servlet/cached-worlds?queries=",
"fortune_url": "/servlet/fortunes",
"update_url": "/servlet/update?queries=",
"port": 8080,
"approach": "Realistic",
"classification": "Platform",
Expand Down
128 changes: 100 additions & 28 deletions frameworks/Java/servlet/pom.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello.world</groupId>
<artifactId>world</artifactId>
<name>Servlet</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-version>1.8</java-version>
<cache2k-version>1.0.0.CR4</cache2k-version>
<!-- This is the default web.xml for plaintext and json only -->
<maven.war.xml>src/main/webapp/WEB-INF/web.xml</maven.war.xml>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1208</version>
</dependency>


<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
<version>2.8.9</version>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>

<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
Expand All @@ -50,20 +44,100 @@
<scope>provided</scope>
</dependency>

<!-- Apache Commons Lang -->
<!-- cache2k -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>${cache2k-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-all</artifactId>
<version>${cache2k-version}</version>
<scope>runtime</scope>
</dependency>

</dependencies>

<profiles>
<profile>
<id>mysql</id>
<properties>
<maven.war.xml>src/main/resources/WEB-INF/mysql/web.xml</maven.war.xml>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/WEB-INF/mysql</directory>
<includes>
<include>resin-web.xml</include>
</includes>
<excludes>
<exclude>src/main/resources/WEB-INF/</exclude>
</excludes>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>postgresql</id>
<properties>
<maven.war.xml>src/main/resources/WEB-INF/postgresql/web.xml</maven.war.xml>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/WEB-INF/postgresql</directory>
<includes>
<include>resin-web.xml</include>
</includes>
<excludes>
<exclude>src/main/resources/WEB-INF/</exclude>
</excludes>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
Expand All @@ -74,15 +148,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<warName>servlet</warName>
<!-- Use the web.xml from the current profile -->
<webXml>${maven.war.xml}</webXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
</plugin>
</plugins>
</build>
</project>
4 changes: 1 addition & 3 deletions frameworks/Java/servlet/setup.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/bin/bash

sed -i 's|localhost|'"${DBHOST}"'|g' src/main/webapp/WEB-INF/resin-web.xml

fw_depends java resin maven

mvn clean compile war:war
mvn clean install
rm -rf $RESIN_HOME/webapps/*
cp target/servlet.war $RESIN_HOME/webapps/
resinctl start
7 changes: 5 additions & 2 deletions frameworks/Java/servlet/setup_mysql.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

fw_depends mysql
fw_depends java resin maven mysql

source ./setup.sh
mvn clean compile war:war -P mysql
rm -rf $RESIN_HOME/webapps/*
cp target/servlet.war $RESIN_HOME/webapps/
resinctl start
7 changes: 5 additions & 2 deletions frameworks/Java/servlet/setup_postgresql.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

fw_depends postgresql
fw_depends java resin maven postgresql

source ./setup.sh
mvn clean compile war:war -P postgresql
rm -rf $RESIN_HOME/webapps/*
cp target/servlet.war $RESIN_HOME/webapps/
resinctl start
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.*;

import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;

/**
* Cache
*/
@SuppressWarnings("serial")
public class Cache2kPostgresServlet extends HttpServlet {
// Database details.
private static final int DB_ROWS = 10000;
private static final int LIMIT = DB_ROWS + 1;

// Database connection pool.
@Resource(name = "jdbc/hello_world")
private DataSource dataSource;
private Cache<Integer, CachedWorld> cache;

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);

Map<Integer, CachedWorld> worlds;
try {
worlds = Common.loadAll(dataSource.getConnection());
} catch (SQLException e) {
throw new ServletException(e);
}

// Build the cache
cache = new Cache2kBuilder<Integer, CachedWorld>() {}
.name("cachedWorld")
.eternal(true)
.entryCapacity(DB_ROWS)
.build();
cache.putAll(worlds);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
final int count = Common.normalise(req.getParameter("queries"));
final Random random = ThreadLocalRandom.current();

//TODO prevent duplicate numbers to be added
List<Integer> keys = new ArrayList<Integer>(count);
for (int i = 0; i < count; i++) {
keys.add(new Integer(random.nextInt(LIMIT)));
}

// Set content type to JSON
res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);

// Write JSON encoded message to the response.
Common.MAPPER.writeValue(res.getOutputStream(), cache.getAll(keys).values());
}
}
9 changes: 9 additions & 0 deletions frameworks/Java/servlet/src/main/java/hello/CachedWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hello;

public class CachedWorld extends World {

public CachedWorld(int id, int randomNumber) {
super(id, randomNumber);
}

}
Loading