-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
14 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
zio-http-benchmarks/src/main/scala/zio/http/benchmark/RoutesBenchmark.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,67 @@ | ||
package zio.http.benchmark | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
import scala.util.Random | ||
|
||
import zio.{Runtime, Unsafe, ZIO} | ||
|
||
import zio.http.endpoint.Endpoint | ||
import zio.http.{Handler, Method, Request, Routes} | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
@State(Scope.Thread) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class RoutesBenchmark { | ||
|
||
val REPEAT_N = 1000 | ||
|
||
val paths = ('a' to 'z').inits.map(_.mkString).toList.reverse.tail | ||
|
||
val routes = Routes.fromIterable(paths.map(p => Endpoint(Method.GET / p).out[Unit].implementHandler(Handler.unit))) | ||
|
||
val requests = paths.map(p => Request.get(p)) | ||
|
||
def request: Request = requests(Random.nextInt(requests.size)) | ||
|
||
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))) | ||
|
||
val requests2 = requests ++ paths2.map(p => Request.get(p)) | ||
|
||
def request2: Request = requests2(Random.nextInt(requests2.size)) | ||
|
||
@Benchmark | ||
def benchmarkSmallDataZioApi(): Unit = | ||
unsafeRun { | ||
ZIO.collectAllDiscard(1.to(REPEAT_N).map(_ => routes.run(request))) | ||
} | ||
|
||
@Benchmark | ||
def benchmarkSmallDataZioApi2(): Unit = | ||
unsafeRun { | ||
ZIO.collectAllDiscard(1.to(REPEAT_N).map(_ => routes2.run(request2))) | ||
} | ||
|
||
@Benchmark | ||
def notFound1(): Unit = | ||
unsafeRun { | ||
ZIO.collectAllDiscard(1.to(REPEAT_N).map(_ => routes.run(Request.get("not-found")))) | ||
} | ||
|
||
@Benchmark | ||
def notFound2(): Unit = | ||
unsafeRun { | ||
ZIO.collectAllDiscard(1.to(REPEAT_N).map(_ => routes2.run(Request.get("not-found")))) | ||
} | ||
|
||
} |
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