Skip to content

Commit

Permalink
Play2 enhancements (#4671)
Browse files Browse the repository at this point in the history
* Upgrade to Play 2.7.1

* Revert "Play2: Use thread-pool-executor for all database benchmarks"

This reverts commit 0fcdb35.

* Let's try parallelism-max = 64

* Update dependency for reactivemongo

* Play 2.7.2 released meanwhile

* Use snapshots of play-reactivemongo for now
  • Loading branch information
mkurz authored and NateBrady23 committed Apr 29, 2019
1 parent 8e53a43 commit 547370f
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,56 @@
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 javax.inject.Inject;

import models.Fortune;
import models.World;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import utils.DatabaseExecutionContext;

public class Application extends Controller {

public Result db() {
return ok(Json.toJson(getRandomWorlds(1).get(0)));
private final DatabaseExecutionContext dbEc;

@Inject
public Application(final DatabaseExecutionContext dbEc) {
this.dbEc = dbEc;
}

public CompletionStage<Result> db() {
return getRandomWorlds(1).thenApply(worlds -> ok(Json.toJson(worlds.get(0))));
}

public Result queries(final String queries) {
return ok(Json.toJson(getRandomWorlds(queryCount(queries))));
public CompletionStage<Result> queries(final String queries) {
return getRandomWorlds(queryCount(queries)).thenApply(worlds -> ok(Json.toJson(worlds)));
}

public Result fortunes() {
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));
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));
return ok(views.html.fortunes.render(fortunes));
}, dbEc);
}

public Result update(final String queries) {
final List<World> worlds = getRandomWorlds(queryCount(queries));
final Random random = ThreadLocalRandom.current();
for (final World world : worlds) {
world.randomNumber = (long) (random.nextInt(10000) + 1);
}
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);
}

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

private int queryCount(final String queryCountString) {
Expand All @@ -57,15 +72,17 @@ private int queryCount(final String queryCountString) {
return queryCount;
}

private List<World> getRandomWorlds(final int n) {
final Random random = ThreadLocalRandom.current();
final List<World> worlds = new ArrayList<>(n);
for (int i = 0; i < n; ++i) {
long randomId = random.nextInt(10000) + 1;
final World world = World.find(randomId);
worlds.add(world);
}
return worlds;
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(10000) + 1;
final World world = World.find(randomId);
worlds.add(world);
}
return worlds;
}, dbEc);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ play.server {
# The default is 1 since Linux Kernel 3.13
# You can check via "cat /proc/sys/net/ipv4/tcp_fastopen"
# However 3 would be better, but we can't change it to that value because we don't have root permission when running the benchmarks
TCP_FASTOPEN = 1
"io.netty.channel.epoll.EpollChannelOption#TCP_FASTOPEN" = 1
}
}
}
Expand All @@ -50,31 +50,25 @@ play.server {
akka {
actor {
default-dispatcher {
executor = "thread-pool-executor"

fork-join-executor {
# one thread per core is enough
# https://github.com/playframework/playframework/issues/7242#issuecomment-295215448
parallelism-factor = 1.0

# Information about the TechEmpower benchmark environment: https://www.techempower.com/benchmarks/#section=environment
# The environment variable physical_cpu_count does NOT include the hyperthreaded cores!
parallelism-max = 14
parallelism-max = ${?thread_count}
#parallelism-max = 14
#parallelism-max = ${?thread_count}
parallelism-max = 64

task-peeking-mode="LIFO" # based on https://www.playframework.com/documentation/2.7.x/Migration24#Thread-pool-configuration
}

# https://www.playframework.com/documentation/2.7.x/ThreadPools#Highly-synchronous
thread-pool-executor {
fixed-pool-size = 44 # db conn pool (29) + number of cores (14) + housekeeping (1)
}

# https://doc.akka.io/docs/akka/2.5.11/dispatchers.html#looking-up-a-dispatcher
# Throughput defines the maximum number of messages to be
# processed per actor before the thread jumps to the next actor.
# Set to 1 for as fair as possible.
throughput = 1
throughput = 64
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.2")

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "5.0.1")
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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 javax.inject.Inject;
Expand All @@ -23,56 +25,61 @@
import play.db.Database;
import play.mvc.Controller;
import play.mvc.Result;
import utils.DatabaseExecutionContext;

public class Application extends Controller {

private static final SQLDialect DIALECT = SQLDialect.MYSQL_5_7;
private static final JSONFormat JSON_FORMAT = JSONFormat.DEFAULT_FOR_RECORDS.recordFormat(RecordFormat.OBJECT);

private final Database db;
private final DatabaseExecutionContext dbEc;

@Inject
public Application(final Database db) {
public Application(final Database db, final DatabaseExecutionContext dbEc) {
this.db = db;
this.dbEc = dbEc;
}

public Result db() {
return ok(getRandomWorlds(1).get(0).formatJSON(JSON_FORMAT)).as(JSON);
public CompletionStage<Result> db() {
return getRandomWorlds(1).thenApply(worlds -> ok(worlds.get(0).formatJSON(JSON_FORMAT)).as(JSON));
}

public Result queries(final String queries) {
return ok(getRandomWorlds(queryCount(queries)).formatJSON(JSON_FORMAT)).as(JSON);
public CompletionStage<Result> queries(final String queries) {
return getRandomWorlds(queryCount(queries)).thenApply(worlds -> ok(worlds.formatJSON(JSON_FORMAT)).as(JSON));
}

public Result fortunes() {
final List<FortuneRecord> fortunes = this.db.withConnection(connection -> {
return DSL.using(connection, DIALECT).select(FORTUNE.ID, FORTUNE.MESSAGE).from(FORTUNE).fetchInto(FortuneRecord.class);
});
fortunes.add(new FortuneRecord(UInteger.valueOf(0), "Additional fortune added at request time."));
Collections.sort(fortunes, (f1, f2) -> f1.getMessage().compareTo(f2.getMessage()));
public CompletionStage<Result> fortunes() {
return CompletableFuture.supplyAsync(() -> {
final List<FortuneRecord> fortunes = this.db.withConnection(connection -> {
return DSL.using(connection, DIALECT).select(FORTUNE.ID, FORTUNE.MESSAGE).from(FORTUNE).fetchInto(FortuneRecord.class);
});
fortunes.add(new FortuneRecord(UInteger.valueOf(0), "Additional fortune added at request time."));
Collections.sort(fortunes, (f1, f2) -> f1.getMessage().compareTo(f2.getMessage()));

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

public Result update(final String queries) {
final org.jooq.Result<WorldRecord> worlds = getRandomWorlds(queryCount(queries));

final Random random = ThreadLocalRandom.current();
for (final WorldRecord world : worlds) {
world.setRandomnumber((random.nextInt(10000) + 1));
}

final int batchSize = 25;
final int batches = ((worlds.size() / batchSize) + 1);
this.db.withConnection(connection -> {
final DSLContext sql = DSL.using(connection, DIALECT);
for ( int i = 0 ; i < batches ; ++i ) {
sql.batchUpdate(worlds.subList(i * batchSize, Math.min((i + 1) * batchSize, worlds.size()))).execute();
public CompletionStage<Result> update(final String queries) {
return getRandomWorlds(queryCount(queries)).thenApplyAsync(worlds -> {
final Random random = ThreadLocalRandom.current();
for (final WorldRecord world : worlds) {
world.setRandomnumber((random.nextInt(10000) + 1));
}
return null;
});

return ok(worlds.formatJSON(JSON_FORMAT)).as(JSON);
final int batchSize = 25;
final int batches = ((worlds.size() / batchSize) + 1);
this.db.withConnection(connection -> {
final DSLContext sql = DSL.using(connection, DIALECT);
for ( int i = 0 ; i < batches ; ++i ) {
sql.batchUpdate(worlds.subList(i * batchSize, Math.min((i + 1) * batchSize, worlds.size()))).execute();
}
return null;
});

return ok(worlds.formatJSON(JSON_FORMAT)).as(JSON);
}, dbEc);
}

private int queryCount(final String queryCountString) {
Expand All @@ -91,22 +98,24 @@ private int queryCount(final String queryCountString) {
return queryCount;
}

private org.jooq.Result<WorldRecord> getRandomWorlds(final int n) {
final Random random = ThreadLocalRandom.current();
org.jooq.Result<WorldRecord> worlds = null;
for (int i = 0; i < n; ++i) {
long randomId = random.nextInt(10000) + 1;
final org.jooq.Result<WorldRecord> world = this.db.withConnection(connection -> {
return DSL.using(connection, DIALECT).selectFrom(WORLD).where(WORLD.ID.eq(UInteger.valueOf(randomId))).fetch();
});

if(worlds == null) {
worlds = world;
} else {
worlds.add(world.get(0));
private CompletionStage<org.jooq.Result<WorldRecord>> getRandomWorlds(final int n) {
return CompletableFuture.supplyAsync(() -> {
final Random random = ThreadLocalRandom.current();
org.jooq.Result<WorldRecord> worlds = null;
for (int i = 0; i < n; ++i) {
long randomId = random.nextInt(10000) + 1;
final org.jooq.Result<WorldRecord> world = this.db.withConnection(connection -> {
return DSL.using(connection, DIALECT).selectFrom(WORLD).where(WORLD.ID.eq(UInteger.valueOf(randomId))).fetch();
});

if(worlds == null) {
worlds = world;
} else {
worlds.add(world.get(0));
}
}
}
return worlds;
return worlds;
}, dbEc);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ play.server {
# The default is 1 since Linux Kernel 3.13
# You can check via "cat /proc/sys/net/ipv4/tcp_fastopen"
# However 3 would be better, but we can't change it to that value because we don't have root permission when running the benchmarks
TCP_FASTOPEN = 1
"io.netty.channel.epoll.EpollChannelOption#TCP_FASTOPEN" = 1
}
}
}
Expand All @@ -50,31 +50,25 @@ play.server {
akka {
actor {
default-dispatcher {
executor = "thread-pool-executor"

fork-join-executor {
# one thread per core is enough
# https://github.com/playframework/playframework/issues/7242#issuecomment-295215448
parallelism-factor = 1.0

# Information about the TechEmpower benchmark environment: https://www.techempower.com/benchmarks/#section=environment
# The environment variable physical_cpu_count does NOT include the hyperthreaded cores!
parallelism-max = 14
parallelism-max = ${?thread_count}
#parallelism-max = 14
#parallelism-max = ${?thread_count}
parallelism-max = 64

task-peeking-mode="LIFO" # based on https://www.playframework.com/documentation/2.7.x/Migration24#Thread-pool-configuration
}

# https://www.playframework.com/documentation/2.7.x/ThreadPools#Highly-synchronous
thread-pool-executor {
fixed-pool-size = 44 # db conn pool (29) + number of cores (14) + housekeeping (1)
}

# https://doc.akka.io/docs/akka/2.5.11/dispatchers.html#looking-up-a-dispatcher
# Throughput defines the maximum number of messages to be
# processed per actor before the thread jumps to the next actor.
# Set to 1 for as fair as possible.
throughput = 1
throughput = 64
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.2")
Loading

0 comments on commit 547370f

Please sign in to comment.