changeset 344:85b9e57dc794

Deprecate and remove InstanceProvider.
author shade
date Tue, 14 Jan 2014 01:26:27 +0400
parents f479fb156309
children 52ba6df72cb3
files jmh-core/src/main/java/org/openjdk/jmh/runner/BaseMicroBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java
diffstat 2 files changed, 14 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseMicroBenchmarkHandler.java	Mon Jan 13 20:19:43 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseMicroBenchmarkHandler.java	Tue Jan 14 01:26:27 2014 +0400
@@ -58,7 +58,7 @@
     protected final ExecutorService executor;
 
     // (Aleksey) Forgive me, Father, for I have sinned.
-    protected final ThreadLocal<InstanceProvider> threadLocal;
+    protected final ThreadLocal<Object> instances;
 
     protected final OutputFormat format;
     protected final TimeUnit timeUnit;
@@ -69,10 +69,16 @@
         this.microbenchmark = microbenchmark;
         this.registeredProfilers = createProfilers(options);
         this.executor = EXECUTOR_TYPE.createExecutor(executionParams.getThreads(), microbenchmark.getUsername());
-        this.threadLocal = new ThreadLocal<InstanceProvider>() {
+        this.instances = new ThreadLocal<Object>() {
             @Override
-            protected InstanceProvider initialValue() {
-                return new EagerInstanceProvider(clazz);
+            protected Object initialValue() {
+                try {
+                    return clazz.newInstance();
+                } catch (InstantiationException e) {
+                    throw new RuntimeException("Class " + clazz.getName() + " instantiation error ", e);
+                } catch (IllegalAccessException e) {
+                    throw new RuntimeException("Class " + clazz.getName() + " instantiation error ", e);
+                }
             }
         };
         this.format = format;
@@ -255,70 +261,4 @@
         shutdownExecutor(executor);
     }
 
-    public interface InstanceProvider {
-
-        /**
-         * Gets the benchmark instance. This call will lazily instantiate benchmark instance.
-         * @return benchmark instance.
-         * @throws RuntimeException if something goes wrong
-         */
-        public Object getInstance();
-
-    }
-
-    /**
-     * Gets the benchmark instance. This call will lazily instantiate benchmark instance.
-     */
-    public static class EagerInstanceProvider implements InstanceProvider {
-
-        private final Object instance;
-
-        public EagerInstanceProvider(Class<?> clazz) {
-            try {
-                instance = clazz.newInstance();
-            } catch (InstantiationException e) {
-                throw new RuntimeException("Class " + clazz.getName() + " instantiation error ", e);
-            } catch (IllegalAccessException e) {
-                throw new RuntimeException("Class " + clazz.getName() + " instantiation error ", e);
-            }
-        }
-
-        @Override
-        public Object getInstance() {
-            return instance;
-        }
-    }
-
-    /**
-     * Gets the benchmark instance. This call will lazily instantiate benchmark instance.
-     */
-    public static class LazyInstanceProvider implements InstanceProvider {
-
-        private final Class<?> clazz;
-
-        private volatile Object instance;
-
-        public LazyInstanceProvider(Class<?> clazz) {
-            this.clazz = clazz;
-        }
-
-        @Override
-        public Object getInstance() {
-            if (instance == null) {
-                synchronized (this) {
-                    if (instance == null) {
-                        try {
-                            instance = clazz.newInstance();
-                        } catch (InstantiationException e) {
-                            throw new RuntimeException("Class " + clazz.getName() + " instantiation error ", e);
-                        } catch (IllegalAccessException e) {
-                            throw new RuntimeException("Class " + clazz.getName() + " instantiation error ", e);
-                        }
-                    }
-                }
-            }
-            return instance;
-        }
-    }
-
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Mon Jan 13 20:19:43 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopMicroBenchmarkHandler.java	Tue Jan 14 01:26:27 2014 +0400
@@ -106,7 +106,7 @@
             remainingSubgroupThreads--;
 
             ThreadControl threadControl = new ThreadControl(currentGroup, currentSubgroup);
-            runners[i] = new BenchmarkTask(threadLocal, control, threadControl);
+            runners[i] = new BenchmarkTask(instances, control, threadControl);
         }
 
         // submit tasks to threadpool
@@ -190,11 +190,11 @@
      */
     class BenchmarkTask implements Callable<Collection<? extends Result>> {
 
-        private final ThreadLocal<InstanceProvider> invocationHandler;
+        private final ThreadLocal<Object> invocationHandler;
         private final InfraControl control;
         private final ThreadControl threadControl;
 
-        BenchmarkTask(ThreadLocal<InstanceProvider> invocationHandler, InfraControl control, ThreadControl threadControl) {
+        BenchmarkTask(ThreadLocal<Object> invocationHandler, InfraControl control, ThreadControl threadControl) {
             this.invocationHandler = invocationHandler;
             this.control = control;
             this.threadControl = threadControl;
@@ -203,7 +203,7 @@
         @Override
         public Collection<? extends Result> call() throws Exception {
             try {
-                return invokeBenchmark(invocationHandler.get().getInstance(), control, threadControl);
+                return invokeBenchmark(invocationHandler.get(), control, threadControl);
             } catch (Throwable e) {
                 // about to fail the iteration;
                 // compensate for missed sync-iteration latches, we don't care about that anymore