/bench/kilim/bench/Chain.java
Java | 98 lines | 73 code | 15 blank | 10 comment | 13 complexity | 762620c7a40a75236449b0cfb0f767c3 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.*; 10 11public class Chain extends Task { 12 static class Mbx extends Mailbox<Integer>{} 13 static int nTasks = 500; 14 static int nMsgs = 10000; 15 16 Mbx mymb, nextmb; 17 static long startTime; 18 19 static Mailbox<Integer> signalMbx = new Mailbox<Integer>(); 20 21 22 public static void main(String[] args) { 23// Scheduler.setDefaultScheduler(new Scheduler(2)); // 2 threads. 24 try { 25 for (int i = 0; i < args.length; i++) { 26 String arg = args[i]; 27 if (arg.equalsIgnoreCase("-nMsgs")) { 28 nMsgs = Integer.parseInt(args[++i]); 29 } else if (arg.equalsIgnoreCase("-nTasks")) { 30 nTasks = Integer.parseInt(args[++i]); 31 } 32 } 33 } 34 catch (NumberFormatException e) { 35 System.err.println("Integer argument expected"); 36 } 37 System.out.println("Num tasks in chain: " + nTasks + ". Num messages sent:" + nMsgs); 38 for (int i = 0; i < 5; i++) { 39 bench(nMsgs, nTasks); 40 } 41 System.exit(0); 42 } 43 44 static void bench(int nMsgs, int nTasks) { 45 startTime = System.currentTimeMillis(); 46 47 Mbx mb = new Mbx(); 48 Mbx nextmb = null; 49 // Create a chain of tasks. 50 51 for (int i = 0; i < nTasks; i++) { 52 Task t = new Chain(mb, nextmb); 53 t.start(); 54 nextmb = mb; 55 mb = new Mbx(); 56 } 57 for (int i = 0; i < nMsgs; i++) { 58 nextmb.putnb(0); // enqueue a message for the head of the chain. 59 } 60 signalMbx.getb(); 61 System.out.println("Bench finished"); 62 try {Thread.sleep(500);}catch (Exception ignore) {} 63 System.gc(); 64 65 } 66 67 public Chain(Mbx mb, Mbx next) { 68 mymb = mb; 69 nextmb = next; 70 } 71 72 int numReceived = 0; 73 74 public void execute() throws Pausable { 75 while (true) { 76// System.out.println("Waiting: # " + id()); 77 Integer val = mymb.get(); 78// System.out.println("GET ===== # " + id()); 79 if (nextmb == null) { 80 numReceived++; 81 if (numReceived == nMsgs) { 82 done(); 83 break; 84 } 85 } else { 86 nextmb.put(val); 87 } 88 } 89 } 90 91 public void done() { 92 System.out.println("Elapsed time: " + 93 (System.currentTimeMillis() - startTime) 94 + " ms "); 95 signalMbx.putnb(0); 96// System.exit(0); 97 } 98 }