-
Notifications
You must be signed in to change notification settings - Fork 518
/
WorkStealingBenchmark.scala
173 lines (153 loc) · 4.61 KB
/
WorkStealingBenchmark.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright 2020-2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cats.effect.benchmarks
import cats.effect.IO
import cats.effect.unsafe._
import cats.syntax.all._
import scala.concurrent.ExecutionContext
import java.util.concurrent.{Executors, TimeUnit}
import java.util.concurrent.atomic.AtomicInteger
import org.openjdk.jmh.annotations._
/**
* To do comparative benchmarks between versions:
*
* benchmarks/run-benchmark WorkStealingBenchmark
*
* This will generate results in `benchmarks/results`.
*
* Or to run the benchmark from within sbt:
*
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.WorkStealingBenchmark
*
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread".
* Please note that benchmarks should be usually executed at least in
* 10 iterations (as a rule of thumb), but more is better.
*/
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.MINUTES)
class WorkStealingBenchmark {
@Param(Array("1000000"))
var size: Int = _
def schedulingBenchmark(implicit runtime: IORuntime): Int = {
def fiber(i: Int): IO[Int] =
IO.cede.flatMap { _ =>
IO(i).flatMap { j =>
IO.cede.flatMap { _ =>
if (j > 10000)
IO.cede.flatMap(_ => IO.pure(j))
else
IO.cede.flatMap(_ => fiber(j + 1))
}
}
}
List
.range(0, size)
.traverse(fiber(_).start)
.flatMap(_.traverse(_.joinWithNever))
.map(_.sum)
.unsafeRunSync()
}
@Benchmark
def scheduling(): Int = {
import cats.effect.unsafe.implicits.global
schedulingBenchmark
}
def allocBenchmark(implicit runtime: IORuntime): Int = {
def allocation(n: Int): IO[Array[AnyRef]] =
IO {
val size = math.max(100, math.min(n, 2000))
val array = new Array[AnyRef](size)
for (i <- (0 until size)) {
array(i) = new AnyRef()
}
array
}
def sum(array: Array[AnyRef]): IO[Int] =
IO {
array.map(_.hashCode()).sum
}
def fiber(i: Int): IO[Int] =
IO.cede.flatMap { _ =>
allocation(i).flatMap { arr =>
IO.cede.flatMap(_ => sum(arr)).flatMap { _ =>
if (i > 1000)
IO.cede.flatMap(_ => IO.pure(i))
else
IO.cede.flatMap(_ => fiber(i + 1))
}
}
}
List
.range(0, 2500)
.traverse(_ => fiber(0).start)
.flatMap(_.traverse(_.joinWithNever))
.map(_.sum)
.unsafeRunSync()
}
@Benchmark
def alloc(): Int = {
import cats.effect.unsafe.implicits.global
allocBenchmark
}
lazy val manyThreadsRuntime: IORuntime = {
val (blocking, blockDown) = {
val threadCount = new AtomicInteger(0)
val executor = Executors.newCachedThreadPool { (r: Runnable) =>
val t = new Thread(r)
t.setName(s"io-blocking-${threadCount.getAndIncrement()}")
t.setDaemon(true)
t
}
(ExecutionContext.fromExecutor(executor), () => executor.shutdown())
}
val (scheduler, schedDown) = {
val executor = Executors.newSingleThreadScheduledExecutor { r =>
val t = new Thread(r)
t.setName("io-scheduler")
t.setDaemon(true)
t.setPriority(Thread.MAX_PRIORITY)
t
}
(Scheduler.fromScheduledExecutor(executor), () => executor.shutdown())
}
val compute = new WorkStealingThreadPool(256, "io-compute", manyThreadsRuntime)
val cancellationCheckThreshold =
System.getProperty("cats.effect.cancellation.check.threshold", "512").toInt
new IORuntime(
compute,
blocking,
scheduler,
() => (),
IORuntimeConfig(
cancellationCheckThreshold,
System
.getProperty("cats.effect.auto.yield.threshold.multiplier", "2")
.toInt * cancellationCheckThreshold
),
internalShutdown = () => {
compute.shutdown()
blockDown()
schedDown()
}
)
}
@Benchmark
def manyThreadsSchedulingBenchmarks(): Int = {
implicit val runtime = manyThreadsRuntime
schedulingBenchmark
}
}