2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/jmh/Queues.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2016-08-09 10:21:47 -06:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-10-22 13:35:19 -07:00
|
|
|
// Performance differences between Queues
|
2017-01-11 09:11:59 -08:00
|
|
|
package collectiontopics.jmh;
|
2016-08-09 10:21:47 -06:00
|
|
|
import org.openjdk.jmh.annotations.*;
|
|
|
|
import java.util.*;
|
2016-10-22 13:35:19 -07:00
|
|
|
import java.util.concurrent.*;
|
2016-08-09 10:21:47 -06:00
|
|
|
|
|
|
|
@State(Scope.Thread)
|
2016-10-22 13:35:19 -07:00
|
|
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
|
|
|
@Warmup(iterations = 5, batchSize = 5000)
|
|
|
|
@Measurement(iterations = 5, batchSize = 5000)
|
|
|
|
@BenchmarkMode(Mode.SingleShotTime)
|
2016-08-09 10:21:47 -06:00
|
|
|
@Fork(1)
|
|
|
|
public class Queues {
|
2016-10-22 13:35:19 -07:00
|
|
|
private Queue<String> queue;
|
2016-08-09 10:21:47 -06:00
|
|
|
|
2016-10-22 13:35:19 -07:00
|
|
|
@Param({
|
|
|
|
"java.util.LinkedList",
|
|
|
|
"java.util.concurrent.ConcurrentLinkedQueue",
|
|
|
|
"java.util.concurrent.LinkedBlockingQueue",
|
|
|
|
"java.util.concurrent.LinkedTransferQueue",
|
|
|
|
"java.util.concurrent.PriorityBlockingQueue",
|
|
|
|
"java.util.PriorityQueue",
|
|
|
|
// Requires a size for construction:
|
|
|
|
//"java.util.concurrent.ArrayBlockingQueue",
|
|
|
|
// Won't accept more than one element:
|
|
|
|
//"java.util.concurrent.SynchronousQueue",
|
|
|
|
// Requires "Delayed" elements:
|
|
|
|
//"java.util.concurrent.DelayQueue",
|
|
|
|
})
|
2016-08-09 10:21:47 -06:00
|
|
|
private String type;
|
|
|
|
|
2016-10-22 13:35:19 -07:00
|
|
|
@Param({
|
|
|
|
"1",
|
|
|
|
"10",
|
|
|
|
"100",
|
|
|
|
"1000",
|
|
|
|
"10000",
|
|
|
|
"100000"
|
|
|
|
})
|
|
|
|
private int size;
|
2016-08-09 10:21:47 -06:00
|
|
|
|
|
|
|
@Setup
|
2016-11-21 12:37:57 -08:00
|
|
|
@SuppressWarnings("unchecked")
|
2016-11-01 11:43:22 -07:00
|
|
|
public void setup() throws Exception {
|
|
|
|
queue = (Queue<String>)
|
|
|
|
Class.forName(type).newInstance();
|
2016-10-22 13:35:19 -07:00
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
queue.add(Integer.toString(i));
|
2016-08-09 10:21:47 -06:00
|
|
|
}
|
|
|
|
@Benchmark
|
2016-10-22 13:35:19 -07:00
|
|
|
public Queue<String> add() {
|
|
|
|
queue.add("test");
|
|
|
|
return queue;
|
2016-08-09 10:21:47 -06:00
|
|
|
}
|
|
|
|
@Benchmark
|
2016-11-01 11:43:22 -07:00
|
|
|
public String poll() {
|
|
|
|
return queue.poll();
|
2016-08-09 10:21:47 -06:00
|
|
|
}
|
|
|
|
}
|