Skip to content

Commit 091c3e3

Browse files
Rest benchmarks
1 parent 77ecfe0 commit 091c3e3

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.udash.rest
2+
3+
import io.udash.rest.raw.RawRest
4+
import monix.eval.Task
5+
import monix.execution.Scheduler
6+
import org.openjdk.jmh.annotations.*
7+
8+
import java.util.concurrent.TimeUnit
9+
import scala.concurrent.Await
10+
import scala.concurrent.duration.Duration
11+
12+
private object RestApi {
13+
trait RestTestApi {
14+
@GET def simpleNumbers(size: Int): Task[List[Int]]
15+
}
16+
17+
object RestTestApi extends DefaultRestApiCompanion[RestTestApi] {
18+
final class Impl extends RestTestApi {
19+
20+
def simpleNumbers(size: Int): Task[List[Int]] =
21+
Task.eval(Range(0, size).toList)
22+
}
23+
}
24+
25+
private def creteApiProxy(): RestTestApi = {
26+
val apiImpl = new RestTestApi.Impl()
27+
val handler = RawRest.asHandleRequest[RestTestApi](apiImpl)
28+
RawRest.fromHandleRequest[RestTestApi](handler)
29+
}
30+
}
31+
32+
33+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
34+
@BenchmarkMode(Array(Mode.Throughput))
35+
@State(Scope.Thread)
36+
class RestApi {
37+
implicit def scheduler: Scheduler = Scheduler.global
38+
private final val proxy = RestApi.creteApiProxy()
39+
40+
@Benchmark
41+
def smallNumbersArray(): Unit = {
42+
waitEndpoint(10)
43+
}
44+
45+
@Benchmark
46+
def mediumNumbersArray(): Unit = {
47+
waitEndpoint(200)
48+
}
49+
50+
@Benchmark
51+
def hugeNumbersArray(): Unit = {
52+
waitEndpoint(5000)
53+
}
54+
55+
private def waitEndpoint(samples: Int): Unit = {
56+
Await.result(this.proxy.simpleNumbers(samples).runToFuture, Duration.apply(10, TimeUnit.SECONDS))
57+
}
58+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.udash.rest
2+
3+
import io.udash.rest.raw.{RawRest, RestRequest, RestResponse, StreamedRestResponse}
4+
import monix.eval.Task
5+
import monix.execution.Scheduler
6+
import monix.reactive.Observable
7+
import org.openjdk.jmh.annotations.*
8+
9+
import java.util.concurrent.TimeUnit
10+
import scala.concurrent.Await
11+
import scala.concurrent.duration.Duration
12+
13+
private object StreamingRestApi {
14+
trait RestTestApi {
15+
@GET def simpleNumbers(size: Int): Observable[Int]
16+
@GET def simpleNumbersWithoutStreaming(size: Int): Task[List[Int]]
17+
}
18+
19+
object RestTestApi extends DefaultRestApiCompanion[RestTestApi] {
20+
final class Impl extends RestTestApi {
21+
22+
def simpleNumbers(size: Int): Observable[Int] =
23+
Observable.fromIterable(Range(0, size))
24+
25+
def simpleNumbersWithoutStreaming(size: Int): Task[List[Int]] =
26+
Task.eval(Range(0, size).toList)
27+
}
28+
}
29+
30+
private def creteApiProxy(): RestTestApi = {
31+
val apiImpl = new RestTestApi.Impl()
32+
val streamingServerHandle = RawRest.asHandleRequestWithStreaming[RestTestApi](apiImpl)
33+
val streamingClientHandler = new RawRest.RestRequestHandler {
34+
override def handleRequest(request: RestRequest): Task[RestResponse] =
35+
streamingServerHandle(request).map(_.asInstanceOf[RestResponse])
36+
37+
override def handleRequestStream(request: RestRequest): Task[StreamedRestResponse] =
38+
streamingServerHandle(request).map(_.asInstanceOf[StreamedRestResponse])
39+
}
40+
RawRest.fromHandleRequestWithStreaming[RestTestApi](streamingClientHandler)
41+
}
42+
}
43+
44+
45+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
46+
@BenchmarkMode(Array(Mode.Throughput))
47+
@State(Scope.Thread)
48+
class StreamingRestApi {
49+
implicit def scheduler: Scheduler = Scheduler.global
50+
private final val proxy = StreamingRestApi.creteApiProxy()
51+
52+
53+
@Benchmark
54+
def smallNumbersArray(): Unit = {
55+
waitStreamingEndpoint(10)
56+
}
57+
58+
@Benchmark
59+
def mediumNumbersArray(): Unit = {
60+
waitStreamingEndpoint(200)
61+
}
62+
63+
@Benchmark
64+
def hugeNumbersArray(): Unit = {
65+
waitStreamingEndpoint(5000)
66+
}
67+
68+
@Benchmark
69+
def smallNumbersArrayWithoutStreaming(): Unit = {
70+
waitEndpointWithoutStreaming(10)
71+
}
72+
73+
@Benchmark
74+
def mediumNumbersArrayWithoutStreaming(): Unit = {
75+
waitEndpointWithoutStreaming(200)
76+
}
77+
78+
@Benchmark
79+
def hugeNumbersArrayWithoutStreaming(): Unit = {
80+
waitEndpointWithoutStreaming(5000)
81+
}
82+
83+
private def waitEndpointWithoutStreaming(samples: Int): Unit = {
84+
wait(this.proxy.simpleNumbersWithoutStreaming(samples))
85+
}
86+
87+
private def waitStreamingEndpoint(samples: Int): Unit = {
88+
wait(this.proxy.simpleNumbers(samples).toListL)
89+
}
90+
91+
private def wait[T](task: Task[List[T]]): Unit = {
92+
Await.result(task.runToFuture, Duration.apply(10, TimeUnit.SECONDS))
93+
}
94+
}

build.sbt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import org.openqa.selenium.firefox.{FirefoxDriverLogLevel, FirefoxOptions}
33
import org.scalajs.jsdependencies.sbtplugin.JSModuleID
44
import org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv
55
import org.scalajs.jsenv.selenium.SeleniumJSEnv
6+
import pl.project13.scala.sbt.JmhPlugin
67

78
name := "udash"
89

@@ -483,4 +484,12 @@ lazy val `guide-selenium` =
483484
`guide-homepage` / Compile / compileStatics,
484485
`guide-guide` / Compile / compileStatics,
485486
).value
486-
)
487+
)
488+
489+
lazy val benchmarksJVM = project.in(file("benchmarks"))
490+
.enablePlugins(JmhPlugin)
491+
.dependsOn(jvmLibraries.map(p => p: ClasspathDep[ProjectReference]): _*)
492+
.settings(
493+
commonSettings,
494+
noPublishSettings,
495+
)

project/plugins.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.11.1")
1212

1313
// Deployment configuration
1414
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.1")
15-
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.12.2")
15+
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.12.2")
16+
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")

0 commit comments

Comments
 (0)