Skip to content

Commit 4995109

Browse files
author
Abduqodiri Qurbonzoda
committed
Implement Blackhole
1 parent 7cb6ebe commit 4995109

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

examples/kotlin-multiplatform/src/commonMain/kotlin/CommonBenchmark.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ class CommonBenchmark {
5050
return value
5151
}
5252

53+
@Benchmark
54+
fun longBlackholeBenchmark(bh: Blackhole) {
55+
repeat(1000) {
56+
bh.consume(text.length)
57+
}
58+
}
5359
}

plugin/main/src/kotlinx/benchmark/gradle/SuiteSourceGenerator.kt

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val
2121
val setupFunctionName = "setUp"
2222
val teardownFunctionName = "tearDown"
2323
val parametersFunctionName = "parametrize"
24+
val bindBlackholeFunctionName = "bind"
2425

2526
val externalConfigurationFQN = "kotlinx.benchmark.ExternalConfiguration"
2627
val benchmarkAnnotationFQN = "kotlinx.benchmark.Benchmark"
@@ -36,6 +37,8 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val
3637
val measureAnnotationFQN = "kotlinx.benchmark.Measurement"
3738
val paramAnnotationFQN = "kotlinx.benchmark.Param"
3839

40+
val blackholeFQN = "kotlinx.benchmark.Blackhole"
41+
3942
val mainBenchmarkPackage = "kotlinx.benchmark.generated"
4043

4144
val suppressUnusedParameter = AnnotationSpec.builder(Suppress::class).addMember("\"UNUSED_PARAMETER\"").build()
@@ -179,6 +182,28 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val
179182
}
180183
}
181184

185+
val bhClass = ClassName.bestGuess(blackholeFQN)
186+
187+
/*
188+
private fun bind(function: CommonBenchmark.(Blackhole) -> Any?, bh: Blackhole): CommonBenchmark.() -> Any? {
189+
return { function(bh) }
190+
}
191+
*/
192+
function(bindBlackholeFunctionName) {
193+
addModifiers(KModifier.PRIVATE)
194+
195+
val bhParam = ParameterSpec.unnamed(bhClass)
196+
val bhBenchType = LambdaTypeName.get(originalClass, listOf(bhParam), ANY.copy(nullable = true))
197+
val boundBenchType = LambdaTypeName.get(originalClass, emptyList(), ANY.copy(nullable = true))
198+
199+
addParameter("function", bhBenchType)
200+
addParameter("bh", bhClass)
201+
returns(boundBenchType)
202+
203+
addStatement("return { function(bh) }")
204+
}
205+
206+
182207
val defaultParameters = parameterProperties.associateBy({ it.name }, {
183208
val annotation = it.annotations.findAnnotation(FqName(paramAnnotationFQN))!!
184209
val constant = annotation.argumentValue("value")
@@ -241,13 +266,29 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val
241266
addStatement("")
242267
for (fn in benchmarkFunctions) {
243268
val functionName = fn.name.toString()
244-
addStatement(
245-
"descriptor.add(%T(%S, descriptor, %T::%N))",
246-
benchmarkDescriptorType,
247-
"${originalClass.canonicalName}.$functionName",
248-
originalClass,
249-
functionName
250-
)
269+
270+
val hasABlackholeParameter = fn.valueParameters.singleOrNull()?.type.toString() == "Blackhole"
271+
if (hasABlackholeParameter) {
272+
println("WARNING: Blackhole works correctly only on JVM")
273+
274+
addStatement(
275+
"descriptor.add(%T(%S, descriptor, %N(%T::%N, %T())))",
276+
benchmarkDescriptorType,
277+
"${originalClass.canonicalName}.$functionName",
278+
bindBlackholeFunctionName,
279+
originalClass,
280+
functionName,
281+
bhClass
282+
)
283+
}
284+
else
285+
addStatement(
286+
"descriptor.add(%T(%S, descriptor, %T::%N))",
287+
benchmarkDescriptorType,
288+
"${originalClass.canonicalName}.$functionName",
289+
originalClass,
290+
functionName
291+
)
251292
}
252293
addStatement("return descriptor")
253294
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package kotlinx.benchmark
2+
3+
expect class Blackhole {
4+
fun consume(obj: Any?)
5+
fun consume(bool: Boolean)
6+
fun consume(c: Char)
7+
fun consume(b: Byte)
8+
fun consume(s: Short)
9+
fun consume(i: Int)
10+
fun consume(l: Long)
11+
fun consume(f: Float)
12+
fun consume(d: Double)
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package kotlinx.benchmark
2+
3+
actual class Blackhole {
4+
actual fun consume(obj: Any?) {}
5+
actual fun consume(bool: Boolean) {}
6+
actual fun consume(c: Char) {}
7+
actual fun consume(b: Byte) {}
8+
actual fun consume(s: Short) {}
9+
actual fun consume(i: Int) {}
10+
actual fun consume(l: Long) {}
11+
actual fun consume(f: Float) {}
12+
actual fun consume(d: Double) {}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package kotlinx.benchmark
2+
3+
actual typealias Blackhole = org.openjdk.jmh.infra.Blackhole
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package kotlinx.benchmark
2+
3+
actual class Blackhole {
4+
actual fun consume(obj: Any?) {}
5+
actual fun consume(bool: Boolean) {}
6+
actual fun consume(c: Char) {}
7+
actual fun consume(b: Byte) {}
8+
actual fun consume(s: Short) {}
9+
actual fun consume(i: Int) {}
10+
actual fun consume(l: Long) {}
11+
actual fun consume(f: Float) {}
12+
actual fun consume(d: Double) {}
13+
}

0 commit comments

Comments
 (0)