Skip to content

Commit 243beea

Browse files
committed
Fixed according to the code review
1 parent 536c9a5 commit 243beea

File tree

3 files changed

+46
-49
lines changed

3 files changed

+46
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
11
import pandas as pd
2-
import random
32
import matplotlib.pyplot as plt
43
from matplotlib.ticker import FormatStrFormatter
5-
from matplotlib.colors import ListedColormap
6-
from matplotlib.backends.backend_pdf import PdfPages
74

85
inputFile = "jmh-result.csv"
9-
outputFile = "flatten-merge-plots.pdf"
6+
outputFile = "flatten-merge-plots.svg"
107
elements = 10000
11-
flattenMergeBenchmarkName="benchmarks.flow.FlattenMergeBenchmark.flattenMerge"
12-
13-
def nextColour():
14-
colours = ['black', 'silver', 'red', 'gold', 'sienna', 'olivedrab', 'lightseagreen', 'navy', 'blue', 'm', 'crimson', 'yellow', 'orangered', 'slateblue', 'aqua']
15-
16-
for colour in colours:
17-
yield colour
18-
19-
def nextMarker():
20-
markers = ['.', 'v', '^', '1', '2', '8', 'p', 'P', 'x', 'D', 'd', 's']
21-
22-
for marker in markers:
23-
yield marker
24-
25-
def draw(data, ax):
26-
data = data[(data.benchmark == flattenMergeBenchmarkName)]
27-
28-
ax.set_xscale('log', basex=2)
29-
ax.xaxis.set_major_formatter(FormatStrFormatter('%0.f'))
30-
ax.grid(linewidth='0.5', color='lightgray')
31-
ax.set_ylabel(data.unit.unique()[0])
32-
ax.set_xlabel('parallelism')
33-
ax.set_xticks(data.concurrency.unique())
34-
35-
colourGen = nextColour()
36-
markerGen = nextMarker()
8+
benchmarkName="benchmarks.flow.FlattenMergeBenchmark.flattenMerge"
9+
10+
markers = ['.', 'v', '^', '1', '2', '8', 'p', 'P', 'x', 'D', 'd', 's']
11+
colours = ['black', 'silver', 'red', 'gold', 'sienna', 'olivedrab', 'lightseagreen', 'navy', 'blue', 'm', 'crimson', 'yellow', 'orangered', 'slateblue', 'aqua']
12+
13+
def next_colour():
14+
i = 0
15+
while True:
16+
yield colours[i % len(colours)]
17+
i += 1
18+
19+
def next_marker():
20+
i = 0
21+
while True:
22+
yield markers[i % len(markers)]
23+
i += 1
24+
25+
def draw(data, plt):
26+
data = data[(data.benchmark == benchmarkName)]
27+
28+
plt.xscale('log', basex=2)
29+
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%0.f'))
30+
plt.grid(linewidth='0.5', color='lightgray')
31+
plt.ylabel(data.unit.unique()[0])
32+
plt.xlabel('parallelism')
33+
plt.xticks(data.concurrency.unique())
34+
35+
colourGen = next_colour()
36+
markerGen = next_marker()
3737
for flows in data.flows.unique():
3838
genColour = next(colourGen)
3939
genMarker = next(markerGen)
4040
res = data[(data.flows == flows)]
41-
ax.plot(res.concurrency, res.score*elements, label="flows={}".format(flows), color=genColour, marker=genMarker)
41+
plt.plot(res.concurrency, res.score*elements, label="flows={}".format(flows), color=genColour, marker=genMarker)
4242

43-
def genFile(pdf):
43+
def genFile():
4444
data = pd.read_table(inputFile, sep=",", skiprows=1, names=["benchmark","mode","threads","samples","score","scoreError","unit","concurrency","flows"])
45-
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 20))
46-
draw(data, ax)
47-
lines, labels = ax.get_legend_handles_labels()
48-
fig.legend(lines, labels, loc='upper center', borderpad=0, ncol=4, frameon=False, borderaxespad=4, prop={'size': 8})
49-
45+
plt.figure(figsize=(20, 20))
46+
draw(data, plt)
47+
plt.legend(loc='upper center', borderpad=0, ncol=4, frameon=False, borderaxespad=4, prop={'size': 8})
5048
plt.tight_layout(pad=12, w_pad=2, h_pad=1)
51-
pdf.savefig(bbox_inches='tight')
49+
plt.savefig(outputFile, bbox_inches='tight')
5250

53-
with PdfPages(outputFile) as pdf:
54-
genFile(pdf)
51+
genFile()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ enum class SemaphoreBenchDispatcherCreator(val create: (parallelism: Int) -> Cor
8383
EXPERIMENTAL({ parallelism -> ExperimentalCoroutineDispatcher(corePoolSize = parallelism, maxPoolSize = parallelism) })
8484
}
8585

86-
private fun doWork(work: Int) {
86+
fun doWork(work: Int) {
8787
// We use geometric distribution here
8888
val p = 1.0 / work
8989
val r = ThreadLocalRandom.current()

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package benchmarks.flow
22

3+
import benchmarks.doWork
34
import kotlinx.coroutines.*
45
import kotlinx.coroutines.flow.*
56
import org.openjdk.jmh.annotations.*
7+
import java.util.concurrent.ThreadLocalRandom
68
import java.util.concurrent.TimeUnit
79
import kotlin.math.ceil
810

@@ -33,16 +35,13 @@ open class FlattenMergeBenchmark {
3335

3436
@Setup
3537
fun setup() {
36-
val flowsNumber = if (flows < 0) {
37-
-flows * concurrency
38-
}
39-
else {
40-
flows
41-
}
38+
val flowsNumber = if (flows >= 0) flows else -(flows * concurrency)
39+
val flowElementsToProcess = ceil(ELEMENTS / flowsNumber.toDouble())
4240

4341
flow = (1..flowsNumber).asFlow().map {
4442
flow {
45-
repeat(ceil(ELEMENTS / flowsNumber.toDouble()).toInt()) {
43+
repeat(flowElementsToProcess.toInt()) {
44+
doWork(WORK)
4645
emit(it)
4746
}
4847
}
@@ -55,4 +54,5 @@ open class FlattenMergeBenchmark {
5554
}
5655
}
5756

58-
private const val ELEMENTS = 10_000
57+
private const val ELEMENTS = 100_000
58+
private const val WORK = 80

0 commit comments

Comments
 (0)