Permalink
Browse files

Moved ping-pong to own project

  • Loading branch information...
1 parent 1ed6c96 commit 612d0389f85bad8e9f7de8ce85ae1b99bd877997 @skunkiferous skunkiferous committed Mar 17, 2013
View
35 src/test/java/org/agilewiki/jactor/pingpong/blocking/PingPongTest.java
@@ -1,35 +0,0 @@
-package org.agilewiki.jactor.pingpong.blocking;
-
-import org.agilewiki.jactor.JAMailboxFactory;
-import org.agilewiki.jactor.MailboxFactory;
-
-import junit.framework.TestCase;
-
-/**
- * Tests the number of request/reply cycles per second.
- */
-public class PingPongTest extends TestCase {
- /** How many repeats? */
- private static final int REPEATS = 10;
-
- public void testRequestReplyCycleSpeed() throws Exception {
- final MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(2);
-
- int count = 0;
- double duration = 0.0d;
- for (int i = 1; i <= REPEATS; i++) {
- System.out.println("Run #" + i + "/" + REPEATS);
- final Pinger pinger = new Pinger(mailboxFactory.createMailbox(),
- "Pinger");
- final Ponger ponger = new Ponger(mailboxFactory.createMailbox());
- final Pinger.HammerResult result = pinger.hammer(ponger);
- count += result.pings();
- duration += result.duration();
- System.out.println(result);
- }
- System.out.println("Average Request/Reply Cycles/sec = "
- + (count / duration));
-
- mailboxFactory.close();
- }
-}
View
100 src/test/java/org/agilewiki/jactor/pingpong/blocking/Pinger.java
@@ -1,100 +0,0 @@
-package org.agilewiki.jactor.pingpong.blocking;
-
-import org.agilewiki.jactor.Actor;
-import org.agilewiki.jactor.JAFuture;
-import org.agilewiki.jactor.Mailbox;
-import org.agilewiki.jactor.RP;
-import org.agilewiki.jactor.lpc.JLPCActor;
-import org.agilewiki.jactor.lpc.Request;
-
-/**
- * The Pinger's job is to hammer the Ponger with ping() request,
- * to count how many can be done in one second.
- */
-public class Pinger extends JLPCActor {
- /* Hammer request result */
- public static final class HammerResult {
- /** Number of pings sent. */
- private final int pings;
-
- /** Duration. */
- private final double duration;
-
- /** Constructor */
- public HammerResult(final int _pings, final double _duration) {
- pings = _pings;
- duration = _duration;
- }
-
- /** toString */
- @Override
- public String toString() {
- return "Sent " + pings + " pings in " + duration + " seconds";
- }
-
- /** Number of pings sent. */
- public int pings() {
- return pings;
- }
-
- /** Duration. */
- public double duration() {
- return duration;
- }
- }
-
- /** How long to send pings? */
- private static final long PING_FOR_IN_MS = 1000L;
-
- /** The name of the pinger. */
- private final String name;
-
- /** A Hammer request, targeted at Pinger. */
- private static class HammerRequest extends Request<HammerResult,Pinger> {
- /** The Ponger to hammer. */
- private final Ponger ponger;
-
- /** Creates a hammer request, with the targeted Ponger. */
- public HammerRequest(final Ponger _ponger) {
- ponger = _ponger;
- }
-
- /** Process the hammer request. */
- @SuppressWarnings("unchecked")
- @Override
- public void processRequest(final JLPCActor targetActor,
- @SuppressWarnings("rawtypes") final RP responseProcessor)
- throws Exception {
- final Pinger pinger = (Pinger) targetActor;
- final String name = pinger.name;
- int count = 0;
- final long start = System.nanoTime();
- final long delay = PING_FOR_IN_MS * 1000000L;
- long now;
- while ((now = System.nanoTime()) - start < delay) {
- ponger.ping(name);
- count++;
- }
- responseProcessor.processResponse(new HammerResult(count,
- ((now - start) / 1000000000.0d)));
- }
-
- @Override
- public boolean isTargetType(Actor targetActor) {
- return Pinger.class.isInstance(targetActor);
- }
- }
-
- /** Creates a Pinger, with it's own mailbox and name.
- * @throws Exception */
- public Pinger(final Mailbox mbox, final String _name) throws Exception {
- initialize(mbox);
- name = _name;
- }
-
- /** Tells the pinger to hammer the Ponger. Describes the speed in the result. */
- public HammerResult hammer(final Ponger ponger) throws Exception {
- final JAFuture future = new JAFuture();
- return new HammerRequest(ponger).send(future, this);
- }
-}
View
53 src/test/java/org/agilewiki/jactor/pingpong/blocking/Ponger.java
@@ -1,53 +0,0 @@
-package org.agilewiki.jactor.pingpong.blocking;
-
-import org.agilewiki.jactor.Actor;
-import org.agilewiki.jactor.JAFuture;
-import org.agilewiki.jactor.Mailbox;
-import org.agilewiki.jactor.RP;
-import org.agilewiki.jactor.lpc.JLPCActor;
-import org.agilewiki.jactor.lpc.Request;
-
-/**
- * Receives Pings, and send Pongs back.
- */
-public class Ponger extends JLPCActor {
- /** Some mutable data of Ponger, which must be access in a thread-safe way. */
- private int pings;
-
- /** A Ping request, targeted at Ponger. */
- private static class PingRequest extends Request<String,Ponger> {
- private final String from;
-
- public PingRequest(final String _from) {
- from = _from;
- }
-
- /** Processes the ping(String) request, from within the Thread of the Ponger. */
- @SuppressWarnings("unchecked")
- @Override
- public void processRequest(final JLPCActor targetActor,
- @SuppressWarnings("rawtypes") final RP responseProcessor)
- throws Exception {
- final Ponger ponger = (Ponger) targetActor;
- responseProcessor.processResponse("Pong " + (ponger.pings++) + " to "
- + from + "!");
- }
-
- @Override
- public boolean isTargetType(Actor targetActor) {
- return Ponger.class.isInstance(targetActor);
- }
- }
-
- /** Creates a Ponger, with it's own mailbox.
- * @throws Exception */
- public Ponger(final Mailbox mbox) throws Exception {
- initialize(mbox);
- }
-
- /** Sends a ping(String) request to the Ponger. Blocks and returns response. */
- public String ping(final String from) throws Exception {
- final JAFuture future = new JAFuture();
- return new PingRequest(from).send(future, this);
- }
-}
View
35 src/test/java/org/agilewiki/jactor/pingpong/iterator/PingPongTest.java
@@ -1,35 +0,0 @@
-package org.agilewiki.jactor.pingpong.iterator;
-
-import org.agilewiki.jactor.JAMailboxFactory;
-import org.agilewiki.jactor.MailboxFactory;
-
-import junit.framework.TestCase;
-
-/**
- * Tests the number of request/reply cycles per second.
- */
-public class PingPongTest extends TestCase {
- /** How many repeats? */
- private static final int REPEATS = 10;
-
- public void testRequestReplyCycleSpeed() throws Exception {
- final MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(2);
-
- int count = 0;
- double duration = 0.0d;
- for (int i = 1; i <= REPEATS; i++) {
- System.out.println("Run #" + i + "/" + REPEATS);
- final Pinger pinger = new Pinger(mailboxFactory.createMailbox(),
- "Pinger");
- final Ponger ponger = new Ponger(pinger.getMailbox());
- final Pinger.HammerResult3 result = pinger.hammer(ponger);
- count += result.pings();
- duration += result.duration();
- System.out.println(result);
- }
- System.out.println("Average Request/Reply Cycles/sec = "
- + (count / duration));
-
- mailboxFactory.close();
- }
-}
View
129 src/test/java/org/agilewiki/jactor/pingpong/iterator/Pinger.java
@@ -1,129 +0,0 @@
-package org.agilewiki.jactor.pingpong.iterator;
-
-import org.agilewiki.jactor.Actor;
-import org.agilewiki.jactor.JABidiIterator;
-import org.agilewiki.jactor.JAFuture;
-import org.agilewiki.jactor.Mailbox;
-import org.agilewiki.jactor.RP;
-import org.agilewiki.jactor.lpc.JLPCActor;
-import org.agilewiki.jactor.lpc.Request;
-
-/**
- * The Pinger's job is to hammer the Ponger with ping() request,
- * to count how many can be done in one second.
- */
-public class Pinger extends JLPCActor {
- /* Hammer request result */
- public static final class HammerResult3 {
- /** Number of pings sent. */
- private final int pings;
-
- /** Duration. */
- private final double duration;
-
- /** Constructor */
- public HammerResult3(final int _pings, final double _duration) {
- pings = _pings;
- duration = _duration;
- }
-
- /** toString */
- @Override
- public String toString() {
- return "Sent " + pings + " pings in " + duration + " seconds";
- }
-
- /** Number of pings sent. */
- public int pings() {
- return pings;
- }
-
- /** Duration. */
- public double duration() {
- return duration;
- }
- }
-
- /** How long to send pings? */
- private static final long PING_FOR_IN_MS = 1000L;
-
- /** The name of the pinger. */
- private final String name;
-
- /** A Hammer request, targeted at Pinger. */
- private static class HammerRequest3 extends Request<HammerResult3,Pinger> {
- private static final long DELAY = PING_FOR_IN_MS * 1000000L;
-
- /** The Ponger to hammer. */
- private final Ponger ponger;
-
- /** Arival from hammer request. */
- private long start;
-
- /** The number of pings. */
- private int count;
-
- /** The pinger */
- private Pinger pinger;
-
- /** Creates a hammer request, with the targeted Ponger. */
- public HammerRequest3(final Ponger _ponger) {
- ponger = _ponger;
- }
-
- /** Process the hammer request. */
- @SuppressWarnings("unchecked")
- @Override
- public void processRequest(final JLPCActor targetActor,
- @SuppressWarnings("rawtypes") final RP _responseProcessor)
- throws Exception {
- pinger = (Pinger) targetActor;
- start = System.nanoTime();
-
- (new JABidiIterator() {
- @Override
- protected void sendRequest(@SuppressWarnings("rawtypes") RP responseProcessor) throws Exception {
- ponger.ping(pinger, responseProcessor);
- }
-
- @Override
- protected Object processResponse(Object response) throws Exception {
- // response is ignored in this case, but we *could* have used it,
- // which is the main thing.
- final long now = System.nanoTime();
- count++;
- if (now - start < DELAY) {
- return null;
- } else {
- return new HammerResult3(count, ((now - start) / 1000000000.0d));
- }
- }
- }).iterate(_responseProcessor);
- }
-
- @Override
- public boolean isTargetType(Actor targetActor) {
- return Pinger.class.isInstance(targetActor);
- }
- }
-
- /** Creates a Pinger, with it's own mailbox and name.
- * @throws Exception */
- public Pinger(final Mailbox mbox, final String _name) throws Exception {
- initialize(mbox);
- name = _name;
- }
-
- /** Tells the pinger to hammer the Ponger. Describes the speed in the result. */
- public HammerResult3 hammer(final Ponger ponger) throws Exception {
- final JAFuture future = new JAFuture();
- return new HammerRequest3(ponger).send(future, this);
- }
-
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
-}
View
52 src/test/java/org/agilewiki/jactor/pingpong/iterator/Ponger.java
@@ -1,52 +0,0 @@
-package org.agilewiki.jactor.pingpong.iterator;
-
-import org.agilewiki.jactor.Actor;
-import org.agilewiki.jactor.JAFuture;
-import org.agilewiki.jactor.Mailbox;
-import org.agilewiki.jactor.RP;
-import org.agilewiki.jactor.lpc.JLPCActor;
-import org.agilewiki.jactor.lpc.Request;
-
-/**
- * Receives Pings, and send Pongs back.
- */
-public class Ponger extends JLPCActor {
- /** Some mutable data of Ponger, which must be access in a thread-safe way. */
- private int pings;
-
- /** A Ping request, targeted at Ponger. */
- private static class PingRequest3 extends Request<String,Ponger> {
- private final String from;
-
- public PingRequest3(final String _from) {
- from = _from;
- }
-
- /** Processes the ping(String) request, from within the Thread of the Ponger. */
- @SuppressWarnings("unchecked")
- @Override
- public void processRequest(final JLPCActor targetActor,
- @SuppressWarnings("rawtypes") final RP responseProcessor)
- throws Exception {
- final Ponger ponger = (Ponger) targetActor;
- responseProcessor.processResponse("Pong " + (ponger.pings++) + " to "
- + from + "!");
- }
-
- @Override
- public boolean isTargetType(Actor targetActor) {
- return Ponger.class.isInstance(targetActor);
- }
- }
-
- /** Creates a Ponger, with it's own mailbox.
- * @throws Exception */
- public Ponger(final Mailbox mbox) throws Exception {
- initialize(mbox);
- }
-
- /** Sends a ping(String) request to the Ponger. Blocks and returns response. */
- public void ping(final Pinger pinger, final RP<String> rp) throws Exception {
- new PingRequest3(pinger.getName()).send(pinger, this, rp);
- }
-}
View
35 src/test/java/org/agilewiki/jactor/pingpong/stackoverflow/PingPongTest.java
@@ -1,35 +0,0 @@
-package org.agilewiki.jactor.pingpong.stackoverflow;
-
-import org.agilewiki.jactor.JAMailboxFactory;
-import org.agilewiki.jactor.MailboxFactory;
-
-import junit.framework.TestCase;
-
-/**
- * Tests the number of request/reply cycles per second.
- */
-public class PingPongTest extends TestCase {
- /** How many repeats? */
- private static final int REPEATS = 10;
-
- public void testRequestReplyCycleSpeed() throws Exception {
- final MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(2);
-
- int count = 0;
- double duration = 0.0d;
- for (int i = 1; i <= REPEATS; i++) {
- System.out.println("Run #" + i + "/" + REPEATS);
- final Pinger pinger = new Pinger(mailboxFactory.createMailbox(),
- "Pinger");
- final Ponger ponger = new Ponger(mailboxFactory.createMailbox());
- final Pinger.HammerResult2 result = pinger.hammer(ponger);
- count += result.pings();
- duration += result.duration();
- System.out.println(result);
- }
- System.out.println("Average Request/Reply Cycles/sec = "
- + (count / duration));
-
- mailboxFactory.close();
- }
-}
View
140 src/test/java/org/agilewiki/jactor/pingpong/stackoverflow/Pinger.java
@@ -1,140 +0,0 @@
-package org.agilewiki.jactor.pingpong.stackoverflow;
-
-import org.agilewiki.jactor.Actor;
-import org.agilewiki.jactor.JAFuture;
-import org.agilewiki.jactor.Mailbox;
-import org.agilewiki.jactor.RP;
-import org.agilewiki.jactor.lpc.JLPCActor;
-import org.agilewiki.jactor.lpc.Request;
-
-/**
- * The Pinger's job is to hammer the Ponger with ping() request,
- * to count how many can be done in one second.
- */
-public class Pinger extends JLPCActor {
- /* Hammer request result */
- public static final class HammerResult2 {
- /** Number of pings sent. */
- private final int pings;
-
- /** Duration. */
- private final double duration;
-
- /** Constructor */
- public HammerResult2(final int _pings, final double _duration) {
- pings = _pings;
- duration = _duration;
- }
-
- /** toString */
- @Override
- public String toString() {
- return "Sent " + pings + " pings in " + duration + " seconds";
- }
-
- /** Number of pings sent. */
- public int pings() {
- return pings;
- }
-
- /** Duration. */
- public double duration() {
- return duration;
- }
- }
-
- /** How long to send pings? */
- private static final long PING_FOR_IN_MS = 1000L;
-
- /** The name of the pinger. */
- private final String name;
-
- /** A Hammer request, targeted at Pinger. */
- private static class HammerRequest2 extends Request<HammerResult2,Pinger> {
- private static final long DELAY = PING_FOR_IN_MS * 1000000L;
-
- /** The Ponger to hammer. */
- private final Ponger ponger;
-
- /** The responseProcessor from the test, to call when done. */
- private RP<HammerResult2> responseProcessor;
-
- /** Arival from hammer request. */
- private long start;
-
- /** The number of pings. */
- private int count;
-
- /** The pinger */
- private Pinger pinger;
-
- /** Creates a hammer request, with the targeted Ponger. */
- public HammerRequest2(final Ponger _ponger) {
- ponger = _ponger;
- }
-
- private void ping() throws Exception {
- ponger.ping(pinger, new RP<String>() {
- @Override
- public void processResponse(String response)
- throws Exception {
- final long now = System.nanoTime();
- count++;
- if (now - start < DELAY) {
- // again ...
- ping();
- } else {
- responseProcessor.processResponse(new HammerResult2(count,
- ((now - start) / 1000000000.0d)));
- }
-
- }
- });
- }
-
- /** Process the hammer request. */
- @SuppressWarnings("unchecked")
- @Override
- public void processRequest(final JLPCActor targetActor,
- @SuppressWarnings("rawtypes") final RP _responseProcessor)
- throws Exception {
- pinger = (Pinger) targetActor;
- responseProcessor = (RP<HammerResult2>) _responseProcessor;
- start = System.nanoTime();
- boolean again = true;
- while (again) {
- try {
- ping();
- again = false;
- } catch (StackOverflowError e) {
- // NOP
- }
- }
- }
-
- @Override
- public boolean isTargetType(Actor targetActor) {
- return Pinger.class.isInstance(targetActor);
- }
- }
-
- /** Creates a Pinger, with it's own mailbox and name.
- * @throws Exception */
- public Pinger(final Mailbox mbox, final String _name) throws Exception {
- initialize(mbox);
- name = _name;
- }
-
- /** Tells the pinger to hammer the Ponger. Describes the speed in the result. */
- public HammerResult2 hammer(final Ponger ponger) throws Exception {
- final JAFuture future = new JAFuture();
- return new HammerRequest2(ponger).send(future, this);
- }
-
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
-}
View
52 src/test/java/org/agilewiki/jactor/pingpong/stackoverflow/Ponger.java
@@ -1,52 +0,0 @@
-package org.agilewiki.jactor.pingpong.stackoverflow;
-
-import org.agilewiki.jactor.Actor;
-import org.agilewiki.jactor.JAFuture;
-import org.agilewiki.jactor.Mailbox;
-import org.agilewiki.jactor.RP;
-import org.agilewiki.jactor.lpc.JLPCActor;
-import org.agilewiki.jactor.lpc.Request;
-
-/**
- * Receives Pings, and send Pongs back.
- */
-public class Ponger extends JLPCActor {
- /** Some mutable data of Ponger, which must be access in a thread-safe way. */
- private int pings;
-
- /** A Ping request, targeted at Ponger. */
- private static class PingRequest2 extends Request<String,Ponger> {
- private final String from;
-
- public PingRequest2(final String _from) {
- from = _from;
- }
-
- /** Processes the ping(String) request, from within the Thread of the Ponger. */
- @SuppressWarnings("unchecked")
- @Override
- public void processRequest(final JLPCActor targetActor,
- @SuppressWarnings("rawtypes") final RP responseProcessor)
- throws Exception {
- final Ponger ponger = (Ponger) targetActor;
- responseProcessor.processResponse("Pong " + (ponger.pings++) + " to "
- + from + "!");
- }
-
- @Override
- public boolean isTargetType(Actor targetActor) {
- return Ponger.class.isInstance(targetActor);
- }
- }
-
- /** Creates a Ponger, with it's own mailbox.
- * @throws Exception */
- public Ponger(final Mailbox mbox) throws Exception {
- initialize(mbox);
- }
-
- /** Sends a ping(String) request to the Ponger. Blocks and returns response. */
- public void ping(final Pinger pinger, final RP<String> rp) throws Exception {
- new PingRequest2(pinger.getName()).send(pinger, this, rp);
- }
-}

0 comments on commit 612d038

Please sign in to comment.