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+ }
0 commit comments