11package io .udash .rest
22
3+ import io .udash .rest .RestExampleData .RestResponseSize
34import io .udash .rest .raw .{RawRest , RestRequest , RestResponse , StreamedRestResponse }
45import monix .eval .Task
56import monix .execution .Scheduler
@@ -12,35 +13,41 @@ import scala.concurrent.duration.Duration
1213
1314private object StreamingRestApi {
1415 trait RestTestApi {
15- @ GET def exampleEndpoint (size : Int ): Observable [RestExampleData ]
16+ @ GET def exampleEndpoint (size : RestResponseSize ): Observable [RestExampleData ]
1617
1718 @ streamingResponseBatchSize(10 )
18- @ GET def exampleEndpointBatch10 (size : Int ): Observable [RestExampleData ]
19+ @ GET def exampleEndpointBatch10 (size : RestResponseSize ): Observable [RestExampleData ]
1920
2021 @ streamingResponseBatchSize(500 )
21- @ GET def exampleEndpointBatch500 (size : Int ): Observable [RestExampleData ]
22+ @ GET def exampleEndpointBatch500 (size : RestResponseSize ): Observable [RestExampleData ]
2223
23- @ GET def exampleEndpointWithoutStreaming (size : Int ): Task [List [RestExampleData ]]
24+ @ GET def exampleEndpointWithoutStreaming (size : RestResponseSize ): Task [List [RestExampleData ]]
2425 }
2526
2627 object RestTestApi extends DefaultRestApiCompanion [RestTestApi ] {
2728 final class Impl extends RestTestApi {
29+ private var responses : Map [RestResponseSize , List [RestExampleData ]] =
30+ Map .empty
2831
29- def exampleEndpoint (size : Int ): Observable [RestExampleData ] =
30- RestExampleData .generateRandomObservable( size)
32+ def exampleEndpoint (size : RestResponseSize ): Observable [RestExampleData ] =
33+ Observable .fromIterable(responses( size) )
3134
32- def exampleEndpointBatch10 (size : Int ): Observable [RestExampleData ] =
33- RestExampleData .generateRandomObservable( size)
35+ def exampleEndpointBatch10 (size : RestResponseSize ): Observable [RestExampleData ] =
36+ Observable .fromIterable(responses( size) )
3437
35- def exampleEndpointBatch500 (size : Int ): Observable [RestExampleData ] =
36- RestExampleData .generateRandomObservable( size)
38+ def exampleEndpointBatch500 (size : RestResponseSize ): Observable [RestExampleData ] =
39+ Observable .fromIterable(responses( size) )
3740
38- def exampleEndpointWithoutStreaming (size : Int ): Task [List [RestExampleData ]] =
39- RestExampleData .generateRandomList(size)
41+ def exampleEndpointWithoutStreaming (size : RestResponseSize ): Task [List [RestExampleData ]] =
42+ Task .eval(responses(size))
43+
44+ def generateResponses (): Unit = {
45+ this .responses = RestResponseSize .values.map(size => size -> RestExampleData .generateRandomList(size)).toMap
46+ }
4047 }
4148 }
4249
43- private def creteApiProxy (): RestTestApi = {
50+ private def creteApiProxy (): ( RestTestApi . Impl , RestTestApi ) = {
4451 val apiImpl = new RestTestApi .Impl ()
4552 val streamingServerHandle = RawRest .asHandleRequestWithStreaming[RestTestApi ](apiImpl)
4653 val streamingClientHandler = new RawRest .RestRequestHandler {
@@ -50,84 +57,88 @@ private object StreamingRestApi {
5057 override def handleRequestStream (request : RestRequest ): Task [StreamedRestResponse ] =
5158 streamingServerHandle(request).map(_.asInstanceOf [StreamedRestResponse ])
5259 }
53- RawRest .fromHandleRequestWithStreaming[RestTestApi ](streamingClientHandler)
60+ (apiImpl, RawRest .fromHandleRequestWithStreaming[RestTestApi ](streamingClientHandler) )
5461 }
5562}
5663
5764
58- @ OutputTimeUnit (TimeUnit .MILLISECONDS )
65+ @ OutputTimeUnit (TimeUnit .SECONDS )
5966@ BenchmarkMode (Array (Mode .Throughput ))
6067@ State (Scope .Thread )
6168class StreamingRestApi {
6269 implicit def scheduler : Scheduler = Scheduler .global
63- private final val proxy = StreamingRestApi .creteApiProxy()
70+ private final val (impl, proxy) = StreamingRestApi .creteApiProxy()
6471
72+ @ Setup (Level .Trial )
73+ def setup (): Unit = {
74+ this .impl.generateResponses()
75+ }
6576
6677 @ Benchmark
6778 def smallArray (): Unit = {
68- waitStreamingEndpoint(10 )
79+ waitStreamingEndpoint(RestResponseSize . Small )
6980 }
7081
7182 @ Benchmark
7283 def mediumArray (): Unit = {
73- waitStreamingEndpoint(200 )
84+ waitStreamingEndpoint(RestResponseSize . Medium )
7485 }
7586
7687 @ Benchmark
7788 def hugeArray (): Unit = {
78- waitStreamingEndpoint(5000 )
89+ waitStreamingEndpoint(RestResponseSize . Huge )
7990 }
8091
8192 @ Benchmark
8293 def smallArrayBatch10 (): Unit = {
83- wait(this .proxy.exampleEndpointBatch10(10 ).toListL)
94+ wait(this .proxy.exampleEndpointBatch10(RestResponseSize . Small ).toListL)
8495 }
8596
8697 @ Benchmark
8798 def mediumArrayBatch10 (): Unit = {
88- wait(this .proxy.exampleEndpointBatch10(200 ).toListL)
99+ wait(this .proxy.exampleEndpointBatch10(RestResponseSize . Medium ).toListL)
89100 }
90101
91102 @ Benchmark
92103 def hugeArrayBatch10 (): Unit = {
93- wait(this .proxy.exampleEndpointBatch10(5000 ).toListL)
104+ wait(this .proxy.exampleEndpointBatch10(RestResponseSize . Huge ).toListL)
94105 }
95106
96107 @ Benchmark
97108 def smallArrayBatch500 (): Unit = {
98- wait(this .proxy.exampleEndpointBatch500(10 ).toListL)
109+ wait(this .proxy.exampleEndpointBatch500(RestResponseSize . Small ).toListL)
99110 }
100111
101112 @ Benchmark
102113 def mediumArrayBatch500 (): Unit = {
103- wait(this .proxy.exampleEndpointBatch500(200 ).toListL)
114+ wait(this .proxy.exampleEndpointBatch500(RestResponseSize . Medium ).toListL)
104115 }
105116
106117 @ Benchmark
107118 def hugeArrayBatch500 (): Unit = {
108- wait(this .proxy.exampleEndpointBatch500(5000 ).toListL)
119+ wait(this .proxy.exampleEndpointBatch500(RestResponseSize . Huge ).toListL)
109120 }
110121
111122 @ Benchmark
112123 def smallArrayWithoutStreaming (): Unit = {
113- waitEndpointWithoutStreaming(10 )
124+ waitEndpointWithoutStreaming(RestResponseSize . Small )
114125 }
115126
116127 @ Benchmark
117128 def mediumArrayWithoutStreaming (): Unit = {
118- waitEndpointWithoutStreaming(200 )
129+ waitEndpointWithoutStreaming(RestResponseSize . Medium )
119130 }
120131
121132 @ Benchmark
122133 def hugeArrayWithoutStreaming (): Unit = {
123- waitEndpointWithoutStreaming(5000 )
134+ waitEndpointWithoutStreaming(RestResponseSize . Huge )
124135 }
125136
126- private def waitEndpointWithoutStreaming (samples : Int ): Unit = {
137+ private def waitEndpointWithoutStreaming (samples : RestResponseSize ): Unit = {
127138 wait(this .proxy.exampleEndpointWithoutStreaming(samples))
128139 }
129140
130- private def waitStreamingEndpoint (samples : Int ): Unit = {
141+ private def waitStreamingEndpoint (samples : RestResponseSize ): Unit = {
131142 wait(this .proxy.exampleEndpoint(samples).toListL)
132143 }
133144
0 commit comments