|
1 | 1 | package blaze.techempower.benchmark
|
2 | 2 |
|
| 3 | +import java.lang.Runtime._ |
3 | 4 | import java.net.InetSocketAddress
|
| 5 | +import java.nio.ByteBuffer |
| 6 | +import java.nio.channels.AsynchronousChannelGroup |
4 | 7 | import java.nio.charset.StandardCharsets.UTF_8
|
| 8 | +import java.util.concurrent.ForkJoinPool |
| 9 | +import java.util.concurrent.ForkJoinPool._ |
5 | 10 |
|
6 |
| -import com.fasterxml.jackson.databind.ObjectMapper |
7 |
| -import com.fasterxml.jackson.module.scala.DefaultScalaModule |
8 |
| - |
9 |
| -import org.http4s.blaze.channel.SocketConnection |
| 11 | +import org.http4s.blaze.channel.nio2.NIO2SocketServerGroup |
10 | 12 | import org.http4s.blaze.http._
|
| 13 | +import org.http4s.blaze.http.HttpServerStageConfig |
| 14 | +import org.http4s.blaze.http.http1.server.Http1ServerStage |
| 15 | +import org.http4s.blaze.pipeline.LeafBuilder |
| 16 | +import com.github.plokhotnyuk.jsoniter_scala.macros._ |
| 17 | +import com.github.plokhotnyuk.jsoniter_scala.core._ |
| 18 | +import org.http4s.blaze.channel.SocketConnection |
| 19 | +import org.http4s.blaze.http.RouteAction._ |
11 | 20 |
|
12 | 21 | import scala.concurrent.Future
|
13 | 22 |
|
14 |
| -object Main { |
15 |
| - |
16 |
| - private val mapper: ObjectMapper = new ObjectMapper().registerModule(DefaultScalaModule) |
17 |
| - |
18 |
| - private val plaintextResult = Future.successful { |
19 |
| - val hs = Seq("server" -> "blaze", "content-type" -> "text/plain") |
20 |
| - RouteAction.Ok("Hello, World!".getBytes(UTF_8), hs) |
21 |
| - } |
| 23 | +case class Message(message: String) |
22 | 24 |
|
23 |
| - private def notFound(path: String) = Future.successful { |
24 |
| - RouteAction.String(s"Not found: $path", 404, "Not Found", Nil) |
25 |
| - } |
| 25 | +object Main { |
| 26 | + private val config = HttpServerStageConfig() |
| 27 | + private val fjp = new ForkJoinPool(getRuntime.availableProcessors, defaultForkJoinWorkerThreadFactory, null, true) |
| 28 | + private val jsonHeaders = Seq("server" -> "blaze", "content-type" -> "application/json") |
| 29 | + private val plaintextHeaders = Seq("server" -> "blaze", "content-type" -> "text/plain") |
26 | 30 |
|
27 |
| - // HTTP service definition |
28 |
| - private def service(request: HttpRequest): Future[RouteAction] = request.uri match { |
29 |
| - case "/plaintext" => plaintextResult |
| 31 | + private implicit val codec: JsonValueCodec[Message] = JsonCodecMaker.make[Message](CodecMakerConfig()) |
30 | 32 |
|
31 |
| - case "/json" => Future.successful { |
32 |
| - val msg = mapper.writeValueAsBytes(Map("message" -> "Hello, World!")) |
33 |
| - RouteAction.Ok(msg, Seq("server" -> "blaze", "content-type" -> "application/json")) |
| 33 | + def serve(request: HttpRequest): Future[RouteAction] = Future.successful { |
| 34 | + request.url match { |
| 35 | + case "/plaintext" => Ok("Hello, World!".getBytes(UTF_8), plaintextHeaders) |
| 36 | + case "/json" => Ok(writeToArray(Message("Hello, World!")), jsonHeaders) |
34 | 37 | }
|
35 |
| - |
36 |
| - case other => notFound(other) |
37 | 38 | }
|
38 | 39 |
|
39 |
| - def main(args: Array[String]): Unit = { |
40 |
| - val srvc = { _: SocketConnection => service(_:HttpRequest) } |
41 |
| - val server = Http1Server(srvc, new InetSocketAddress(8080), HttpServerStageConfig()) |
42 |
| - .getOrElse(sys.error("Failed to bind socket")) |
| 40 | + def connect(conn: SocketConnection): Future[LeafBuilder[ByteBuffer]] = |
| 41 | + Future.successful(LeafBuilder(new Http1ServerStage(serve, config))) |
43 | 42 |
|
44 |
| - try server.channel.join() |
45 |
| - finally { |
46 |
| - server.group.closeGroup() |
47 |
| - } |
| 43 | + def main(args: Array[String]): Unit = { |
| 44 | + NIO2SocketServerGroup(group = Some(AsynchronousChannelGroup.withThreadPool(fjp))) |
| 45 | + .bind(new InetSocketAddress(8080), connect) |
| 46 | + .getOrElse(sys.error("Failed to start server.")) |
| 47 | + .join() |
48 | 48 | }
|
49 | 49 | }
|
50 |
| - |
0 commit comments