-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmark feature implementation (#34)
- Loading branch information
Showing
2 changed files
with
183 additions
and
20 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
src/main/kotlin/com/featurevisor/testRunner/BenchmarkFeature.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,149 @@ | ||
package com.featurevisor.testRunner | ||
|
||
import com.featurevisor.sdk.FeaturevisorInstance | ||
import com.featurevisor.sdk.getVariable | ||
import com.featurevisor.sdk.getVariation | ||
import com.featurevisor.sdk.isEnabled | ||
import com.featurevisor.types.* | ||
|
||
data class BenchmarkOutput( | ||
val value: Any? = null, | ||
val duration: Double | ||
) | ||
|
||
data class BenchMarkOptions( | ||
val environment: String = "", | ||
val feature: String = "", | ||
val n: Int = 0, | ||
val projectRootPath: String = "", | ||
val context: Context = emptyMap(), | ||
val variation: Boolean? = null, | ||
val variable: String? = null, | ||
) | ||
|
||
fun benchmarkFeature(option: BenchMarkOptions) { | ||
println("Running benchmark for feature ${option.feature}...") | ||
|
||
println("Building datafile containing all features for ${option.environment}...") | ||
|
||
val datafileBuildStart = System.nanoTime().toDouble() | ||
|
||
val datafileContent = buildDataFileForStaging(option.projectRootPath) | ||
|
||
val datafileBuildEnd = System.nanoTime().toDouble() | ||
|
||
val datafileBuildDuration = datafileBuildEnd - datafileBuildStart | ||
|
||
println("Datafile build duration: ${convertNanoSecondToMilliSecond(datafileBuildDuration)}") | ||
|
||
val sdk = initializeSdkWithDataFileContent(datafileContent) | ||
|
||
println("...SDK initialized") | ||
|
||
println("Against context: ${option.context}") | ||
|
||
val output: BenchmarkOutput | ||
|
||
if (option.variable != null) { | ||
println("Evaluating variable ${option.variable} ${option.n} times...") | ||
|
||
output = benchmarkFeatureVariable( | ||
sdk, | ||
feature = option.feature, | ||
variableKey = option.variable, | ||
context = option.context, | ||
n = option.n | ||
) | ||
|
||
} else if (option.variation != null) { | ||
println("Evaluating variation ${option.variation} ${option.n} times...") | ||
|
||
output = benchmarkFeatureVariation( | ||
sdk, | ||
feature = option.feature, | ||
context = option.context, | ||
n = option.n | ||
) | ||
} else { | ||
println("Evaluating flag ${option.n} times...") | ||
|
||
output = benchmarkFeatureFlag( | ||
sdk, | ||
feature = option.feature, | ||
context = option.context, | ||
n = option.n | ||
) | ||
} | ||
|
||
println("Evaluated value : ${output.value}") | ||
println("Total duration : ${convertNanoSecondToMilliSecond(output.duration)}") | ||
if (option.n != 0) { | ||
println("Average duration: ${convertNanoSecondToMilliSecond(output.duration / option.n)}") | ||
} | ||
} | ||
|
||
|
||
fun benchmarkFeatureFlag( | ||
f: FeaturevisorInstance, | ||
feature: FeatureKey, | ||
context: Context, | ||
n: Int | ||
): BenchmarkOutput { | ||
val start = System.nanoTime().toDouble() | ||
var value: Any = false | ||
|
||
for (i in 0..n) { | ||
value = f.isEnabled(featureKey = feature, context = context) | ||
} | ||
|
||
val end = System.nanoTime().toDouble() | ||
|
||
return BenchmarkOutput( | ||
value = value, | ||
duration = end - start | ||
) | ||
} | ||
|
||
|
||
fun benchmarkFeatureVariation( | ||
f: FeaturevisorInstance, | ||
feature: FeatureKey, | ||
context: Context, | ||
n: Int | ||
): BenchmarkOutput { | ||
val start = System.nanoTime().toDouble() | ||
var value: VariationValue? = null | ||
|
||
for (i in 0..n) { | ||
value = f.getVariation(featureKey = feature, context = context) | ||
} | ||
|
||
val end = System.nanoTime().toDouble() | ||
|
||
return BenchmarkOutput( | ||
value = value, | ||
duration = end - start | ||
) | ||
} | ||
|
||
fun benchmarkFeatureVariable( | ||
f: FeaturevisorInstance, | ||
feature: FeatureKey, | ||
variableKey: VariableKey, | ||
context: Context, | ||
n: Int | ||
): BenchmarkOutput { | ||
val start = System.nanoTime().toDouble() | ||
var value: VariableValue? = null | ||
|
||
for (i in 0..n) { | ||
value = f.getVariable(featureKey = feature, variableKey = variableKey, context = context) | ||
} | ||
|
||
val end = System.nanoTime().toDouble() | ||
|
||
return BenchmarkOutput( | ||
value = value, | ||
duration = end - start | ||
) | ||
} |
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