PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/bench/kilim/bench/PingPong.java

http://github.com/kilim/kilim
Java | 104 lines | 79 code | 11 blank | 14 comment | 4 complexity | 75ef1d22a8afe0c208bb1118add1b455 MD5 | raw file
  1/* Copyright (c) 2006, Sriram Srinivasan
  2 *
  3 * You may distribute this software under the terms of the license 
  4 * specified in the file "License"
  5 */
  6
  7package kilim.bench;
  8
  9import kilim.Mailbox;
 10import kilim.Pausable;
 11import kilim.Task;
 12/*
 13 * This code is unnecessarily long for what it aims to do: bounce a message back
 14 * and forth between two tasks for a certain number of times. The reason for the
 15 * length is to compare against a similar example in the scala distribution.
 16 */
 17public class PingPong {
 18
 19    public static void main(String[] args) {
 20        Mailbox<Msg> pingmb = new Mailbox<Msg>();
 21        Mailbox<Msg> pongmb = new Mailbox<Msg>();
 22        new Ping(pingmb, pongmb).start();
 23        new Pong(pongmb).start();
 24        pingmb.putnb(new Msg(MsgType.Init, 100000, null));
 25        pingmb.putnb(new Msg(MsgType.Start, pingmb));
 26    }
 27}
 28
 29class Ping extends Task {
 30    Mailbox<Msg> mymb;
 31    Mailbox<Msg> pongmb;
 32    int origcount;
 33    int count;
 34    long beginTime;
 35    Ping(Mailbox<Msg> mb, Mailbox<Msg> pong) {
 36        mymb = mb;
 37        pongmb = pong;
 38    }
 39    
 40    public void execute() throws Pausable {
 41        while (true) {
 42            Msg m = mymb.get();
 43            switch(m.type) {
 44                case Init:
 45                    origcount = count = m.count;
 46                    beginTime = System.currentTimeMillis();
 47                    break;
 48                case Start:
 49                case PongMsg:
 50                    if (count > 0) {
 51//                        System.out.println("Ping: " + m + " " + count); System.out.flush();
 52//                        pongmb.put(new Msg(MsgType.PingMsg, mymb));
 53                        m.type = MsgType.PingMsg;
 54                        pongmb.put(m);
 55                        count--;
 56                    } else {
 57                        long  elapsed = System.currentTimeMillis() - beginTime;
 58                        System.out.println("Total time: " + elapsed + " millis, " 
 59                                + origcount + " rounds");
 60                        System.out.println("Time to send msg + context switch: " +
 61                                (elapsed * 1000.0 / 2 / origcount) + " micros");
 62                        System.exit(0);
 63                    }
 64                    break;
 65            }
 66        }
 67    }
 68}
 69
 70class Pong extends Task {
 71    Mailbox<Msg> mymb;
 72    Pong(Mailbox<Msg> mb) {
 73        mymb = mb;
 74    }
 75    
 76    public void execute() throws Pausable {
 77        while (true) {
 78            Msg m = mymb.get();
 79//            System.out.println("Pong: " + m); System.out.flush();
 80            switch(m.type) {
 81                case PingMsg:
 82//                    m.mb.put(new Msg(MsgType.PongMsg, null));
 83                    m.type = MsgType.PongMsg;
 84                    m.mb.put(m);
 85                    break;
 86            }
 87        }
 88    }
 89}
 90
 91
 92enum MsgType {Init, Start, PingMsg, PongMsg};
 93
 94class Msg {
 95    MsgType type;
 96    Mailbox<Msg> mb;
 97    int count; // for init
 98    Msg(MsgType t, int c, Mailbox<Msg> amb) {type = t; mb = amb; count = c;}
 99    Msg(MsgType t, Mailbox<Msg> amb) {type = t; mb = amb;}
100    
101    public String toString() {
102        return "" + System.identityHashCode(this) + " " + type;
103    }
104}