public class CircularRoutingLogic implements RoutingLogic {
final AtomicInteger next = new AtomicInteger(-1);
@Override
public Routee select(Object message, IndexedSeq<Routee> routees) {
final int size = routees.size();
// null should be replaced by NoRoutee Akka/Scala instance.
return size == 0 ? null : routees.apply(next.accumulateAndGet(1, (index, increment) -> ++index >= size ? 0 : index));
}
}
/**
* Atomically updates the current value with the results of
* applying the given function to the current and given values,
* returning the updated value. The function should be
* side-effect-free, since it may be re-applied when attempted
* updates fail due to contention among threads. The function
* is applied with the current value as its first argument,
* and the given update as the second argument.
*
* @param x the update value
* @param accumulatorFunction a side-effect-free function of two arguments
* @return the updated value
* @since 1.8
*/
public final int accumulateAndGet(int x,
IntBinaryOperator accumulatorFunction) {
int prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsInt(prev, x);
} while (!compareAndSet(prev, next));
return next;
}
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
final class RoundRobinRoutingLogic extends RoutingLogic {
val next = new AtomicLong(0)
override def select(message: Any, routees: immutable.IndexedSeq[Routee]): Routee =
if (routees.isEmpty) NoRoutee
else routees((next.getAndIncrement % routees.size).asInstanceOf[Int])
}
final class RoundRobinRoutingLogic extends RoutingLogic {
val next = new AtomicInteger(0)
override def select(message: Any, routees: immutable.IndexedSeq[Routee]): Routee =
if (routees.isEmpty) NoRoutee
else routees(Math.abs(next.getAndIncrement % routees.size).asInstanceOf[Int])
}
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@State(Scope.Benchmark)
public class CASvsMOD {
private final AtomicInteger nextInt = new AtomicInteger();
private final AtomicLong nextLong = new AtomicLong();
private int nextInt(int routees) {
return nextInt.accumulateAndGet(1, (index, increment) -> ++index < routees ? index : 0);
}
@Benchmark
public void intCAS_1() {
nextInt(1);
}
@Benchmark
public void intCAS_4() {
nextInt(4);
}
@Benchmark
public void intCAS_8() {
nextInt(8);
}
private int nextIntWithMod(int routees) {
return Math.abs(nextInt.incrementAndGet() % routees);
}
@Benchmark
public void intMOD_1() {
nextIntWithMod(1);
}
@Benchmark
public void intMOD_4() {
nextIntWithMod(4);
}
@Benchmark
public void intMOD_8() {
nextIntWithMod(8);
}
private long nextLong(int routees) {
return Math.abs(nextLong.incrementAndGet() % routees);
}
@Benchmark
public void longMOD_1() {
nextLong(1);
}
@Benchmark
public void longMOD_4() {
nextLong(4);
}
@Benchmark
public void longMOD_8() {
nextLong(8);
}
public static void main(String[] args) throws RunnerException, IOException {
final Options opt = new OptionsBuilder().
include(CASvsMOD.class.getSimpleName()).
warmupIterations(5).
measurementIterations(5).
forks(1).
build();
new Runner(opt).run();
}
}
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Patrik Nordwall
Typesafe - Reactive apps on the JVM
Twitter: @patriknw