|
@@ -1,9 +1,7 @@
|
|
package com.techempower;
|
|
package com.techempower;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
-import io.jooby.Context;
|
|
|
|
import io.jooby.Jooby;
|
|
import io.jooby.Jooby;
|
|
-import io.jooby.ServerOptions;
|
|
|
|
import io.jooby.hikari.HikariModule;
|
|
import io.jooby.hikari.HikariModule;
|
|
import io.jooby.json.JacksonModule;
|
|
import io.jooby.json.JacksonModule;
|
|
import io.jooby.rocker.RockerModule;
|
|
import io.jooby.rocker.RockerModule;
|
|
@@ -15,12 +13,11 @@ import java.sql.ResultSet;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.Random;
|
|
|
|
import java.util.StringJoiner;
|
|
import java.util.StringJoiner;
|
|
-import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
+import static com.techempower.Util.randomWorld;
|
|
import static io.jooby.ExecutionMode.EVENT_LOOP;
|
|
import static io.jooby.ExecutionMode.EVENT_LOOP;
|
|
import static io.jooby.MediaType.JSON;
|
|
import static io.jooby.MediaType.JSON;
|
|
|
|
|
|
@@ -28,8 +25,6 @@ public class App extends Jooby {
|
|
|
|
|
|
private static final String SELECT_WORLD = "select * from world where id=?";
|
|
private static final String SELECT_WORLD = "select * from world where id=?";
|
|
|
|
|
|
- private static final int DB_ROWS = 10000;
|
|
|
|
-
|
|
|
|
private static final String MESSAGE = "Hello, World!";
|
|
private static final String MESSAGE = "Hello, World!";
|
|
|
|
|
|
private static final byte[] MESSAGE_BYTES = MESSAGE.getBytes(StandardCharsets.UTF_8);
|
|
private static final byte[] MESSAGE_BYTES = MESSAGE.getBytes(StandardCharsets.UTF_8);
|
|
@@ -39,12 +34,6 @@ public class App extends Jooby {
|
|
}
|
|
}
|
|
|
|
|
|
{
|
|
{
|
|
- /** Server options (netty only): */
|
|
|
|
- setServerOptions(new ServerOptions()
|
|
|
|
- .setSingleLoop(true)
|
|
|
|
- .setDirectBuffers(true)
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
/** JSON: */
|
|
/** JSON: */
|
|
install(new JacksonModule());
|
|
install(new JacksonModule());
|
|
ObjectMapper mapper = require(ObjectMapper.class);
|
|
ObjectMapper mapper = require(ObjectMapper.class);
|
|
@@ -70,12 +59,10 @@ public class App extends Jooby {
|
|
|
|
|
|
/** Single query: */
|
|
/** Single query: */
|
|
get("/db", ctx -> {
|
|
get("/db", ctx -> {
|
|
- Random rnd = ThreadLocalRandom.current();
|
|
|
|
World result;
|
|
World result;
|
|
try (Connection conn = ds.getConnection()) {
|
|
try (Connection conn = ds.getConnection()) {
|
|
- int id = nextRandom(rnd);
|
|
|
|
try (final PreparedStatement statement = conn.prepareStatement(SELECT_WORLD)) {
|
|
try (final PreparedStatement statement = conn.prepareStatement(SELECT_WORLD)) {
|
|
- statement.setInt(1, id);
|
|
|
|
|
|
+ statement.setInt(1, randomWorld());
|
|
try (ResultSet rs = statement.executeQuery()) {
|
|
try (ResultSet rs = statement.executeQuery()) {
|
|
rs.next();
|
|
rs.next();
|
|
result = new World(rs.getInt("id"), rs.getInt("randomNumber"));
|
|
result = new World(rs.getInt("id"), rs.getInt("randomNumber"));
|
|
@@ -89,13 +76,11 @@ public class App extends Jooby {
|
|
|
|
|
|
/** Multiple queries: */
|
|
/** Multiple queries: */
|
|
get("/queries", ctx -> {
|
|
get("/queries", ctx -> {
|
|
- World[] result = new World[queries(ctx)];
|
|
|
|
- Random rnd = ThreadLocalRandom.current();
|
|
|
|
|
|
+ World[] result = new World[Util.queries(ctx)];
|
|
try (Connection conn = ds.getConnection()) {
|
|
try (Connection conn = ds.getConnection()) {
|
|
for (int i = 0; i < result.length; i++) {
|
|
for (int i = 0; i < result.length; i++) {
|
|
- int id = nextRandom(rnd);
|
|
|
|
try (final PreparedStatement statement = conn.prepareStatement(SELECT_WORLD)) {
|
|
try (final PreparedStatement statement = conn.prepareStatement(SELECT_WORLD)) {
|
|
- statement.setInt(1, id);
|
|
|
|
|
|
+ statement.setInt(1, randomWorld());
|
|
try (ResultSet rs = statement.executeQuery()) {
|
|
try (ResultSet rs = statement.executeQuery()) {
|
|
rs.next();
|
|
rs.next();
|
|
result[i] = new World(rs.getInt("id"), rs.getInt("randomNumber"));
|
|
result[i] = new World(rs.getInt("id"), rs.getInt("randomNumber"));
|
|
@@ -110,9 +95,7 @@ public class App extends Jooby {
|
|
|
|
|
|
/** Updates: */
|
|
/** Updates: */
|
|
get("/updates", ctx -> {
|
|
get("/updates", ctx -> {
|
|
- World[] result = new World[queries(ctx)];
|
|
|
|
- Random rnd = ThreadLocalRandom.current();
|
|
|
|
-
|
|
|
|
|
|
+ World[] result = new World[Util.queries(ctx)];
|
|
StringJoiner updateSql = new StringJoiner(
|
|
StringJoiner updateSql = new StringJoiner(
|
|
", ",
|
|
", ",
|
|
"UPDATE world SET randomNumber = temp.randomNumber FROM (VALUES ",
|
|
"UPDATE world SET randomNumber = temp.randomNumber FROM (VALUES ",
|
|
@@ -121,7 +104,7 @@ public class App extends Jooby {
|
|
try (Connection connection = ds.getConnection()) {
|
|
try (Connection connection = ds.getConnection()) {
|
|
try (PreparedStatement statement = connection.prepareStatement(SELECT_WORLD)) {
|
|
try (PreparedStatement statement = connection.prepareStatement(SELECT_WORLD)) {
|
|
for (int i = 0; i < result.length; i++) {
|
|
for (int i = 0; i < result.length; i++) {
|
|
- statement.setInt(1, nextRandom(rnd));
|
|
|
|
|
|
+ statement.setInt(1, randomWorld());
|
|
try (ResultSet rs = statement.executeQuery()) {
|
|
try (ResultSet rs = statement.executeQuery()) {
|
|
rs.next();
|
|
rs.next();
|
|
result[i] = new World(rs.getInt("id"), rs.getInt("randomNumber"));
|
|
result[i] = new World(rs.getInt("id"), rs.getInt("randomNumber"));
|
|
@@ -134,7 +117,7 @@ public class App extends Jooby {
|
|
try (PreparedStatement statement = connection.prepareStatement(updateSql.toString())) {
|
|
try (PreparedStatement statement = connection.prepareStatement(updateSql.toString())) {
|
|
int i = 0;
|
|
int i = 0;
|
|
for (World world : result) {
|
|
for (World world : result) {
|
|
- world.randomNumber = nextRandom(rnd);
|
|
|
|
|
|
+ world.randomNumber = randomWorld();
|
|
statement.setInt(++i, world.id);
|
|
statement.setInt(++i, world.id);
|
|
statement.setInt(++i, world.randomNumber);
|
|
statement.setInt(++i, world.randomNumber);
|
|
}
|
|
}
|
|
@@ -169,20 +152,4 @@ public class App extends Jooby {
|
|
public static void main(final String[] args) {
|
|
public static void main(final String[] args) {
|
|
runApp(args, EVENT_LOOP, App::new);
|
|
runApp(args, EVENT_LOOP, App::new);
|
|
}
|
|
}
|
|
-
|
|
|
|
- private final int nextRandom(Random rnd) {
|
|
|
|
- return rnd.nextInt(DB_ROWS) + 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private int queries(Context ctx) {
|
|
|
|
- String value = ctx.query("queries").value("");
|
|
|
|
- if (value.length() == 0) {
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- return Math.min(500, Math.max(1, Integer.parseInt(value)));
|
|
|
|
- } catch (NumberFormatException x) {
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
}
|
|
}
|