HelloWebServer.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package hello;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.github.mustachejava.DefaultMustacheFactory;
  4. import com.github.mustachejava.MustacheFactory;
  5. import com.google.common.cache.CacheBuilder;
  6. import com.google.common.cache.CacheLoader;
  7. import com.google.common.cache.LoadingCache;
  8. import com.google.common.net.MediaType;
  9. import com.mongodb.DB;
  10. import com.mongodb.MongoClient;
  11. import io.undertow.Handlers;
  12. import io.undertow.Undertow;
  13. import io.undertow.util.Headers;
  14. import javax.sql.DataSource;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.Properties;
  22. /**
  23. * An implementation of the TechEmpower benchmark tests using the Undertow web
  24. * server. The only test that truly exercises Undertow in isolation is the
  25. * plaintext test. For the rest, it uses best-of-breed components that are
  26. * expected to perform well. The idea is that using these components enables
  27. * these tests to serve as performance baselines for hypothetical, Undertow-based
  28. * frameworks. For instance, it is unlikely that such frameworks would complete
  29. * the JSON test faster than this will, because this implementation uses
  30. * Undertow and Jackson in the most direct way possible to fulfill the test
  31. * requirements.
  32. */
  33. public final class HelloWebServer {
  34. //MediaType.toString() does non-trivial work and does not cache the result
  35. //so we cache it here
  36. public static final String JSON_UTF8 = MediaType.JSON_UTF_8.toString();
  37. public static final String TEXT_PLAIN = MediaType.PLAIN_TEXT_UTF_8.toString();
  38. public static final String HTML_UTF8 = MediaType.HTML_UTF_8.toString();
  39. public static void main(String[] args) throws Exception {
  40. new HelloWebServer();
  41. }
  42. /**
  43. * Creates and starts a new web server whose configuration is specified in the
  44. * {@code server.properties} file.
  45. *
  46. * @throws IOException if the application properties file cannot be read or
  47. * the Mongo database hostname cannot be resolved
  48. * @throws SQLException if reading from the SQL database (while priming the
  49. * cache) fails
  50. */
  51. public HelloWebServer() throws IOException, SQLException {
  52. Properties properties = new Properties();
  53. try (InputStream in = HelloWebServer.class.getResourceAsStream(
  54. "server.properties")) {
  55. properties.load(in);
  56. }
  57. final ObjectMapper objectMapper = new ObjectMapper();
  58. final MustacheFactory mustacheFactory = new DefaultMustacheFactory();
  59. final DataSource mysql = Helper.newDataSource(
  60. properties.getProperty("mysql.uri"),
  61. properties.getProperty("mysql.user"),
  62. properties.getProperty("mysql.password"));
  63. final DataSource postgresql = Helper.newDataSource(
  64. properties.getProperty("postgresql.uri"),
  65. properties.getProperty("postgresql.user"),
  66. properties.getProperty("postgresql.password"));
  67. final DB mongodb = new MongoClient(properties.getProperty("mongodb.host"))
  68. .getDB(properties.getProperty("mongodb.name"));
  69. //
  70. // The world cache is primed at startup with all values. It doesn't
  71. // matter which database backs it; they all contain the same information
  72. // and the CacheLoader.load implementation below is never invoked.
  73. //
  74. final LoadingCache<Integer, World> worldCache = CacheBuilder.newBuilder()
  75. .build(new CacheLoader<Integer, World>() {
  76. @Override
  77. public World load(Integer id) throws Exception {
  78. try (Connection connection = mysql.getConnection();
  79. PreparedStatement statement = connection.prepareStatement(
  80. "SELECT * FROM World WHERE id = ?",
  81. ResultSet.TYPE_FORWARD_ONLY,
  82. ResultSet.CONCUR_READ_ONLY)) {
  83. statement.setInt(1, id);
  84. try (ResultSet resultSet = statement.executeQuery()) {
  85. resultSet.next();
  86. return new World(
  87. resultSet.getInt("id"),
  88. resultSet.getInt("randomNumber"));
  89. }
  90. }
  91. }
  92. });
  93. try (Connection connection = mysql.getConnection();
  94. PreparedStatement statement = connection.prepareStatement(
  95. "SELECT * FROM World",
  96. ResultSet.TYPE_FORWARD_ONLY,
  97. ResultSet.CONCUR_READ_ONLY);
  98. ResultSet resultSet = statement.executeQuery()) {
  99. while (resultSet.next()) {
  100. World world = new World(
  101. resultSet.getInt("id"),
  102. resultSet.getInt("randomNumber"));
  103. worldCache.put(world.id, world);
  104. }
  105. }
  106. Undertow.builder()
  107. .addListener(
  108. Integer.parseInt(properties.getProperty("web.port")),
  109. properties.getProperty("web.host"))
  110. .setBufferSize(1024 * 16)
  111. .setHandler(Handlers.date(Handlers.header(Handlers.path()
  112. .addPath("/json",
  113. new JsonHandler(objectMapper))
  114. .addPath("/db/mysql",
  115. new DbSqlHandler(objectMapper, mysql))
  116. .addPath("/db/postgresql",
  117. new DbSqlHandler(objectMapper, postgresql))
  118. .addPath("/db/mongodb",
  119. new DbMongoHandler(objectMapper, mongodb))
  120. .addPath("/fortunes/mysql",
  121. new FortunesSqlHandler(mustacheFactory, mysql))
  122. .addPath("/fortunes/postgresql",
  123. new FortunesSqlHandler(mustacheFactory, postgresql))
  124. .addPath("/fortunes/mongodb",
  125. new FortunesMongoHandler(mustacheFactory, mongodb))
  126. .addPath("/updates/mysql",
  127. new UpdatesSqlHandler(objectMapper, mysql))
  128. .addPath("/updates/postgresql",
  129. new UpdatesSqlHandler(objectMapper, postgresql))
  130. .addPath("/updates/mongodb",
  131. new UpdatesMongoHandler(objectMapper, mongodb))
  132. .addPath("/plaintext",
  133. new PlaintextHandler())
  134. .addPath("/cache",
  135. new CacheHandler(objectMapper, worldCache)),
  136. Headers.SERVER_STRING, "undertow")))
  137. .build()
  138. .start();
  139. }
  140. }