-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update dependencies, switch to most efficient JSON serializer and tu…
…ne JVM options (#3180) * Make ./setup.sh executable * Update dependencies and switch to most efficient JSON serializer * Tune JVM options * Fix JSON response
- Loading branch information
1 parent
3a53fd4
commit 72003b9
Showing
3 changed files
with
43 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
name := """colossus-example""" | ||
|
||
version := "0.3.0" | ||
version := "0.4.0" | ||
|
||
scalaVersion := "2.11.7" | ||
scalaVersion := "2.12.4" | ||
|
||
com.github.retronym.SbtOneJar.oneJarSettings | ||
|
||
mainClass in oneJar := Some("example.Main") | ||
|
||
libraryDependencies ++= Seq( | ||
"com.tumblr" %% "colossus" % "0.7.1-RC1", | ||
"org.json4s" %% "json4s-jackson" % "3.3.0" | ||
"com.tumblr" %% "colossus" % "0.11.0-M4", | ||
"com.github.plokhotnyuk.jsoniter-scala" %% "macros" % "0.3.4" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 38 additions & 48 deletions
86
frameworks/Scala/colossus/src/main/scala/example/Main.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,48 @@ | ||
package example | ||
|
||
import colossus._ | ||
import colossus.core.{Initializer, Server, ServerRef, ServerSettings} | ||
import colossus.service._ | ||
import Callback.Implicits._ | ||
import akka.actor.ActorSystem | ||
import colossus.core._ | ||
import colossus.protocols.http._ | ||
import colossus.service._ | ||
import colossus.service.Callback.Implicits._ | ||
import colossus.service.GenRequestHandler.PartialHandler | ||
import colossus.util.DataSize | ||
import com.github.plokhotnyuk.jsoniter_scala.core._ | ||
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker._ | ||
import com.github.plokhotnyuk.jsoniter_scala.macros._ | ||
|
||
import org.json4s._ | ||
import org.json4s.jackson.JsonMethods._ | ||
import org.json4s.JsonDSL._ | ||
|
||
object BenchmarkService { | ||
|
||
implicit object JsonBody extends HttpBodyEncoder[JValue] { | ||
val jsonHeader = HttpHeader("Content-Type", "application/json") | ||
def encode(json: JValue) = new HttpBody(compact(render(json)).getBytes("UTF-8"), Some(jsonHeader)) | ||
} | ||
|
||
def json: JValue = ("message" -> "Hello, World!") | ||
val plaintext = HttpBody("Hello, World!") | ||
val serverHeader = HttpHeader("Server", "Colossus") | ||
|
||
def start(port: Int)(implicit io: IOSystem) { | ||
|
||
val serverConfig = ServerSettings( | ||
port = port, | ||
maxConnections = 16384, | ||
tcpBacklogSize = Some(1024) | ||
) | ||
val serviceConfig = ServiceConfig( | ||
requestMetrics = false | ||
) | ||
|
||
Server.start("benchmark", serverConfig) { new Initializer(_) { | ||
|
||
val dateHeader = new DateHeader | ||
val headers = HttpHeaders(serverHeader, dateHeader) | ||
|
||
def onConnect = ctx => new Service[Http](serviceConfig, ctx){ | ||
def handle = { | ||
case req if (req.head.url == "/plaintext") => req.ok(plaintext, headers) | ||
case req if (req.head.url == "/json") => req.ok(json, headers) | ||
} | ||
} | ||
}} | ||
} | ||
|
||
} | ||
import scala.concurrent.duration.Duration | ||
|
||
case class Message(message: String) | ||
|
||
object Main extends App { | ||
def toHttpBodyEncoder[T](codec: JsonCodec[T]): HttpBodyEncoder[T] = new HttpBodyEncoder[T] { | ||
override def encode(data: T): HttpBody = new HttpBody(JsonWriter.write(codec, data)) | ||
|
||
implicit val io_system = IOSystem() | ||
|
||
BenchmarkService.start(9007) | ||
override def contentType: String = "application/json" | ||
} | ||
|
||
val serverConfig = ServerSettings( | ||
port = 9007, | ||
maxConnections = 16384, | ||
tcpBacklogSize = Some(1024)) | ||
val serviceConfig = ServiceConfig( | ||
logErrors = false, | ||
requestMetrics = false, | ||
requestTimeout = Duration("1s"), | ||
requestBufferSize = 65536, | ||
maxRequestSize = DataSize(1024 * 1024)) | ||
|
||
implicit val actorSystem: ActorSystem = ActorSystem() | ||
implicit val ioSystem: IOSystem = IOSystem() | ||
implicit val messageEncoder: HttpBodyEncoder[Message] = toHttpBodyEncoder(make[Message](CodecMakerConfig())) | ||
|
||
HttpServer.start("Colossus", serverConfig)(initContext => new Initializer(initContext) { | ||
override def onConnect: RequestHandlerFactory = serverContext => new RequestHandler(serverContext, serviceConfig) { | ||
override def handle: PartialHandler[Http] = { | ||
case req if req.head.url == "/plaintext" => req.ok("Hello, World!") | ||
case req if req.head.url == "/json" => req.ok(Message("Hello, World!")) | ||
} | ||
} | ||
}) | ||
} |