Created
November 20, 2014 02:48
-
-
Save clohfink/9ee94e3767b5d8170220 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
@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 final Meter tasksAdded; | |
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")); | |
tasksAdded = Metrics.newMeter(new MetricName("g", "t", "n2"), "tasks", TimeUnit.SECONDS); | |
} | |
@Override | |
protected void afterExecute(Runnable r, Throwable t) | |
{ | |
wallTime.inc(System.nanoTime() - startTime.get()); | |
super.afterExecute(r, t); | |
} | |
@Override | |
public void execute(Runnable task) { | |
tasksAdded.mark(); | |
super.execute(task); | |
} | |
@Override | |
protected void beforeExecute(Thread t, Runnable r) | |
{ | |
super.beforeExecute(t, r); | |
startTime.set(System.nanoTime()); | |
} | |
} | |
@State(Scope.Benchmark) | |
public static class WrapRunnableExecutorPool extends JMXEnabledThreadPoolExecutor { | |
public final Counter wallTime; | |
public final Meter tasksAdded; | |
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")); | |
tasksAdded = Metrics.newMeter(new MetricName("g", "t", "n2"), "tasks", TimeUnit.SECONDS); | |
} | |
@Override | |
public void execute(Runnable task) { | |
long startTime = System.nanoTime(); | |
try | |
{ | |
tasksAdded.mark(); | |
super.execute(task); | |
} finally { | |
wallTime.inc(System.nanoTime() - 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.nanoTime(); | |
try | |
{ | |
task.run(); | |
} finally | |
{ | |
wallTime.inc(System.nanoTime() - startTime); | |
} | |
} | |
} | |
@State(Scope.Benchmark) | |
public static class StaticWrapExecutorPool extends JMXEnabledThreadPoolExecutor { | |
public final Counter wallTime; | |
public final Meter tasksAdded; | |
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")); | |
tasksAdded = Metrics.newMeter(new MetricName("g", "t", "n2"), "tasks", TimeUnit.SECONDS); | |
} | |
@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"); | |
} | |
@Override | |
public void execute(Runnable task) | |
{ | |
super.execute(task); | |
} | |
} | |
@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