-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the Client's performance for non-stream bodies #2919
Merged
jdegoes
merged 17 commits into
zio:main
from
kyri-petrou:improve-client-non-streaming-performance
Jun 20, 2024
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dc2237e
Fix memory leak in netty connection pool
kyri-petrou e4fbc0e
Use a forked effect to monitor the close future for client requests
kyri-petrou d86916f
Merge branch 'main' into fix-netty-connection-pool-memory-leak
kyri-petrou 7d40cd3
Use Netty's future listener again
kyri-petrou 97b6e96
fmt
kyri-petrou 781b121
Remove suspendSucceed
kyri-petrou 7958749
Improve non-streaming performance of Client
kyri-petrou 76866d1
Cleanups in AsyncBodyReader
kyri-petrou 91956fd
One more improvement
kyri-petrou 54942fa
Merge branch 'main' into improve-client-non-streaming-performance
kyri-petrou 90ee08b
Reimplement using buffering within `AsyncBodyReader`
kyri-petrou d694b59
Add benchmarks
kyri-petrou e5ff979
Fix url interpolator macro
kyri-petrou af5d7d6
Merge branch 'main' into improve-client-non-streaming-performance
kyri-petrou 407be4f
Re-generate GH workflow
kyri-petrou f2a3483
Merge remote-tracking branch 'kyri-petrou/improve-client-non-streamin…
kyri-petrou fe2cdf5
fmt
kyri-petrou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
zio-http-benchmarks/src/main/scala/zhttp.benchmarks/ClientBenchmark.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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package zhttp.benchmarks | ||
|
||
import org.openjdk.jmh.annotations._ | ||
import zio._ | ||
import zio.http._ | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.annotation.nowarn | ||
|
||
@nowarn | ||
@State(org.openjdk.jmh.annotations.Scope.Benchmark) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@Warmup(iterations = 3, time = 3) | ||
@Measurement(iterations = 3, time = 3) | ||
@Fork(1) | ||
class ClientBenchmark { | ||
private val random = scala.util.Random | ||
random.setSeed(42) | ||
|
||
private implicit val unsafe: Unsafe = Unsafe.unsafe(identity) | ||
|
||
@Param(Array("small", "large")) | ||
var path: String = _ | ||
|
||
private val smallString = "Hello!".getBytes | ||
private val largeString = random.alphanumeric.take(10000).mkString.getBytes | ||
|
||
private val smallRequest = Request(url = url"http://0.0.0.0:8080/small") | ||
private val largeRequest = Request(url = url"http://0.0.0.0:8080/large") | ||
|
||
private val smallResponse = Response(status = Status.Ok, body = Body.fromArray(smallString)) | ||
private val largeResponse = Response(status = Status.Ok, body = Body.fromArray(largeString)) | ||
|
||
private val smallRoute = Route.route(Method.GET / "small")(handler(smallResponse)) | ||
private val largeRoute = Route.route(Method.GET / "large")(handler(largeResponse)) | ||
|
||
private val shutdownResponse = Response.text("shutting down") | ||
|
||
private def shutdownRoute(shutdownSignal: Promise[Nothing, Unit]) = | ||
Route.route(Method.GET / "shutdown")(handler(shutdownSignal.succeed(()).as(shutdownResponse))) | ||
|
||
private def http(shutdownSignal: Promise[Nothing, Unit]) = | ||
Routes(smallRoute, largeRoute, shutdownRoute(shutdownSignal)) | ||
|
||
private val rtm = Runtime.unsafe.fromLayer(ZClient.default) | ||
private val runtime = rtm.unsafe | ||
|
||
private def run(f: RIO[Client, Any]): Any = runtime.run(f).getOrThrow() | ||
|
||
@Setup(Level.Trial) | ||
def setup(): Unit = { | ||
val startServer: Task[Unit] = (for { | ||
shutdownSignal <- Promise.make[Nothing, Unit] | ||
fiber <- Server.serve(http(shutdownSignal)).fork | ||
_ <- shutdownSignal.await *> fiber.interrupt | ||
} yield ()).provideLayer(Server.default) | ||
|
||
val waitForServerStarted: Task[Unit] = (for { | ||
client <- ZIO.service[Client] | ||
_ <- client.request(smallRequest) | ||
} yield ()).provide(ZClient.default, zio.Scope.default) | ||
|
||
run(startServer.forkDaemon *> waitForServerStarted.retry(Schedule.fixed(1.second))) | ||
} | ||
|
||
@TearDown(Level.Trial) | ||
def tearDown(): Unit = { | ||
val stopServer = (for { | ||
client <- ZIO.service[Client] | ||
_ <- client.request(Request(url = url"http://localhost:8080/shutdown")) | ||
} yield ()).provide(ZClient.default, zio.Scope.default) | ||
run(stopServer) | ||
rtm.shutdown0() | ||
} | ||
|
||
@Benchmark | ||
@OperationsPerInvocation(100) | ||
def zhttpChunkBenchmark(): Any = run { | ||
val req = if (path == "small") smallRequest else largeRequest | ||
ZIO.serviceWithZIO[Client] { client => | ||
ZIO.scoped(client.request(req).flatMap(_.body.asChunk)).repeatN(100) | ||
} | ||
} | ||
|
||
@Benchmark | ||
@OperationsPerInvocation(100) | ||
def zhttpStreamToChunkBenchmark(): Any = run { | ||
val req = if (path == "small") smallRequest else largeRequest | ||
ZIO.serviceWithZIO[Client] { client => | ||
ZIO.scoped(client.request(req).flatMap(_.body.asStream.runCollect)).repeatN(100) | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realised that these macros did not work while adding the benchmarks because the
fromAbsoluteURI
method is package-private.I decided to just fix it here instead so that I could use them in the benchmarks