syndicate-java/src/test/java/org/syndicate_lang/actors/example/example2/Main.java

58 lines
1.9 KiB
Java
Raw Normal View History

2020-12-04 22:25:40 +00:00
package org.syndicate_lang.actors.example.example2;
import org.syndicate_lang.actors.Actor;
2020-12-05 22:50:41 +00:00
import org.syndicate_lang.actors.Remote;
2020-12-04 22:25:40 +00:00
import java.util.ArrayList;
import java.util.List;
2020-12-04 23:01:28 +00:00
import static java.lang.Integer.parseInt;
2020-12-04 22:25:40 +00:00
public class Main implements IForwarder {
public static void main(String[] args) throws InterruptedException {
Actor.convenientLogging();
2020-12-04 23:01:28 +00:00
Actor.forObject(new Main(parseInt(args[0]), parseInt(args[1]))).syncVoid(Main::boot).await();
2020-12-04 22:25:40 +00:00
Actor.awaitAll();
}
public final int _nActors;
public final int _nRounds;
public int _remainingToReceive;
2020-12-07 22:51:31 +00:00
public long _startTime = 0;
2020-12-04 22:25:40 +00:00
public Main(int nActors, int nRounds) {
this._nActors = nActors;
this._nRounds = nRounds;
this._remainingToReceive = nActors;
}
public void boot() {
2020-12-04 23:01:28 +00:00
Actor.log().info("Available processors: " + Runtime.getRuntime().availableProcessors());
2020-12-05 22:50:41 +00:00
List<IForwarder> _actors = new ArrayList<>();
final IForwarder me = Actor.ref(this).asyncProxy(IForwarder.class);
2020-12-04 22:25:40 +00:00
for (int i = 0; i < _nActors; i++) {
2020-12-05 22:50:41 +00:00
Remote<IForwarder> a = Actor.forObject(new Forwarder(i, _actors, me, this._nRounds));
a.getActor().link();
_actors.add(a.asyncProxy(IForwarder.class));
2020-12-04 22:25:40 +00:00
}
Actor.log().info("Start");
2020-12-07 22:51:31 +00:00
this._startTime = System.currentTimeMillis();
2020-12-05 22:50:41 +00:00
_actors.forEach((a) -> a.handleMessage(0));
2020-12-04 22:25:40 +00:00
}
@Override
public void handleMessage(int hopCount) {
this._remainingToReceive--;
if (this._remainingToReceive == 0) {
2020-12-07 22:51:31 +00:00
long delta = System.currentTimeMillis() - this._startTime;
long nMessages = _nActors * _nRounds;
double hz = nMessages / (delta / 1000.0);
2020-12-05 22:50:41 +00:00
Actor.current().stop();
2020-12-09 08:59:50 +00:00
Actor.log().info(String.format("Stop after %d messages; %.1f Hz",
nMessages,
hz));
2020-12-04 22:25:40 +00:00
}
}
}