Permalink
Browse files

Migrating CacheableBenchmark from Scala to Java

  • Loading branch information...
nurkiewicz committed Jan 12, 2013
1 parent 1add1fd commit 58d40614cb5fb8c14feca39d5f2bc006358b0577
@@ -0,0 +1,55 @@
+package com.blogspot.nurkiewicz.cacheable;
+
+import com.blogspot.nurkiewicz.cacheable.calculator.Calculator;
+import com.google.caliper.SimpleBenchmark;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+/**
+ * @author Tomasz Nurkiewicz
+ * @since 1/12/13, 12:07 AM
+ */
+public class CacheableBenchmark extends SimpleBenchmark {
+
+ private final Calculator noCaching = fromSpringContext(NoCachingConfig.class);
+ private final Calculator manualCaching = fromSpringContext(ManualCachingConfig.class);
+ private final Calculator cacheableCglib = fromSpringContext(CacheableCglibConfig.class);
+ private final Calculator cacheableJdkProxy = fromSpringContext(CacheableJdkProxyConfig.class);
+ private final Calculator cacheableAspectJ = fromSpringContext(CacheableAspectJWeaving.class);
+ private final Calculator aspectJCustom = fromSpringContext(AspectJCustomAspect.class);
+
+ private static <T extends BaseConfig> Calculator fromSpringContext(Class<T> config) {
+ return new AnnotationConfigApplicationContext(config).getBean(Calculator.class);
+ }
+
+ private int benchmarkWith(Calculator calculator, int reps) {
+ int accum = 0;
+ for (int i = 0; i < reps; ++i) {
+ accum += calculator.identity(i % 10);
+ }
+ return accum;
+ }
+
+ public int timeNoCaching(int reps) {
+ return benchmarkWith(noCaching, reps);
+ }
+
+ public int timeManualCaching(int reps) {
+ return benchmarkWith(manualCaching, reps);
+ }
+
+ public int timeCacheableWithCglib(int reps) {
+ return benchmarkWith(cacheableCglib, reps);
+ }
+
+ public int timeCacheableWithJdkProxy(int reps) {
+ return benchmarkWith(cacheableJdkProxy, reps);
+ }
+
+ public int timeCacheableWithAspectJWeaving(int reps) {
+ return benchmarkWith(cacheableAspectJ, reps);
+ }
+
+ public int timeAspectJCustom(int reps) {
+ return benchmarkWith(aspectJCustom, reps);
+ }
+}
@@ -1,41 +0,0 @@
-package com.blogspot.nurkiewicz.cacheable
-
-import calculator.Calculator
-import com.google.caliper.SimpleBenchmark
-import org.springframework.context.annotation.AnnotationConfigApplicationContext
-
-/**
- * @author Tomasz Nurkiewicz
- * @since 1/12/13, 12:07 AM
- */
-class CacheableBenchmark extends SimpleBenchmark {
-
- val noCaching: Calculator = fromSpringContext(classOf[NoCachingConfig])
- val manualCaching: Calculator = fromSpringContext(classOf[ManualCachingConfig])
- val cacheableCglib: Calculator = fromSpringContext(classOf[CacheableCglibConfig])
- val cacheableJdkProxy: Calculator = fromSpringContext(classOf[CacheableJdkProxyConfig])
- val cacheableAspectJ: Calculator = fromSpringContext(classOf[CacheableAspectJWeaving])
- val aspectJCustom: Calculator = fromSpringContext(classOf[AspectJCustomAspect])
-
- println("=" * 80)
-
- def fromSpringContext[T <: BaseConfig](config: Class[T]) =
- new AnnotationConfigApplicationContext(config).getBean(classOf[Calculator])
-
- def benchmarkWith(calculator: Calculator, reps: Int) = {
- var i = reps
- var accum = 0L
- while(i > 0) {
- accum += calculator.identity(i % 10)
- i -= 1
- }
- accum
- }
-
- def timeNoCaching(reps: Int) = benchmarkWith(noCaching, reps)
- def timeManualCaching(reps: Int) = benchmarkWith(manualCaching, reps)
- def timeCacheableWithCglib(reps: Int) = benchmarkWith(cacheableCglib, reps)
- def timeCacheableWithJdkProxy(reps: Int) = benchmarkWith(cacheableJdkProxy, reps)
- def timeCacheableWithAspectJWeaving(reps: Int) = benchmarkWith(cacheableAspectJ, reps)
- def timeAspectJCustom(reps: Int) = benchmarkWith(aspectJCustom, reps)
-}

0 comments on commit 58d4061

Please sign in to comment.