Skip to content

Commit 3e052e8

Browse files
Rest benchmarks
1 parent 091c3e3 commit 3e052e8

File tree

3 files changed

+89
-22
lines changed

3 files changed

+89
-22
lines changed

benchmarks/src/main/scala/io/udash/rest/RestApi.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import scala.concurrent.duration.Duration
1111

1212
private object RestApi {
1313
trait RestTestApi {
14-
@GET def simpleNumbers(size: Int): Task[List[Int]]
14+
@GET def exampleEndpoint(size: Int): Task[List[RestExampleData]]
1515
}
1616

1717
object RestTestApi extends DefaultRestApiCompanion[RestTestApi] {
1818
final class Impl extends RestTestApi {
1919

20-
def simpleNumbers(size: Int): Task[List[Int]] =
21-
Task.eval(Range(0, size).toList)
20+
def exampleEndpoint(size: Int): Task[List[RestExampleData]] =
21+
RestExampleData.generateRandomList(size)
2222
}
2323
}
2424

@@ -30,29 +30,29 @@ private object RestApi {
3030
}
3131

3232

33-
@OutputTimeUnit(TimeUnit.MILLISECONDS)
33+
@OutputTimeUnit(TimeUnit.SECONDS)
3434
@BenchmarkMode(Array(Mode.Throughput))
3535
@State(Scope.Thread)
3636
class RestApi {
3737
implicit def scheduler: Scheduler = Scheduler.global
3838
private final val proxy = RestApi.creteApiProxy()
3939

4040
@Benchmark
41-
def smallNumbersArray(): Unit = {
41+
def smallArray(): Unit = {
4242
waitEndpoint(10)
4343
}
4444

4545
@Benchmark
46-
def mediumNumbersArray(): Unit = {
46+
def mediumArray(): Unit = {
4747
waitEndpoint(200)
4848
}
4949

5050
@Benchmark
51-
def hugeNumbersArray(): Unit = {
51+
def hugeArray(): Unit = {
5252
waitEndpoint(5000)
5353
}
5454

5555
private def waitEndpoint(samples: Int): Unit = {
56-
Await.result(this.proxy.simpleNumbers(samples).runToFuture, Duration.apply(10, TimeUnit.SECONDS))
56+
Await.result(this.proxy.exampleEndpoint(samples).runToFuture, Duration.apply(10, TimeUnit.SECONDS))
5757
}
5858
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.udash.rest
2+
3+
import monix.eval.Task
4+
import monix.reactive.Observable
5+
6+
import scala.util.Random
7+
8+
case class RestExampleData(number: Long, string: String)
9+
10+
object RestExampleData extends RestDataCompanion[RestExampleData]{
11+
private def random() = {
12+
RestExampleData(
13+
Random.nextLong(),
14+
Iterator.continually(Random.nextPrintableChar()).take(200).mkString
15+
)
16+
}
17+
18+
def generateRandomObservable(size: Int): Observable[RestExampleData] =
19+
Observable.fromIterable(Range(0, size).map(_ => RestExampleData.random()))
20+
21+
def generateRandomList(size: Int): Task[List[RestExampleData]] =
22+
Task.eval(Range(0, size).toList.map(_ => RestExampleData.random()))
23+
24+
}

benchmarks/src/main/scala/io/udash/rest/StreamingRestApi.scala

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@ import scala.concurrent.duration.Duration
1212

1313
private object StreamingRestApi {
1414
trait RestTestApi {
15-
@GET def simpleNumbers(size: Int): Observable[Int]
16-
@GET def simpleNumbersWithoutStreaming(size: Int): Task[List[Int]]
15+
@GET def exampleEndpoint(size: Int): Observable[RestExampleData]
16+
17+
@streamingResponseBatchSize(10)
18+
@GET def exampleEndpointBatch10(size: Int): Observable[RestExampleData]
19+
20+
@streamingResponseBatchSize(500)
21+
@GET def exampleEndpointBatch500(size: Int): Observable[RestExampleData]
22+
23+
@GET def exampleEndpointWithoutStreaming(size: Int): Task[List[RestExampleData]]
1724
}
1825

1926
object RestTestApi extends DefaultRestApiCompanion[RestTestApi] {
2027
final class Impl extends RestTestApi {
2128

22-
def simpleNumbers(size: Int): Observable[Int] =
23-
Observable.fromIterable(Range(0, size))
29+
def exampleEndpoint(size: Int): Observable[RestExampleData] =
30+
RestExampleData.generateRandomObservable(size)
2431

25-
def simpleNumbersWithoutStreaming(size: Int): Task[List[Int]] =
26-
Task.eval(Range(0, size).toList)
32+
def exampleEndpointBatch10(size: Int): Observable[RestExampleData] =
33+
RestExampleData.generateRandomObservable(size)
34+
35+
def exampleEndpointBatch500(size: Int): Observable[RestExampleData] =
36+
RestExampleData.generateRandomObservable(size)
37+
38+
def exampleEndpointWithoutStreaming(size: Int): Task[List[RestExampleData]] =
39+
RestExampleData.generateRandomList(size)
2740
}
2841
}
2942

@@ -51,41 +64,71 @@ class StreamingRestApi {
5164

5265

5366
@Benchmark
54-
def smallNumbersArray(): Unit = {
67+
def smallArray(): Unit = {
5568
waitStreamingEndpoint(10)
5669
}
5770

5871
@Benchmark
59-
def mediumNumbersArray(): Unit = {
72+
def mediumArray(): Unit = {
6073
waitStreamingEndpoint(200)
6174
}
6275

6376
@Benchmark
64-
def hugeNumbersArray(): Unit = {
77+
def hugeArray(): Unit = {
6578
waitStreamingEndpoint(5000)
6679
}
6780

6881
@Benchmark
69-
def smallNumbersArrayWithoutStreaming(): Unit = {
82+
def smallArrayBatch10(): Unit = {
83+
wait(this.proxy.exampleEndpointBatch10(10).toListL)
84+
}
85+
86+
@Benchmark
87+
def mediumArrayBatch10(): Unit = {
88+
wait(this.proxy.exampleEndpointBatch10(200).toListL)
89+
}
90+
91+
@Benchmark
92+
def hugeArrayBatch10(): Unit = {
93+
wait(this.proxy.exampleEndpointBatch10(5000).toListL)
94+
}
95+
96+
@Benchmark
97+
def smallArrayBatch500(): Unit = {
98+
wait(this.proxy.exampleEndpointBatch500(10).toListL)
99+
}
100+
101+
@Benchmark
102+
def mediumArrayBatch500(): Unit = {
103+
wait(this.proxy.exampleEndpointBatch500(200).toListL)
104+
}
105+
106+
@Benchmark
107+
def hugeArrayBatch500(): Unit = {
108+
wait(this.proxy.exampleEndpointBatch500(5000).toListL)
109+
}
110+
111+
@Benchmark
112+
def smallArrayWithoutStreaming(): Unit = {
70113
waitEndpointWithoutStreaming(10)
71114
}
72115

73116
@Benchmark
74-
def mediumNumbersArrayWithoutStreaming(): Unit = {
117+
def mediumArrayWithoutStreaming(): Unit = {
75118
waitEndpointWithoutStreaming(200)
76119
}
77120

78121
@Benchmark
79-
def hugeNumbersArrayWithoutStreaming(): Unit = {
122+
def hugeArrayWithoutStreaming(): Unit = {
80123
waitEndpointWithoutStreaming(5000)
81124
}
82125

83126
private def waitEndpointWithoutStreaming(samples: Int): Unit = {
84-
wait(this.proxy.simpleNumbersWithoutStreaming(samples))
127+
wait(this.proxy.exampleEndpointWithoutStreaming(samples))
85128
}
86129

87130
private def waitStreamingEndpoint(samples: Int): Unit = {
88-
wait(this.proxy.simpleNumbers(samples).toListL)
131+
wait(this.proxy.exampleEndpoint(samples).toListL)
89132
}
90133

91134
private def wait[T](task: Task[List[T]]): Unit = {

0 commit comments

Comments
 (0)