Created
April 22, 2015 00:51
-
-
Save the8472/ae462c8f8d598a7be671 to your computer and use it in GitHub Desktop.
JMH bench for custom executor
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
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.TearDown; | |
import org.openjdk.jmh.annotations.Threads; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.infra.Blackhole; | |
import the8472.utils.concurrent.NonblockingScheduledExecutor; | |
@State(Scope.Benchmark) | |
@Threads(4) | |
@Warmup(iterations=7, time=500, timeUnit=TimeUnit.MILLISECONDS) | |
@Measurement(iterations=7, time=500, timeUnit=TimeUnit.MILLISECONDS) | |
public class SchedulerBenchmark { | |
public enum Type { | |
JDK, | |
CUSTOM; | |
} | |
@Param({"JDK", "CUSTOM"}) | |
Type type; | |
@Param({"1", "3", "4", "5", "8"}) | |
int threads; | |
ScheduledExecutorService service; | |
@Setup//(Level.Iteration) | |
public void buildPool() { | |
if(type == Type.JDK) { | |
service = new ScheduledThreadPoolExecutor(threads, (r) -> { | |
Thread t = new Thread(r); | |
t.setDaemon(true); | |
return t; | |
}); | |
} else { | |
service = new NonblockingScheduledExecutor("foo", threads, null); | |
} | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.Throughput) | |
public void testPool() { | |
doWork().join(); | |
} | |
@TearDown//(Level.Iteration) | |
public void shutdownPool() { | |
service.shutdownNow(); | |
service = null; | |
} | |
CompletableFuture<Void> doWork() { | |
List<CompletableFuture<Void>> futures = new ArrayList<>(); | |
for(int i=0;i<50;i++) { | |
final CompletableFuture<Void> f = new CompletableFuture<Void>(); | |
futures.add(f); | |
service.execute(() -> { | |
Blackhole.consumeCPU(64); | |
service.schedule(() -> taskTimeout(f), 1, TimeUnit.NANOSECONDS); | |
}); | |
} | |
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); | |
} | |
void taskTimeout(CompletableFuture<Void> f) { | |
f.complete(null); | |
Blackhole.consumeCPU(64); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment