-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1149 from donovanmuller/master
Added missing tests, new configuration with HikariCP and bumped Play version
- Loading branch information
Showing
33 changed files
with
781 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
frameworks/Java/play2-java/play2-java-jpa-hikaricp/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
logs | ||
project/project | ||
project/target | ||
target | ||
tmp | ||
.history | ||
dist | ||
|
||
# Ignore all dotfiles... | ||
.* | ||
# except for .gitignore | ||
!.gitignore | ||
|
||
# Ignore Play! working directory # | ||
db | ||
eclipse | ||
lib | ||
log | ||
logs | ||
modules | ||
precompiled | ||
project/project | ||
project/target | ||
target | ||
tmp | ||
test-result | ||
server.pid | ||
*.iml | ||
*.eml |
40 changes: 40 additions & 0 deletions
40
frameworks/Java/play2-java/play2-java-jpa-hikaricp/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#Play Benchmarking Test | ||
|
||
This is the Play portion of a [benchmarking test suite](../) comparing a variety of web development platforms. | ||
|
||
### JSON Encoding Test | ||
|
||
* [JSON test controller](app/controllers/Application.java) | ||
|
||
### Data-Store/Database Mapping Test | ||
|
||
* [Database test controller](app/controllers/Application.java) | ||
* [Database World test model](app/models/World.java) | ||
* [Database Fortune test model](app/models/Fortune.java) | ||
|
||
### Plain Text Test | ||
|
||
* [Plain text test controller](app/controllers/Application.java) | ||
|
||
## Infrastructure Software Versions | ||
The tests were run with: | ||
|
||
* [Java OpenJDK 1.7](http://openjdk.java.net/) | ||
* [Play 2.3.6](http://http://www.playframework.com/) | ||
|
||
## Test URLs | ||
### JSON Encoding Test | ||
|
||
* http://localhost/json | ||
|
||
### Data-Store/Database Mapping Test | ||
|
||
* http://localhost/db | ||
* http://localhost/queries?queries=10 | ||
* http://localhost/fortunes | ||
* http://localhost/update?queries=10 | ||
|
||
### Plain Text Test | ||
|
||
* http://localhost/plaintext | ||
|
151 changes: 151 additions & 0 deletions
151
frameworks/Java/play2-java/play2-java-jpa-hikaricp/app/controllers/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package controllers; | ||
|
||
import akka.dispatch.ExecutionContexts; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import models.Fortune; | ||
import models.World; | ||
import play.*; | ||
import play.core.NamedThreadFactory; | ||
import play.libs.F; | ||
import play.libs.Json; | ||
import play.mvc.*; | ||
|
||
import scala.concurrent.ExecutionContext; | ||
import utils.Predicate; | ||
import utils.Predicated; | ||
import views.html.*; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Application extends Controller { | ||
|
||
private static final int MAX_QUERIES_PER_REQUEST = 20; | ||
private static final int TEST_DATABASE_ROWS = 10000; | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
private static final int partitionCount = Play.application().configuration().getInt("db.default.partitionCount"); | ||
private static final int maxConnections = | ||
partitionCount * Play.application().configuration().getInt("db.default.maxConnectionsPerPartition"); | ||
private static final int minConnections = | ||
partitionCount * Play.application().configuration().getInt("db.default.minConnectionsPerPartition"); | ||
|
||
private static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(minConnections, maxConnections, | ||
0L, TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue<Runnable>(), | ||
new NamedThreadFactory("dbEc")); | ||
private static final ExecutionContext dbEc = ExecutionContexts.fromExecutorService(tpe); | ||
|
||
public static Result json() { | ||
final ObjectNode result = OBJECT_MAPPER.createObjectNode(); | ||
result.put("message", "Hello World!"); | ||
return ok(result); | ||
} | ||
|
||
// If the thread-pool used by the database grows too large then our server | ||
// is probably struggling, and we should start dropping requests. Set | ||
// the max size of our queue something above the number of concurrent | ||
// connections that we need to handle. | ||
public static class IsDbAvailable implements Predicate { | ||
@Override | ||
public boolean condition() { | ||
return tpe.getQueue().size() <= 1024; | ||
} | ||
} | ||
|
||
@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE) | ||
public static F.Promise<Result> db() { | ||
return getRandomWorlds(1).map(new F.Function<List<World>, Result>() { | ||
@Override | ||
public Result apply(List<World> worlds) { | ||
return ok(Json.toJson(worlds.get(0))); | ||
} | ||
}); | ||
} | ||
|
||
@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE) | ||
public static F.Promise<Result> queries(final String queryCountString) { | ||
return getRandomWorlds(queryCount(queryCountString)).map(new F.Function<List<World>, Result>() { | ||
@Override | ||
public Result apply(List<World> worlds) { | ||
return ok(Json.toJson(worlds)); | ||
} | ||
}); | ||
} | ||
|
||
@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE) | ||
public static F.Promise<Result> fortunes() { | ||
return F.Promise.promise(new F.Function0<Result>() { | ||
|
||
@Override | ||
public Result apply() throws Throwable { | ||
List<Fortune> fortunes = Fortune.findAll(); | ||
fortunes.add(new Fortune("Additional fortune added at request time.")); | ||
Collections.sort(fortunes, new Comparator<Fortune>() { | ||
@Override | ||
public int compare(Fortune f1, Fortune f2) { | ||
return f1.message.compareTo(f2.message); | ||
} | ||
}); | ||
return ok(views.html.fortunes.render(fortunes)); | ||
} | ||
}, dbEc); | ||
} | ||
|
||
@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE) | ||
public static F.Promise<Result> update(final String queryCountString) { | ||
return getRandomWorlds(queryCount(queryCountString)).map(new F.Function<List<World>, Result>() { | ||
@Override | ||
public Result apply(List<World> worlds) throws Throwable { | ||
Random random = ThreadLocalRandom.current(); | ||
for (World world : worlds) { | ||
world.randomNumber = (long) (random.nextInt(10000) + 1); | ||
} | ||
|
||
worlds = World.save(worlds); | ||
return ok(Json.toJson(worlds)); | ||
} | ||
}); | ||
} | ||
|
||
public static Result plainText() { | ||
return ok("Hello, World!"); | ||
} | ||
|
||
private static int queryCount(String queryCountString) { | ||
int queryCount; | ||
try { | ||
queryCount = Integer.parseInt(queryCountString, 10); | ||
} catch (NumberFormatException e) { | ||
queryCount = 1; | ||
} | ||
if (queryCount < 1) { | ||
queryCount = 1; | ||
} else if (queryCount > 500) { | ||
queryCount = 500; | ||
} | ||
|
||
return queryCount; | ||
} | ||
|
||
private static F.Promise<List<World>> getRandomWorlds(final int n) { | ||
return F.Promise.promise(new F.Function0<List<World>>() { | ||
@Override | ||
public List<World> apply() throws Throwable { | ||
Random random = ThreadLocalRandom.current(); | ||
List<World> worlds = new ArrayList<World>(n); | ||
for (int i = 0; i < n; ++i) { | ||
long randomId = random.nextInt(TEST_DATABASE_ROWS) + 1; | ||
World world = World.findById(randomId); | ||
worlds.add(world); | ||
} | ||
return worlds; | ||
} | ||
}, dbEc); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
frameworks/Java/play2-java/play2-java-jpa-hikaricp/app/models/Fortune.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package models; | ||
|
||
import play.db.jpa.JPA; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import java.util.List; | ||
|
||
@Entity | ||
public class Fortune { | ||
|
||
@Id | ||
public Long id = 0L; | ||
|
||
public String message; | ||
|
||
public Fortune() { | ||
} | ||
|
||
public Fortune(String message) { | ||
this.message = message; | ||
} | ||
|
||
public static List<Fortune> findAll() throws Throwable { | ||
return JPA.withTransaction("default", true, new play.libs.F.Function0<List<Fortune>>() { | ||
public List<Fortune> apply() { | ||
CriteriaQuery<Fortune> criteria = JPA.em().getCriteriaBuilder().createQuery(Fortune.class); | ||
criteria.select(criteria.from(Fortune.class)); | ||
return JPA.em().createQuery(criteria).getResultList(); | ||
} | ||
}); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
frameworks/Java/play2-java/play2-java-jpa-hikaricp/app/models/World.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package models; | ||
|
||
import play.db.jpa.JPA; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import java.util.List; | ||
|
||
@Entity | ||
public class World { | ||
|
||
@Id | ||
public Long id; | ||
|
||
@Column(name = "randomNumber") | ||
public Long randomNumber; | ||
|
||
public static World findById(final Long id) throws Throwable { | ||
return JPA.withTransaction("default", true, new play.libs.F.Function0<World>() { | ||
public World apply() { | ||
return JPA.em().find(World.class, id); | ||
} | ||
}); | ||
} | ||
|
||
public static List<World> save(final List<World> worlds) throws Throwable { | ||
for (final World world : worlds) { | ||
JPA.withTransaction("default", false, new play.libs.F.Function0<World>() { | ||
public World apply() { | ||
return JPA.em().merge(world); | ||
} | ||
}); | ||
} | ||
|
||
return worlds; | ||
} | ||
} |
Oops, something went wrong.