11package io .udash .rest
22
3+ import com .avsystem .commons .serialization .json .JsonStringOutput
34import io .udash .rest .RestExampleData .RestResponseSize
45import io .udash .rest .raw .{RawRest , RestRequest , RestResponse , StreamedRestResponse }
56import monix .eval .Task
67import monix .execution .Scheduler
78import monix .reactive .Observable
89import org .openjdk .jmh .annotations .*
910
11+ import java .nio .charset .StandardCharsets
1012import java .util .concurrent .TimeUnit
1113import scala .concurrent .Await
1214import scala .concurrent .duration .Duration
1315
1416private object StreamingRestApi {
1517 trait RestTestApi {
1618 @ GET def exampleEndpoint (size : RestResponseSize ): Observable [RestExampleData ]
19+ @ GET def exampleEndpointBinary (size : RestResponseSize ): Observable [Array [Byte ]]
1720
1821 @ streamingResponseBatchSize(10 )
1922 @ GET def exampleEndpointBatch10 (size : RestResponseSize ): Observable [RestExampleData ]
2023
24+ @ streamingResponseBatchSize(10 )
25+ @ GET def exampleEndpointBatch10Binary (size : RestResponseSize ): Observable [Array [Byte ]]
26+
2127 @ streamingResponseBatchSize(500 )
2228 @ GET def exampleEndpointBatch500 (size : RestResponseSize ): Observable [RestExampleData ]
2329
30+ @ streamingResponseBatchSize(500 )
31+ @ GET def exampleEndpointBatch500Binary (size : RestResponseSize ): Observable [Array [Byte ]]
32+
2433 @ GET def exampleEndpointWithoutStreaming (size : RestResponseSize ): Task [List [RestExampleData ]]
2534 }
2635
2736 object RestTestApi extends DefaultRestApiCompanion [RestTestApi ] {
2837 final class Impl extends RestTestApi {
29- private var responses : Map [RestResponseSize , List [RestExampleData ]] =
30- Map .empty
38+ private var responses : Map [RestResponseSize , List [RestExampleData ]] = Map .empty
3139
3240 def exampleEndpoint (size : RestResponseSize ): Observable [RestExampleData ] =
33- Observable .fromIterable(responses(size))
41+ Observable .fromIterable(getResponse(size))
42+
43+ def exampleEndpointBinary (size : RestResponseSize ): Observable [Array [Byte ]] =
44+ getResponseBinary(size)
3445
3546 def exampleEndpointBatch10 (size : RestResponseSize ): Observable [RestExampleData ] =
36- Observable .fromIterable(responses(size))
47+ Observable .fromIterable(getResponse(size))
48+
49+ def exampleEndpointBatch10Binary (size : RestResponseSize ): Observable [Array [Byte ]] =
50+ getResponseBinary(size)
3751
3852 def exampleEndpointBatch500 (size : RestResponseSize ): Observable [RestExampleData ] =
39- Observable .fromIterable(responses(size))
53+ Observable .fromIterable(getResponse(size))
54+
55+ def exampleEndpointBatch500Binary (size : RestResponseSize ): Observable [Array [Byte ]] =
56+ getResponseBinary(size)
4057
4158 def exampleEndpointWithoutStreaming (size : RestResponseSize ): Task [List [RestExampleData ]] =
42- Task .eval(responses(size))
59+ Task .eval(getResponse(size))
60+
61+ private def getResponse (size : RestResponseSize ): List [RestExampleData ] =
62+ responses(size)
63+
64+ private def getResponseBinary (size : RestResponseSize ): Observable [Array [Byte ]] =
65+ Observable .fromIterable(getResponse(size)).map(JsonStringOutput .write(_).getBytes(StandardCharsets .UTF_8 ))
4366
44- def generateResponses (): Unit = {
67+ def generateResponses (): Unit =
4568 this .responses = RestResponseSize .values.map(size => size -> RestExampleData .generateRandomList(size)).toMap
46- }
4769 }
4870 }
4971
@@ -76,48 +98,93 @@ class StreamingRestApi {
7698 }
7799
78100 @ Benchmark
79- def smallArray (): Unit = {
101+ def smallArrayJsonList (): Unit = {
80102 waitStreamingEndpoint(RestResponseSize .Small )
81103 }
82104
83105 @ Benchmark
84- def mediumArray (): Unit = {
106+ def mediumArrayJsonList (): Unit = {
85107 waitStreamingEndpoint(RestResponseSize .Medium )
86108 }
87109
88110 @ Benchmark
89- def hugeArray (): Unit = {
111+ def hugeArrayJsonList (): Unit = {
90112 waitStreamingEndpoint(RestResponseSize .Huge )
91113 }
92114
93115 @ Benchmark
94- def smallArrayBatch10 (): Unit = {
95- wait(this .proxy.exampleEndpointBatch10(RestResponseSize .Small ).toListL)
116+ def smallArrayBinary (): Unit = {
117+ waitStreamingEndpointBinary(RestResponseSize .Small )
118+ }
119+
120+ @ Benchmark
121+ def mediumArrayBinary (): Unit = {
122+ waitStreamingEndpointBinary(RestResponseSize .Medium )
123+ }
124+
125+ @ Benchmark
126+ def hugeArrayBinary (): Unit = {
127+ waitStreamingEndpointBinary(RestResponseSize .Huge )
128+ }
129+
130+ @ Benchmark
131+ def smallArrayBatch10JsonList (): Unit = {
132+ waitObservable(this .proxy.exampleEndpointBatch10(RestResponseSize .Small ))
133+ }
134+
135+ @ Benchmark
136+ def mediumArrayBatch10JsonList (): Unit = {
137+ waitObservable(this .proxy.exampleEndpointBatch10(RestResponseSize .Medium ))
96138 }
97139
98140 @ Benchmark
99- def mediumArrayBatch10 (): Unit = {
100- wait (this .proxy.exampleEndpointBatch10(RestResponseSize .Medium ).toListL )
141+ def hugeArrayBatch10JsonList (): Unit = {
142+ waitObservable (this .proxy.exampleEndpointBatch10(RestResponseSize .Huge ) )
101143 }
102144
103145 @ Benchmark
104- def hugeArrayBatch10 (): Unit = {
105- wait (this .proxy.exampleEndpointBatch10 (RestResponseSize .Huge ).toListL )
146+ def smallArrayBatch10Binary (): Unit = {
147+ waitObservable (this .proxy.exampleEndpointBatch10Binary (RestResponseSize .Small ) )
106148 }
107149
108150 @ Benchmark
109- def smallArrayBatch500 (): Unit = {
110- wait (this .proxy.exampleEndpointBatch500 (RestResponseSize .Small ).toListL )
151+ def mediumArrayBatch10Binary (): Unit = {
152+ waitObservable (this .proxy.exampleEndpointBatch10Binary (RestResponseSize .Medium ) )
111153 }
112154
113155 @ Benchmark
114- def mediumArrayBatch500 (): Unit = {
115- wait (this .proxy.exampleEndpointBatch500 (RestResponseSize .Medium ).toListL )
156+ def hugeArrayBatch10Binary (): Unit = {
157+ waitObservable (this .proxy.exampleEndpointBatch10Binary (RestResponseSize .Huge ) )
116158 }
117159
118160 @ Benchmark
119- def hugeArrayBatch500 (): Unit = {
120- wait(this .proxy.exampleEndpointBatch500(RestResponseSize .Huge ).toListL)
161+ def smallArrayBatch500JsonList (): Unit = {
162+ waitObservable(this .proxy.exampleEndpointBatch500(RestResponseSize .Small ))
163+ }
164+
165+ @ Benchmark
166+ def mediumArrayBatch500JsonList (): Unit = {
167+ waitObservable(this .proxy.exampleEndpointBatch500(RestResponseSize .Medium ))
168+ }
169+
170+ @ Benchmark
171+ def hugeArrayBatch500JsonList (): Unit = {
172+ waitObservable(this .proxy.exampleEndpointBatch500(RestResponseSize .Huge ))
173+ }
174+
175+ @ Benchmark
176+ def smallArrayBatch500Binary (): Unit = {
177+ waitObservable(this .proxy.exampleEndpointBatch500Binary(RestResponseSize .Small ))
178+ }
179+
180+ @ Benchmark
181+ def mediumArrayBatch500Binary (): Unit = {
182+ waitObservable(this .proxy.exampleEndpointBatch500Binary(RestResponseSize .Medium ))
183+ }
184+
185+ @ Benchmark
186+ def hugeArrayBatch500Binary (): Unit = {
187+ waitObservable(this .proxy.exampleEndpointBatch500Binary(RestResponseSize .Huge ))
121188 }
122189
123190 @ Benchmark
@@ -135,15 +202,18 @@ class StreamingRestApi {
135202 waitEndpointWithoutStreaming(RestResponseSize .Huge )
136203 }
137204
138- private def waitEndpointWithoutStreaming (samples : RestResponseSize ): Unit = {
205+ private def waitEndpointWithoutStreaming (samples : RestResponseSize ): Unit =
139206 wait(this .proxy.exampleEndpointWithoutStreaming(samples))
140- }
141207
142- private def waitStreamingEndpoint (samples : RestResponseSize ): Unit = {
143- wait(this .proxy.exampleEndpoint(samples).toListL)
144- }
208+ private def waitStreamingEndpoint (samples : RestResponseSize ): Unit =
209+ wait(this .proxy.exampleEndpoint(samples).completedL)
145210
146- private def wait [T ](task : Task [List [T ]]): Unit = {
147- Await .result(task.runToFuture, Duration .apply(10 , TimeUnit .SECONDS ))
148- }
149- }
211+ private def waitStreamingEndpointBinary (samples : RestResponseSize ): Unit =
212+ wait(this .proxy.exampleEndpointBinary(samples).completedL)
213+
214+ private def wait [T ](task : Task [T ]): Unit =
215+ Await .result(task.runToFuture, Duration .apply(15 , TimeUnit .SECONDS ))
216+
217+ private def waitObservable [T ](obs : Observable [T ]): Unit =
218+ Await .result(obs.completedL.runToFuture, Duration .apply(15 , TimeUnit .SECONDS ))
219+ }
0 commit comments