Last active
December 15, 2015 14:48
-
-
Save oehme/5276731 to your computer and use it in GitHub Desktop.
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
@Active(typeof(BenchmarkProcessor)) | |
annotation Benchmark { | |
} | |
class BenchmarkProcessor extends AbstractClassProcessor { | |
override doTransform(MutableClassDeclaration cls, TransformationContext context) { | |
new BenchmarkClassGenerator(context, cls).generate | |
} | |
} | |
@Data | |
class BenchmarkClassGenerator { | |
val extension TransformationContext context | |
val MutableClassDeclaration benchmark | |
def generate() { | |
benchmark.final = true | |
benchmark.extendedClass = typeof(SimpleBenchmark).newTypeReference | |
benchmark.addMethod("main") [ | |
static = true | |
addParameter("args", newArrayTypeReference(string)) | |
body = [extension it| | |
''' | |
«typeof(Runner).newTypeReference.toJavaCode».main(«benchmark.simpleName».class, args); | |
'''] | |
] | |
timedMethods.forEach [ | |
addParameter("iterations", primitiveInt) | |
] | |
loopMethods.forEach [ method | | |
benchmark.addMethod(method.simpleName.replaceFirst("loop", "time")) [ | |
addParameter("iterations", primitiveInt) | |
body = [ | |
''' | |
for (int i = 0; i < iterations;i++) { | |
«method.simpleName»(); | |
} | |
'''] | |
] | |
] | |
benchmarkParameters.forEach [ param | | |
benchmark.addField(param.simpleName.replace("Values", "")) [ | |
addAnnotation(typeof(Param).findTypeGlobally) | |
type = param.propertyType.actualTypeArguments.get(0) | |
] | |
param.visibility = Visibility::DEFAULT | |
param.makeStatic | |
] | |
} | |
def benchmarkParameters() { | |
(benchmark.declaredFields + benchmark.declaredMethods).filter [ | |
simpleName.endsWith("Values") | |
] | |
} | |
def timedMethods() { | |
benchmark.declaredMethods.filter [ | |
static == false && simpleName.startsWith("time") | |
] | |
} | |
def loopMethods() { | |
benchmark.declaredMethods.filter [ | |
static == false && simpleName.startsWith("loop") | |
] | |
} | |
def dispatch makeStatic(MutableFieldDeclaration field) { | |
field.static = true | |
} | |
def dispatch makeStatic(MutableMethodDeclaration method) { | |
method.static = true | |
} | |
def dispatch propertyType(MutableFieldDeclaration field) { | |
field.type | |
} | |
def dispatch propertyType(MutableMethodDeclaration method) { | |
method.returnType | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment