All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kilim.bench.Chain Maven / Gradle / Ivy

Go to download

Coroutines, continuations, fibers, actors and message passing for the JVM

There is a newer version: 2.0.2-jdk7
Show newest version
/* Copyright (c) 2006, Sriram Srinivasan
 *
 * You may distribute this software under the terms of the license 
 * specified in the file "License"
 */

package kilim.bench;

import kilim.*;

public class Chain extends Task {
    static class Mbx extends Mailbox{}
    static int nTasks = 500;
    static int nMsgs = 10000;
    
    Mbx  mymb, nextmb;
    static long startTime;
    
    static Mailbox signalMbx = new Mailbox();
    
    
    public static void main(String[] args) {
//        Scheduler.setDefaultScheduler(new Scheduler(2)); // 2 threads.
        try {
            for (int i = 0; i < args.length; i++) {
                String arg = args[i];
                if (arg.equalsIgnoreCase("-nMsgs")) {
                    nMsgs = Integer.parseInt(args[++i]);
                } else if (arg.equalsIgnoreCase("-nTasks")) {
                    nTasks = Integer.parseInt(args[++i]);
                }
            }
        } 
        catch (NumberFormatException e) {
            System.err.println("Integer argument expected");
        }
        System.out.println("Num tasks in chain: " + nTasks + ". Num messages sent:" + nMsgs);
        for (int i = 0; i < 5; i++) {
          bench(nMsgs, nTasks);
        }
        System.exit(0);
    }
    
    static void bench(int nMsgs, int nTasks) {
        startTime = System.currentTimeMillis();
        
        Mbx mb = new Mbx();
        Mbx nextmb = null;
        // Create a chain of tasks.
        
        for (int i = 0; i < nTasks; i++) {
           Task t = new Chain(mb, nextmb);
           t.start();
           nextmb = mb;
           mb = new Mbx();
        }
        for (int i = 0; i < nMsgs; i++) {
            nextmb.putnb(0); // enqueue a message for the head of the chain.
        }
        signalMbx.getb();
        System.out.println("Bench finished");
        try {Thread.sleep(500);}catch (Exception ignore) {}
        System.gc();
        
    }
    
    public Chain(Mbx mb, Mbx next) {
        mymb = mb;
        nextmb = next;
    }
    
    int numReceived = 0;
    
    public void execute() throws Pausable {
        while (true) {
//          System.out.println("Waiting: # " + id());
            Integer val = mymb.get();
//          System.out.println("GET ===== # " + id());
            if (nextmb == null) {
                numReceived++;
                if (numReceived == nMsgs) {
                  done();
                  break;
                }
            } else {
                nextmb.put(val);
            }
        }
    }
    
    public void done() {
      System.out.println("Elapsed time: " + 
          (System.currentTimeMillis() - startTime)
          + " ms "); 
      signalMbx.putnb(0);
//      System.exit(0);
    }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy