Skip to content

Commit

Permalink
Update all play2-java benchmark projects (#3061)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz authored and NateBrady23 committed Nov 13, 2017
1 parent 592b326 commit 4401dfb
Show file tree
Hide file tree
Showing 46 changed files with 362 additions and 599 deletions.
12 changes: 6 additions & 6 deletions frameworks/Java/play2-java/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"database": "None",
"approach": "Realistic",
"classification": "Fullstack",
"platform": "Netty",
"platform": "Akka",
"webserver": "None",
"database_os": "Linux",
"notes": "",
"versus": "netty",
"versus": "akka-http",
"port": "9000",
"json_url": "/json",
"plaintext_url": "/plaintext"
Expand All @@ -33,11 +33,11 @@
"database": "MySQL",
"approach": "Realistic",
"classification": "Fullstack",
"platform": "Netty",
"platform": "Akka",
"webserver": "None",
"database_os": "Linux",
"notes": "",
"versus": "netty",
"versus": "akka-http",
"port": "9000",
"db_url": "/db",
"query_url": "/queries?queries=",
Expand All @@ -55,11 +55,11 @@
"database": "MySQL",
"approach": "Realistic",
"classification": "Fullstack",
"platform": "Netty",
"platform": "Akka",
"webserver": "None",
"database_os": "Linux",
"notes": "",
"versus": "netty",
"versus": "akka-http",
"port": "9000",
"db_url": "/db",
"query_url": "/queries?queries=",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ test-result
server.pid
*.iml
*.eml

14 changes: 1 addition & 13 deletions frameworks/Java/play2-java/play2-java-ebean-hikaricp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

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 8
* [Play 2.5.14](https://www.playframework.com/)
* [Play 2.6.7](https://www.playframework.com/)

## Test URLs
### JSON Encoding Test

* http://localhost/json

### Data-Store/Database Mapping Test

* http://localhost/db
Expand Down
Original file line number Diff line number Diff line change
@@ -1,85 +1,66 @@
package controllers;

import com.google.inject.name.Named;
import models.Fortune;
import models.World;
import play.Play;
import play.libs.F;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.With;
import scala.concurrent.ExecutionContext;
import utils.Headers;
import utils.Predicate;
import utils.Predicated;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;

import javax.inject.Inject;

@With(Headers.class)
public class Application extends Controller {
import models.Fortune;
import models.World;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import utils.DatabaseExecutionContext;

private static final int TEST_DATABASE_ROWS = 10000;
public class Application extends Controller {

@Inject @Named("dbEc") private ExecutionContext dbEc;
private final DatabaseExecutionContext dbEc;

// 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 {
@Inject @Named("dbTpe") private ThreadPoolExecutor tpe;
@Override
public boolean condition() {
return tpe.getQueue().size() <= 1024;
}
@Inject
public Application(final DatabaseExecutionContext dbEc) {
this.dbEc = dbEc;
}

@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
public F.Promise<Result> db() {
return getRandomWorlds(1).map(worlds -> ok(Json.toJson(worlds.get(0))));
public CompletionStage<Result> db() {
return getRandomWorlds(1).thenApply(worlds -> ok(Json.toJson(worlds.get(0))));
}

@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
public F.Promise<Result> queries(final String queryCountString) {
return getRandomWorlds(queryCount(queryCountString)).map(worlds -> ok(Json.toJson(worlds)));
public CompletionStage<Result> queries(final String queries) {
return getRandomWorlds(queryCount(queries)).thenApply(worlds -> ok(Json.toJson(worlds)));
}

@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
public F.Promise<Result> fortunes() {
return F.Promise.promise(() -> {
List<Fortune> fortunes = Fortune.findAll();
public CompletionStage<Result> fortunes() {
return CompletableFuture.supplyAsync(() -> {
final List<Fortune> fortunes = Fortune.findAll();
fortunes.add(new Fortune("Additional fortune added at request time."));
Collections.sort(fortunes, (f1, f2) -> f1.message.compareTo(f2.message));

return ok(views.html.fortunes.render(fortunes));
}, dbEc);
}

@Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
public F.Promise<Result> update(final String queryCountString) {
return getRandomWorlds(queryCount(queryCountString)).map(worlds -> {
Random random = ThreadLocalRandom.current();
for (World world : worlds) {
public CompletionStage<Result> update(final String queries) {
return getRandomWorlds(queryCount(queries)).thenApplyAsync(worlds -> {
final Random random = ThreadLocalRandom.current();
for (final World world : worlds) {
world.randomNumber = (long) (random.nextInt(10000) + 1);
}

List<World> updatedWorlds = World.save(worlds);
final List<World> updatedWorlds = World.save(worlds);
return ok(Json.toJson(updatedWorlds));
}, dbEc);
}

private int queryCount(String queryCountString) {
private int queryCount(final String queryCountString) {
int queryCount;
try {
queryCount = Integer.parseInt(queryCountString, 10);
} catch (NumberFormatException e) {
} catch (final NumberFormatException e) {
queryCount = 1;
}
if (queryCount < 1) {
Expand All @@ -91,13 +72,13 @@ private int queryCount(String queryCountString) {
return queryCount;
}

private F.Promise<List<World>> getRandomWorlds(final int n) {
return F.Promise.promise(() -> {
Random random = ThreadLocalRandom.current();
List<World> worlds = new ArrayList<>(n);
private CompletionStage<List<World>> getRandomWorlds(final int n) {
return CompletableFuture.supplyAsync(() -> {
final Random random = ThreadLocalRandom.current();
final List<World> worlds = new ArrayList<>(n);
for (int i = 0; i < n; ++i) {
long randomId = random.nextInt(TEST_DATABASE_ROWS) + 1;
World world = World.find(randomId);
long randomId = random.nextInt(10000) + 1;
final World world = World.find(randomId);
worlds.add(world);
}
return worlds;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package models;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Model;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.List;

import io.ebean.Finder;
import io.ebean.Model;

@Entity
public class Fortune extends Model {

private static final Finder<Long, Fortune> find = new Finder<>(Fortune.class);

@Id
public Long id = 0L;

Expand All @@ -18,11 +21,11 @@ public class Fortune extends Model {
public Fortune() {
}

public Fortune(String message) {
public Fortune(final String message) {
this.message = message;
}

public static List<Fortune> findAll() {
return Ebean.find(Fortune.class).findList();
return find.all();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
package models;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Model;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.List;

import io.ebean.Finder;
import io.ebean.Model;
import io.ebean.Transaction;

@Entity
public class World extends Model {

private static final Finder<Long, World> find = new Finder<>(World.class);

@Id
public Long id;

@Column(name = "randomNumber")
public Long randomNumber;

public static World find(Long id) {
return Ebean.find(World.class, id);
public static World find(final Long id) {
return find.byId(id);
}

public static List<World> save(final List<World> worlds) {
worlds.forEach(Ebean::update);
final int batchSize = 25;
final int batches = ((worlds.size() / batchSize) + 1);
for ( int i = 0 ; i < batches ; ++i ) {
final Transaction transaction = World.db().beginTransaction();
try {
transaction.setBatchMode(true);
transaction.setBatchSize(batchSize);
for(int j = i * batchSize ; j < Math.min((i + 1) * batchSize, worlds.size()); ++j) {
World.db().update(worlds.get(j), transaction);
}
transaction.commit();
} finally {
transaction.end();
}
}

return worlds;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils;

import javax.inject.Inject;

import akka.actor.ActorSystem;
import play.libs.concurrent.CustomExecutionContext;

public class DatabaseExecutionContext extends CustomExecutionContext {

@Inject
public DatabaseExecutionContext(final ActorSystem actorSystem) {
super(actorSystem, "database.dispatcher");
}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4401dfb

Please sign in to comment.