HelloWebServer.java 6.6 KB

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