-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
benchmarks/src/jmh/kotlin/benchmarks/debug/DebugProbesConcurrentBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package benchmarks.debug | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.debug.* | ||
import org.openjdk.jmh.annotations.* | ||
import org.openjdk.jmh.annotations.State | ||
import java.util.concurrent.* | ||
|
||
@Warmup(iterations = 5, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
@Fork(value = 1) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Benchmark) | ||
open class DebugProbesConcurrentBenchmark { | ||
|
||
@Setup | ||
fun setup() { | ||
DebugProbes.sanitizeStackTraces = false | ||
DebugProbes.enableCreationStackTraces = false | ||
DebugProbes.install() | ||
} | ||
|
||
@TearDown | ||
fun tearDown() { | ||
DebugProbes.uninstall() | ||
} | ||
|
||
|
||
@Benchmark | ||
fun run() = runBlocking<Long> { | ||
var sum = 0L | ||
repeat(8) { | ||
launch(Dispatchers.Default) { | ||
val seq = stressSequenceBuilder((1..100).asSequence()) { | ||
(1..it).asSequence() | ||
} | ||
|
||
for (i in seq) { | ||
sum += i.toLong() | ||
} | ||
} | ||
} | ||
sum | ||
} | ||
|
||
private fun <Node> stressSequenceBuilder(initialSequence: Sequence<Node>, children: (Node) -> Sequence<Node>): Sequence<Node> { | ||
return sequence { | ||
val initialIterator = initialSequence.iterator() | ||
if (!initialIterator.hasNext()) { | ||
return@sequence | ||
} | ||
val visited = HashSet<Node>() | ||
val sequences = ArrayDeque<Sequence<Node>>() | ||
sequences.addLast(initialIterator.asSequence()) | ||
while (sequences.isNotEmpty()) { | ||
val currentSequence = sequences.removeFirst() | ||
for (node in currentSequence) { | ||
if (visited.add(node)) { | ||
yield(node) | ||
sequences.addLast(children(node)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |