@@ -81,6 +81,7 @@ private fun addBenchmarkMethods(typeSpecBuilder: TypeSpec.Builder, descriptor: C
81
81
method.annotations.any { it.name == " kotlinx.benchmark.Setup" || it.name == " kotlinx.benchmark.TearDown" } -> {
82
82
generateNonMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
83
83
}
84
+
84
85
else -> {
85
86
generateMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
86
87
}
@@ -95,8 +96,12 @@ private fun generateMeasurableMethod(
95
96
typeSpecBuilder : TypeSpec .Builder
96
97
) {
97
98
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
100
105
101
106
val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _${method.name} " )
102
107
.addAnnotation(ClassName (" org.junit" , " Test" ))
@@ -106,7 +111,10 @@ private fun generateMeasurableMethod(
106
111
.build()
107
112
)
108
113
// 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
+ )
110
118
.beginControlFlow(" while (state.keepRunning())" )
111
119
.addStatement(" $propertyName .${method.name} ()" )
112
120
.endControlFlow()
@@ -126,6 +134,7 @@ private fun generateNonMeasurableMethod(
126
134
.addStatement(" $propertyName .${method.name} ()" )
127
135
typeSpecBuilder.addFunction(methodSpecBuilder.build())
128
136
}
137
+
129
138
" kotlinx.benchmark.TearDown" -> {
130
139
val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _tearDown" )
131
140
.addAnnotation(ClassName (" org.junit" , " After" ))
@@ -152,10 +161,12 @@ private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<
152
161
val updatedAndroidBlockContent = if (androidBlockContent.contains(" dependencies {" )) {
153
162
val dependenciesBlockStart = androidBlockContent.indexOf(" dependencies {" )
154
163
val dependenciesBlockEnd = androidBlockContent.indexOf(" }" , dependenciesBlockStart) + 1
155
- val dependenciesBlockContent = androidBlockContent.substring(dependenciesBlockStart, dependenciesBlockEnd)
164
+ val dependenciesBlockContent =
165
+ androidBlockContent.substring(dependenciesBlockStart, dependenciesBlockEnd)
156
166
157
167
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 "))"""
159
170
}
160
171
androidBlockContent.replace(
161
172
dependenciesBlockContent,
@@ -166,12 +177,14 @@ private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<
166
177
)
167
178
} else {
168
179
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 "))"""
170
182
}
171
183
androidBlockContent.replace(" {" , " {\n dependencies {\n $newDependenciesString \n }\n " )
172
184
}
173
185
174
- val updatedBuildGradleContent = buildGradleContent.replace(androidBlockContent, updatedAndroidBlockContent)
186
+ val updatedBuildGradleContent =
187
+ buildGradleContent.replace(androidBlockContent, updatedAndroidBlockContent)
175
188
buildGradleFile.writeText(updatedBuildGradleContent)
176
189
}
177
190
}
0 commit comments