|
| 1 | +package benchmarks; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.InetSocketAddress; |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.PreparedStatement; |
| 8 | +import java.sql.ResultSet; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.text.ParseException; |
| 11 | +import java.util.*; |
| 12 | +import java.util.concurrent.SynchronousQueue; |
| 13 | +import java.util.concurrent.ThreadPoolExecutor; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import javax.sql.DataSource; |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.fasterxml.jackson.module.afterburner.AfterburnerModule; |
| 18 | +import com.sun.net.httpserver.HttpHandler; |
| 19 | +import com.sun.net.httpserver.HttpServer; |
| 20 | +import com.zaxxer.hikari.HikariConfig; |
| 21 | +import com.zaxxer.hikari.HikariDataSource; |
| 22 | +import httl.Engine; |
| 23 | +import httl.Template; |
| 24 | + |
| 25 | +public class Server { |
| 26 | + |
| 27 | + private static final String HELLO_TEXT = "Hello, World!"; |
| 28 | + private static final byte[] HELLO_BYTES = HELLO_TEXT.getBytes(); |
| 29 | + private static final int HELLO_LENGTH = HELLO_BYTES.length; |
| 30 | + private static final String SERVER_NAME = "httpserver"; |
| 31 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 32 | + |
| 33 | + static { |
| 34 | + MAPPER.registerModule(new AfterburnerModule()); |
| 35 | + } |
| 36 | + |
| 37 | + private static List<Fortune> queryFortunes(DataSource ds) throws SQLException { |
| 38 | + List<Fortune> fortunes = new ArrayList<>(); |
| 39 | + try (Connection conn = ds.getConnection(); |
| 40 | + PreparedStatement statement = conn.prepareStatement("SELECT id, message FROM fortune"); |
| 41 | + ResultSet resultSet = statement.executeQuery()) { |
| 42 | + while (resultSet.next()) |
| 43 | + fortunes.add(new Fortune(resultSet.getInt(1), resultSet.getString(2))); |
| 44 | + } |
| 45 | + return fortunes; |
| 46 | + } |
| 47 | + |
| 48 | + private static DataSource createPostgresDataSource() throws ClassNotFoundException { |
| 49 | + Class.forName("org.postgresql.Driver"); |
| 50 | + HikariConfig config = new HikariConfig(); |
| 51 | + config.setJdbcUrl("jdbc:postgresql://tfb-database:5432/hello_world"); |
| 52 | + config.setUsername("benchmarkdbuser"); |
| 53 | + config.setPassword("benchmarkdbpass"); |
| 54 | + config.setMaximumPoolSize(64); |
| 55 | + return new HikariDataSource(config); |
| 56 | + } |
| 57 | + |
| 58 | + private static Template loadTemplate(String filename) throws IOException, ParseException { |
| 59 | + Properties props = new Properties(); |
| 60 | + props.put("import.packages", "java.util," + Fortune.class.getPackage().getName()); |
| 61 | + props.put("input.encoding", "UTF-8"); |
| 62 | + props.put("output.encoding", "UTF-8"); |
| 63 | + props.put("precompiled", "false"); |
| 64 | + Engine engine = Engine.getEngine(props); |
| 65 | + return engine.getTemplate(filename); |
| 66 | + } |
| 67 | + |
| 68 | + private static HttpHandler createPlaintextHandler() { |
| 69 | + return t -> { |
| 70 | + t.getResponseHeaders().add("Content-Type", "text/plain"); |
| 71 | + t.getResponseHeaders().add("Server", SERVER_NAME); |
| 72 | + t.sendResponseHeaders(200, HELLO_LENGTH); |
| 73 | + t.getResponseBody().write(HELLO_BYTES); |
| 74 | + t.getResponseBody().close(); |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + private static HttpHandler createJSONHandler() { |
| 79 | + return t -> { |
| 80 | + // serialize message to JSON |
| 81 | + Message msg = new Message(HELLO_TEXT); |
| 82 | + byte[] bytes; |
| 83 | + try { |
| 84 | + bytes = MAPPER.writeValueAsBytes(msg); |
| 85 | + } catch (Exception e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + // send response |
| 89 | + t.getResponseHeaders().add("Content-Type", "application/json"); |
| 90 | + t.getResponseHeaders().add("Server", SERVER_NAME); |
| 91 | + t.sendResponseHeaders(200, bytes.length); |
| 92 | + t.getResponseBody().write(bytes); |
| 93 | + t.getResponseBody().close(); |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + private static HttpHandler createFortunesHandler(DataSource ds) throws IOException, ParseException { |
| 98 | + Template template = loadTemplate("/fortunes.template.httl"); |
| 99 | + return t -> { |
| 100 | + try { |
| 101 | + // query db |
| 102 | + List<Fortune> fortunes = queryFortunes(ds); |
| 103 | + fortunes.add(new Fortune(0, "Additional fortune added at request time.")); |
| 104 | + Collections.sort(fortunes); |
| 105 | + // render template |
| 106 | + Map<String, Object> context = new HashMap<>(1); |
| 107 | + context.put("fortunes", fortunes); |
| 108 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 109 | + template.render(context, out); |
| 110 | + byte[] bytes = out.toByteArray(); |
| 111 | + // send response |
| 112 | + t.getResponseHeaders().add("Content-Type", "text/html; charset=utf-8"); |
| 113 | + t.getResponseHeaders().add("Server", SERVER_NAME); |
| 114 | + t.sendResponseHeaders(200, bytes.length); |
| 115 | + t.getResponseBody().write(bytes); |
| 116 | + t.getResponseBody().close(); |
| 117 | + } catch (SQLException | ParseException e) { |
| 118 | + throw new IOException(e); |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + public static void main(String[] args) throws Exception { |
| 124 | + // parse arguments |
| 125 | + String settings = args.length > 0 ? args[0] : ""; |
| 126 | + int port = args.length > 1 ? Integer.parseInt(args[1]) : 8080; |
| 127 | + if (settings.contains("debug")) |
| 128 | + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG"); |
| 129 | + // create server |
| 130 | + HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
| 131 | + server.setExecutor(new ThreadPoolExecutor( |
| 132 | + 8, Integer.MAX_VALUE, 300, TimeUnit.SECONDS, new SynchronousQueue<>())); |
| 133 | + // add context handlers |
| 134 | + server.createContext("/plaintext", createPlaintextHandler()); |
| 135 | + server.createContext("/json", createJSONHandler()); |
| 136 | + if (settings.contains("postgres")) { |
| 137 | + DataSource ds = createPostgresDataSource(); |
| 138 | + server.createContext("/fortunes", createFortunesHandler(ds)); |
| 139 | + } |
| 140 | + // start server |
| 141 | + server.start(); |
| 142 | + } |
| 143 | +} |
0 commit comments