Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tellnobody1 committed Sep 11, 2024
1 parent d5b1a6b commit 7d6aa1b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 53 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.4
- uses: actions/checkout@v4.1.7
- uses: actions/setup-java@v4.3.0
with:
clean: false
fetch-depth: 0 # with tags
submodules: 'recursive'
- uses: actions/setup-java@v1.4.3
with:
java-version: 18
java-version: 22
- env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: sbt test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ curl -X TRACE -d 'こんにちは' http://localhost:9012
| -------- | -----:| --------:|-----:|
| frontier | 75 | 37 | 100% |
| ziohttp | 75 | 39 | 95% |
| akkahttp | 75 | 60 | 62% |

```sh
ulimit -n 65536
Expand Down
30 changes: 30 additions & 0 deletions benchmark/src/akkahttp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package benchmark
package akkahttp

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.ws.{BinaryMessage, Message}
import akka.http.scaladsl.server.Directives.{handleWebSocketMessages, path}
import akka.stream.scaladsl.Flow
import scala.util.{Failure, Success}

object AkkahttpWs extends App:
given system: ActorSystem = ActorSystem("akkahttp-ws")
import system.dispatcher
import conf.port
Http()
.newServerAt("localhost", port)
.bind:
path(conf.path):
handleWebSocketMessages:
Flow[Message].collect:
case bm: BinaryMessage.Strict =>
BinaryMessage.Strict(bm.data)
case bm: BinaryMessage.Streamed =>
BinaryMessage.Streamed(bm.dataStream)
.onComplete:
case Success(binding) =>
println(s"Server is listening on ws://localhost:$port/wsecho")
case Failure(e) =>
println(s"Server failed to start, exception=$e")
system.terminate()
5 changes: 5 additions & 0 deletions benchmark/src/conf.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package benchmark
package conf

val port = 9011
val path = "wsecho"
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package frontier

import ftier.*, ws.*, http.*, server.*
import java.net.InetSocketAddress
import zio.*
import zio.ZIOAppDefault
import zio.ZIO.{attempt, succeed, unit}

object FtierWs extends ZIOAppDefault:
val run =
for
addr <- ZIO.attempt(InetSocketAddress(9011)).orDie
addr <- attempt(InetSocketAddress(conf.port)).orDie
_ <- bind(addr, httpHandler, ServerConf(workers=10))
yield ()

val httpHandler: HttpHandler[Any] =
case UpgradeRequest(r) if r.req.path == "/wsecho" =>
ZIO.succeed(WsResp(r, wsHandler))
case UpgradeRequest(r) if r.req.path == s"/${conf.path}" =>
succeed(WsResp(r, wsHandler))
case _ =>
ZIO.succeed(Response.empty(404))
succeed(Response.empty(404))

val wsHandler: WsHandler[WsContext] =
case msg: Binary => Ws.send(msg)
case _: Close => Ws.close()
case _ => ZIO.unit
case _ => unit
26 changes: 0 additions & 26 deletions benchmark/src/main/scala/ziohttp.scala

This file was deleted.

18 changes: 18 additions & 0 deletions benchmark/src/ziohttp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package benchmark
package ziohttp

import zio.*, http.*, Method.GET, ChannelEvent.Read

object ZiohttpWs extends ZIOAppDefault:
val run =
Server.serve(
Routes(GET / conf.path -> handler(socketApp.toResponse))
).provide(Server.defaultWithPort(conf.port))

val socketApp: WebSocketApp[Any] =
Handler.webSocket: channel =>
channel.receiveAll:
case x @ Read(WebSocketFrame.Binary(_)) =>
channel.send(x)
case _ =>
ZIO.unit
31 changes: 17 additions & 14 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
val scala = "3.3.3"
val zio = "2.1.9"
val gatling = "3.12.0"

lazy val `ftier-root` = project
.in(file("."))
Expand All @@ -11,15 +9,13 @@ lazy val ftier = project
.settings(
scalaVersion := scala
, libraryDependencies ++= Seq(
"dev.zio" %% "zio-streams" % zio
, "dev.zio" %% "zio-test-sbt" % zio % Test
"dev.zio" %% "zio-streams" % "2.1.9"
, "dev.zio" %% "zio-test-sbt" % "2.1.9" % Test
)
, testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
, scalacOptions ++= Seq(
"-feature"
, "-language:postfixOps"
"-language:postfixOps"
, "-language:strictEquality"
, "-new-syntax"
, "-Wunused:imports"
, "-Xfatal-warnings"
, "-Yexplicit-nulls"
Expand All @@ -32,29 +28,36 @@ lazy val demo = project
Compile / scalaSource := baseDirectory.value / "src"
, scalaVersion := scala
, scalacOptions ++= Seq(
"-language:strictEquality"
, "-Yexplicit-nulls"
, "-new-syntax"
"-Wunused:imports"
, "-Xfatal-warnings"
)
, run / fork := true
).dependsOn(ftier)

lazy val benchmark = project
.in(file("benchmark"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio-http" % "3.0.0"
Compile / scalaSource := baseDirectory.value / "src"
, resolvers += "Akka".at("https://repo.akka.io/maven")
, libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http" % "10.6.3"
, "com.typesafe.akka" %% "akka-stream" % "2.9.3"
, "dev.zio" %% "zio-http" % "3.0.0"
)
, scalaVersion := scala
, scalacOptions := Seq(
"-Wunused:imports"
, "-Xfatal-warnings"
)
, run / fork := true
).dependsOn(ftier)

lazy val it = project
.in(file("it"))
.settings(
libraryDependencies ++= Seq(
"io.gatling.highcharts" % "gatling-charts-highcharts" % gatling % "it"
, "io.gatling" % "gatling-test-framework" % gatling % "it"
"io.gatling.highcharts" % "gatling-charts-highcharts" % "3.12.0" % "it"
, "io.gatling" % "gatling-test-framework" % "3.12.0" % "it"
)
, scalaVersion := "2.13.14"
, scalacOptions += "-Xsource:3"
Expand Down

0 comments on commit 7d6aa1b

Please sign in to comment.