Skip to content

Instantly share code, notes, and snippets.

@nitsanw
Created April 17, 2016 13:07
Show Gist options
  • Save nitsanw/0b9f94bfd80a265a36ce93497f098382 to your computer and use it in GitHub Desktop.
Save nitsanw/0b9f94bfd80a265a36ce93497f098382 to your computer and use it in GitHub Desktop.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jctools.jmh.throughput;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TimeUnit;
import org.jctools.queues.MpmcArrayTransferQueue;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class TransferQueueThroughputBackoffNone {
private static final Integer ONE = 777;
LinkedTransferQueue<Integer> ltqJDK;
MpmcArrayTransferQueue<Integer> atqJctools;
@Param("1")
int consumers;
@Setup(Level.Trial)
public void createQ() {
ltqJDK = new LinkedTransferQueue<Integer>();
atqJctools = new MpmcArrayTransferQueue<Integer>();
Runnable takeJdk = new Runnable() {
@Override
public void run() {
while (true) {
try {
ltqJDK.take();
} catch (InterruptedException e) {
}
}
}
};
Runnable takeJctools = new Runnable() {
@Override
public void run() {
while (true) {
try {
atqJctools.take();
} catch (InterruptedException e) {
}
}
}
};
for(int i=0;i<consumers;i++) {
startConsumer(takeJdk);
startConsumer(takeJctools);
}
}
private void startConsumer(Runnable takeJdk) {
Thread j = new Thread(takeJdk);
j.setDaemon(true);
j.start();
}
@Benchmark
public void jdkTransfer() throws InterruptedException {
ltqJDK.transfer(ONE);
}
@Benchmark
public void jctoolsTransfer() throws InterruptedException {
atqJctools.transfer(ONE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment