Skip to content

Commit

Permalink
Add RoundtripBenchmark (#3291) (#3292)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Feb 6, 2025
1 parent 7c59163 commit 668eb91
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
47 changes: 46 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,44 @@ jobs:
name: Jmh_Main_ProbeContentTypeBenchmark
path: Main_ProbeContentTypeBenchmark.txt

Jmh_RoundtripBenchmark:
name: Jmh RoundtripBenchmark
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
- uses: coursier/setup-action@v1
with:
apps: sbt

- uses: actions/checkout@v4
with:
path: zio-http

- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 11

- name: Benchmark_Main
id: Benchmark_Main
env:
GITHUB_TOKEN: ${{secrets.ACTIONS_PAT}}
run: |
cd zio-http
sed -i -e '$aaddSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")' project/plugins.sbt
cat > Main_RoundtripBenchmark.txt
sbt -no-colors -v "zioHttpBenchmarks/jmh:run -i 3 -wi 3 -f1 -t1 RoundtripBenchmark" | grep -e "thrpt" -e "avgt" >> ../Main_RoundtripBenchmark.txt
- uses: actions/upload-artifact@v4
with:
name: Jmh_Main_RoundtripBenchmark
path: Main_RoundtripBenchmark.txt

Jmh_RoutesBenchmark:
name: Jmh RoutesBenchmark
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
Expand Down Expand Up @@ -794,7 +832,7 @@ jobs:

Jmh_cache:
name: Cache Jmh benchmarks
needs: [Jmh_CachedDateHeaderBenchmark, Jmh_ClientBenchmark, Jmh_CookieDecodeBenchmark, Jmh_EndpointBenchmark, Jmh_HttpCollectEval, Jmh_HttpCombineEval, Jmh_HttpNestedFlatMapEval, Jmh_HttpRouteTextPerf, Jmh_ProbeContentTypeBenchmark, Jmh_RoutesBenchmark, Jmh_SchemeDecodeBenchmark, Jmh_ServerInboundHandlerBenchmark, Jmh_UtilBenchmark]
needs: [Jmh_CachedDateHeaderBenchmark, Jmh_ClientBenchmark, Jmh_CookieDecodeBenchmark, Jmh_EndpointBenchmark, Jmh_HttpCollectEval, Jmh_HttpCombineEval, Jmh_HttpNestedFlatMapEval, Jmh_HttpRouteTextPerf, Jmh_ProbeContentTypeBenchmark, Jmh_RoundtripBenchmark, Jmh_RoutesBenchmark, Jmh_SchemeDecodeBenchmark, Jmh_ServerInboundHandlerBenchmark, Jmh_UtilBenchmark]
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
strategy:
matrix:
Expand Down Expand Up @@ -866,6 +904,13 @@ jobs:
- name: Format_Main_ProbeContentTypeBenchmark
run: cat Main_ProbeContentTypeBenchmark.txt >> Main_benchmarks.txt

- uses: actions/download-artifact@v4
with:
name: Jmh_Main_RoundtripBenchmark

- name: Format_Main_RoundtripBenchmark
run: cat Main_RoundtripBenchmark.txt >> Main_benchmarks.txt

- uses: actions/download-artifact@v4
with:
name: Jmh_Main_RoutesBenchmark
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package zio.http.benchmark

import java.net.http.{HttpClient, HttpRequest, HttpResponse}
import java.net.{ConnectException, URI}
import java.util.concurrent.TimeUnit

import zio._

import zio.http._

import org.openjdk.jmh.annotations._

@State(org.openjdk.jmh.annotations.Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class RoundtripBenchmark {

val routes = Method.GET / "test" -> Handler.ok

val request = Request(
url = url"http://localhost:8080/test",
)

val server = Server.serve(routes).provide(Server.defaultWithPort(8080), ErrorResponseConfig.debugLayer)

unsafeRun(server.forkDaemon)

val runRequestJavaClientAPI =
ZIO.attempt {
val client = HttpClient.newHttpClient()
val request = HttpRequest
.newBuilder()
.uri(URI.create("http://localhost:8080/test"))
.version(HttpClient.Version.HTTP_1_1)
.GET()
.build()
var i = 0
while (i < 1000) {
try client.send(request, HttpResponse.BodyHandlers.discarding())
// client starts requesting before server is ready, ignore errors while server is starting
catch { case _: ConnectException => () }
i += 1
}
}

def unsafeRun[E, A](zio: ZIO[Any, E, A]): Unit = Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe
.run(zio.unit)
.getOrThrowFiberFailure()
}

@Benchmark
def roundtripBenchmarkJavaClientAPI(): Unit =
unsafeRun(runRequestJavaClientAPI)

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ class RoutesBenchmark {
def request: Request = requests(Random.nextInt(requests.size))
val smallDataRequests = Array.fill(REPEAT_N)(request)

def unsafeRun[E, A](zio: ZIO[Any, E, A]): Unit = Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe
.run(zio.unit)
.getOrThrowFiberFailure()
}

val paths2 = ('b' to 'z').inits.map(_.mkString).toList.reverse.tail

val routes2 = Routes.fromIterable(paths2.map(p => Endpoint(Method.GET / p).out[Unit].implementHandler(Handler.unit)))
Expand Down

0 comments on commit 668eb91

Please sign in to comment.