Created
November 20, 2014 04:37
-
-
Save clohfink/b51eb027c55008377d93 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@State(Scope.Benchmark) | |
public class ThreadLocalBenchmark { | |
@State(Scope.Benchmark) | |
public static class ThreadLocalExecutorPool extends JMXEnabledThreadPoolExecutor { | |
protected final ThreadLocal<Long> startTime = new ThreadLocal<Long>(); | |
public final Counter wallTime; | |
public ThreadLocalExecutorPool() | |
{ | |
super(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("tl", Thread.MIN_PRIORITY), "internal"); | |
wallTime = Metrics.newCounter(new MetricName("g", "t", "n")); | |
} | |
@Override | |
protected void afterExecute(Runnable r, Throwable t) | |
{ | |
wallTime.inc(System.currentTimeMillis() - startTime.get()); | |
super.afterExecute(r, t); | |
} | |
@Override | |
public void execute(Runnable task) { | |
super.execute(task); | |
} | |
@Override | |
protected void beforeExecute(Thread t, Runnable r) | |
{ | |
super.beforeExecute(t, r); | |
startTime.set(System.currentTimeMillis()); | |
} | |
} | |
@State(Scope.Benchmark) | |
public static class WrapRunnableExecutorPool extends JMXEnabledThreadPoolExecutor { | |
public final Counter wallTime; | |
public WrapRunnableExecutorPool() | |
{ | |
super(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("wrapped", Thread.MIN_PRIORITY), "internal"); | |
wallTime = Metrics.newCounter(new MetricName("g", "t", "n")); | |
} | |
@Override | |
public void execute(Runnable task) { | |
long startTime = System.currentTimeMillis(); | |
try | |
{ | |
super.execute(task); | |
} finally { | |
wallTime.inc(System.currentTimeMillis() - startTime); | |
} | |
} | |
} | |
static class TimedRunnable implements Runnable | |
{ | |
Counter wallTime; | |
Runnable task; | |
public TimedRunnable(Counter wallTime, Runnable task) | |
{ | |
this.wallTime = wallTime; | |
this.task = task; | |
} | |
public void run() | |
{ | |
long startTime = System.currentTimeMillis(); | |
try | |
{ | |
task.run(); | |
} finally | |
{ | |
wallTime.inc(System.currentTimeMillis() - startTime); | |
} | |
} | |
} | |
@State(Scope.Benchmark) | |
public static class StaticWrapExecutorPool extends JMXEnabledThreadPoolExecutor { | |
public final Counter wallTime; | |
public StaticWrapExecutorPool() | |
{ | |
super(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("staticwrapped", Thread.MIN_PRIORITY), "internal"); | |
wallTime = Metrics.newCounter(new MetricName("g", "t", "n")); | |
} | |
@Override | |
public void execute(Runnable task) { | |
super.execute(new TimedRunnable(wallTime, task)); | |
} | |
} | |
@State(Scope.Benchmark) | |
public static class BaseLineExecutorPool extends JMXEnabledThreadPoolExecutor | |
{ | |
public BaseLineExecutorPool() | |
{ | |
super(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("baseline", | |
Thread.MIN_PRIORITY), "internal"); | |
} | |
} | |
@Benchmark() | |
@BenchmarkMode(Mode.All) | |
public synchronized Object baseline(BaseLineExecutorPool pool) throws InterruptedException, ExecutionException { | |
return pool.submit(new Runnable() { | |
public void run() { | |
Thread.yield(); | |
} | |
}).get(); | |
} | |
@Benchmark() | |
@BenchmarkMode(Mode.All) | |
public synchronized Object staticWrap(StaticWrapExecutorPool pool) throws InterruptedException, ExecutionException { | |
return pool.submit(new Runnable() { | |
public void run() { | |
Thread.yield(); | |
} | |
}).get(); | |
} | |
@Benchmark() | |
@BenchmarkMode(Mode.All) | |
public synchronized Object threadlocal(ThreadLocalExecutorPool pool) throws InterruptedException, ExecutionException { | |
return pool.submit(new Runnable() { | |
public void run() { | |
Thread.yield(); | |
} | |
}).get(); | |
} | |
@Benchmark() | |
@BenchmarkMode(Mode.All) | |
public synchronized Object wrapped(WrapRunnableExecutorPool pool) throws InterruptedException, ExecutionException { | |
return pool.submit(new Runnable() { | |
public void run() { | |
Thread.yield(); | |
} | |
}).get(); | |
} | |
@TearDown | |
public static void teardown(ThreadLocalExecutorPool tlp, WrapRunnableExecutorPool wrap, BaseLineExecutorPool blp) { | |
try | |
{ | |
Field f = QueryProcessor.class.getDeclaredField("evictionCheckTimer"); | |
f.setAccessible(true); | |
((ScheduledExecutorService)f.get(null)).shutdown(); | |
} catch (IllegalArgumentException e) | |
{ | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) | |
{ | |
e.printStackTrace(); | |
} catch (NoSuchFieldException e) | |
{ | |
e.printStackTrace(); | |
} catch (SecurityException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(".*" + ThreadLocalBenchmark.class.getSimpleName() + ".*") | |
.warmupIterations(3) | |
.measurementIterations(15) | |
.forks(1) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment