Skip to content

Commit f849d00

Browse files
committed
Fixed according to the code review
1 parent c383ade commit f849d00

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

benchmarks/scripts/generate_plots_flow_flatten_merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def draw(data, plt):
4040
gen_marker = next(marker_gen)
4141
res = data[(data.flows == flows)]
4242
plt.plot(res.concurrency, res.score*elements, label="flows={}".format(flows), color=gen_colour, marker=gen_marker)
43-
plt.errorbar(x=res.concurrency, y=res.score*elements, yerr=res.score_error*elements, solid_capstyle='projecting', capsize=5)
43+
plt.errorbar(x=res.concurrency, y=res.score*elements, yerr=res.score_error*elements, solid_capstyle='projecting', capsize=5, color=gen_colour)
4444

4545
data = pd.read_table(input_file, sep=",")
4646
data = data[csv_columns].rename(columns=rename_columns)

benchmarks/src/jmh/kotlin/benchmarks/SemaphoreBenchmark.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package benchmarks
22

3-
import doWork
3+
import doGeomDistrWork
44
import kotlinx.coroutines.*
55
import kotlinx.coroutines.channels.Channel
66
import kotlinx.coroutines.scheduling.ExperimentalCoroutineDispatcher
@@ -50,9 +50,9 @@ open class SemaphoreBenchmark {
5050
jobs += GlobalScope.launch {
5151
repeat(n) {
5252
semaphore.withPermit {
53-
doWork(WORK_INSIDE)
53+
doGeomDistrWork(WORK_INSIDE)
5454
}
55-
doWork(WORK_OUTSIDE)
55+
doGeomDistrWork(WORK_OUTSIDE)
5656
}
5757
}
5858
}
@@ -68,9 +68,9 @@ open class SemaphoreBenchmark {
6868
jobs += GlobalScope.launch {
6969
repeat(n) {
7070
semaphore.send(Unit) // acquire
71-
doWork(WORK_INSIDE)
71+
doGeomDistrWork(WORK_INSIDE)
7272
semaphore.receive() // release
73-
doWork(WORK_OUTSIDE)
73+
doGeomDistrWork(WORK_OUTSIDE)
7474
}
7575
}
7676
}
@@ -85,5 +85,4 @@ enum class SemaphoreBenchDispatcherCreator(val create: (parallelism: Int) -> Cor
8585

8686
private const val WORK_INSIDE = 80
8787
private const val WORK_OUTSIDE = 40
88-
// If you change this variable please be sure that you change variable elements in the corresponding python script as well
8988
private const val BATCH_SIZE = 1000000

benchmarks/src/jmh/kotlin/benchmarks/flow/FlowFlattenMergeBenchmark.kt

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
14
package benchmarks.flow
25

3-
import doWork
6+
import doGeomDistrWork
7+
import kotlinx.coroutines.Dispatchers
48
import kotlinx.coroutines.flow.*
59
import kotlinx.coroutines.runBlocking
610
import org.openjdk.jmh.annotations.*
711
import java.util.concurrent.TimeUnit
812

9-
@Warmup(iterations = 3)
10-
@Measurement(iterations = 10)
13+
/**
14+
* Benchmark to measure performance of [kotlinx.coroutines.flow.FlowKt.flattenMerge].
15+
* In addition to that, it can be considered as a macro benchmark for the [kotlinx.coroutines.sync.Semaphore]
16+
*/
17+
@Warmup(iterations = 3, time = 1)
18+
@Measurement(iterations = 10, time = 1)
1119
@BenchmarkMode(Mode.Throughput)
12-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
20+
@OutputTimeUnit(TimeUnit.SECONDS)
1321
@State(Scope.Benchmark)
1422
@Fork(1)
1523
open class FlowFlattenMergeBenchmark {
@@ -35,18 +43,19 @@ open class FlowFlattenMergeBenchmark {
3543
flow = (1..n).asFlow().map {
3644
flow {
3745
repeat(flowElementsToProcess) {
38-
doWork(WORK)
46+
doGeomDistrWork(WORK)
3947
emit(it)
4048
}
4149
}
4250
}
4351
}
4452

4553
@Benchmark
46-
fun flattenMerge() = runBlocking {
54+
fun flattenMerge() = runBlocking(Dispatchers.Default) {
4755
flow.flattenMerge(concurrency = concurrency).collect()
4856
}
4957
}
5058

59+
// If you change this variable please be sure that you change variable elements in the corresponding python script as well
5160
private const val ELEMENTS = 100_000
5261
private const val WORK = 80

benchmarks/src/main/kotlin/BenchmarkUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import java.util.concurrent.ThreadLocalRandom
66

7-
fun doWork(work: Int) {
7+
fun doGeomDistrWork(work: Int) {
88
// We use geometric distribution here
99
val p = 1.0 / work
1010
val r = ThreadLocalRandom.current()

0 commit comments

Comments
 (0)