mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-10-20 11:50:20 +00:00
one more rewrite of semaphore
This commit is contained in:
16
coroutines/benchmark/build.gradle
Normal file
16
coroutines/benchmark/build.gradle
Normal file
@@ -0,0 +1,16 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.jvm"
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
56
coroutines/benchmark/src/main/kotlin/main.kt
Normal file
56
coroutines/benchmark/src/main/kotlin/main.kt
Normal file
@@ -0,0 +1,56 @@
|
||||
import dev.inmo.micro_utils.coroutines.SmartSemaphore
|
||||
import dev.inmo.micro_utils.coroutines.withAcquire
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.joinAll
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import kotlin.math.tanh
|
||||
import kotlin.system.measureNanoTime
|
||||
|
||||
data class Record(
|
||||
val permits: Int,
|
||||
val i: Int,
|
||||
val timeNanos: Long
|
||||
)
|
||||
|
||||
private const val permitsCount = 1000
|
||||
private val acquiresCount = 10000
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val outputFilePath = args.firstOrNull() ?: "local.bench.txt"
|
||||
|
||||
val records = ArrayList<Record>(permitsCount * acquiresCount)
|
||||
val jobs = ArrayList<Job>()
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
for (permits in 1 .. permitsCount) {
|
||||
val semaphore = SmartSemaphore.Mutable(permits)
|
||||
for (i in 0 until acquiresCount) {
|
||||
jobs.add(
|
||||
scope.launch(start = CoroutineStart.LAZY) {
|
||||
val timeNanos = measureNanoTime {
|
||||
semaphore.withAcquire {
|
||||
tanh(57.179)
|
||||
}
|
||||
}
|
||||
records.add(Record(permits, i, timeNanos))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
jobs.joinAll()
|
||||
val file = File(outputFilePath)
|
||||
file.writeText("")
|
||||
val text = buildString {
|
||||
records.groupBy { it.permits }.forEach {
|
||||
it.value.sortedBy { it.i }.forEach {
|
||||
append("${it.permits} ${it.i} ${it.timeNanos}\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
file.appendText(text)
|
||||
file.appendText("\n")
|
||||
println(text)
|
||||
}
|
||||
Reference in New Issue
Block a user