Skip to content

Commit

Permalink
Add RoundtripBenchmark (#3291)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Feb 5, 2025
1 parent 307ca54 commit 652d21e
Show file tree
Hide file tree
Showing 3 changed files with 89 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 @@ -636,6 +636,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 @@ -790,7 +828,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 @@ -862,6 +900,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,43 @@
package zio.http.benchmark

import java.util.concurrent.TimeUnit

import zio._

import zio.http._

import org.openjdk.jmh.annotations._

@State(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 requestCall =
ZIO.serviceWithZIO[ZClient[Any, Any, Body, Throwable, Response]] {
_.request(request)
}

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

val runRequests =
ZIO
.foreachDiscard(1 to 1000)(_ => requestCall)
.provide(Client.default >>> ZLayer(ZIO.serviceWith[Client](_.batched)))

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

@Benchmark
def benchmarkSmallDataZioApi(): Unit =
unsafeRun(server.forkDaemon *> runRequests)

}
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 652d21e

Please sign in to comment.