@@ -81,6 +81,7 @@ private fun addBenchmarkMethods(typeSpecBuilder: TypeSpec.Builder, descriptor: C
8181 method.annotations.any { it.name == " kotlinx.benchmark.Setup" || it.name == " kotlinx.benchmark.TearDown" } -> {
8282 generateNonMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
8383 }
84+
8485 else -> {
8586 generateMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
8687 }
@@ -95,8 +96,12 @@ private fun generateMeasurableMethod(
9596 typeSpecBuilder : TypeSpec .Builder
9697) {
9798
98- val measurementAnnotation = descriptor.annotations.find { it.name == " kotlinx.benchmark.Measurement" }
99- val iterations = measurementAnnotation?.parameters?.get(" iterations" ) as ? Int ? : 5
99+ val measurementIterations = descriptor.annotations
100+ .find { it.name == " kotlinx.benchmark.Measurement" }
101+ ?.parameters?.get(" iterations" ) as ? Int ? : 5
102+ val warmupIterations = descriptor.annotations
103+ .find { it.name == " kotlinx.benchmark.Warmup" }
104+ ?.parameters?.get(" iterations" ) as ? Int ? : 5
100105
101106 val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _${method.name} " )
102107 .addAnnotation(ClassName (" org.junit" , " Test" ))
@@ -106,7 +111,10 @@ private fun generateMeasurableMethod(
106111 .build()
107112 )
108113 // TODO: Add warmupCount and repeatCount parameters
109- .addStatement(" val state = %T(warmupCount = 5, repeatCount = $iterations )" , ClassName (" androidx.benchmark" , " BenchmarkState" ))
114+ .addStatement(
115+ " val state = %T(warmupCount = $warmupIterations , repeatCount = $measurementIterations )" ,
116+ ClassName (" androidx.benchmark" , " BenchmarkState" )
117+ )
110118 .beginControlFlow(" while (state.keepRunning())" )
111119 .addStatement(" $propertyName .${method.name} ()" )
112120 .endControlFlow()
@@ -126,6 +134,7 @@ private fun generateNonMeasurableMethod(
126134 .addStatement(" $propertyName .${method.name} ()" )
127135 typeSpecBuilder.addFunction(methodSpecBuilder.build())
128136 }
137+
129138 " kotlinx.benchmark.TearDown" -> {
130139 val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _tearDown" )
131140 .addAnnotation(ClassName (" org.junit" , " After" ))
@@ -152,10 +161,12 @@ private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<
152161 val updatedAndroidBlockContent = if (androidBlockContent.contains(" dependencies {" )) {
153162 val dependenciesBlockStart = androidBlockContent.indexOf(" dependencies {" )
154163 val dependenciesBlockEnd = androidBlockContent.indexOf(" }" , dependenciesBlockStart) + 1
155- val dependenciesBlockContent = androidBlockContent.substring(dependenciesBlockStart, dependenciesBlockEnd)
164+ val dependenciesBlockContent =
165+ androidBlockContent.substring(dependenciesBlockStart, dependenciesBlockEnd)
156166
157167 val newDependenciesString = newDependencies.joinToString(" \n " ) { (dependency, version) ->
158- version?.let { """ androidTestImplementation("$dependency :$version ")""" } ? : """ androidTestImplementation(files("$dependency "))"""
168+ version?.let { """ androidTestImplementation("$dependency :$version ")""" }
169+ ? : """ androidTestImplementation(files("$dependency "))"""
159170 }
160171 androidBlockContent.replace(
161172 dependenciesBlockContent,
@@ -166,12 +177,14 @@ private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<
166177 )
167178 } else {
168179 val newDependenciesString = newDependencies.joinToString(" \n " ) { (dependency, version) ->
169- version?.let { """ androidTestImplementation("$dependency :$version ")""" } ? : """ androidTestImplementation(files("$dependency "))"""
180+ version?.let { """ androidTestImplementation("$dependency :$version ")""" }
181+ ? : """ androidTestImplementation(files("$dependency "))"""
170182 }
171183 androidBlockContent.replace(" {" , " {\n dependencies {\n $newDependenciesString \n }\n " )
172184 }
173185
174- val updatedBuildGradleContent = buildGradleContent.replace(androidBlockContent, updatedAndroidBlockContent)
186+ val updatedBuildGradleContent =
187+ buildGradleContent.replace(androidBlockContent, updatedAndroidBlockContent)
175188 buildGradleFile.writeText(updatedBuildGradleContent)
176189 }
177190 }
0 commit comments