// String i/o benchmark, adapted from JavaSpaces book HellowWorld example
// (write/take 40 byte strings to/from space)

import net.jini.core.lease.Lease;
import net.jini.space.JavaSpace;
import net.jini.core.entry.Entry;

public class StringIO extends Master {

    public static void main(String[] args) {
	StringIO sio = new StringIO(args);
    }

    public StringIO(String[] args) {
        try {

	    if (args.length < 2) {
	    	System.out.print("Usage: java StringIO numIters numObjects\n");
		System.exit(-1);
	    }

	    int i;
            Message msg = new Message();
	    numIters = Integer.parseInt(args[0]);
	    numObjs = Integer.parseInt(args[1]);
	    Message template = new Message();

	    init(template);
	    msg.content = "Sequential writes / sequential takes    ";
	    reporter = new Reporter("Sequential write / sequential take test",
								numObjs,msg);
	    for (int iter=0; iter < numIters; iter++)
            {
	      i=0;
	      timer.checkp();
	      while (i++ < numObjs)
		  space.write(msg, null, Lease.FOREVER);
	      reporter.writeReport(timer.checkp());

	      i = 0;
	      while (i++ < numObjs)
		msg = (Message)space.take(snapshot,null,Long.MAX_VALUE);
	      reporter.takeReport(timer.checkp());
	    }

	    msg.content = "Interleaved read/write                  ";
            reporter = new Reporter("Interleaved write/take test "+
			     "(write/take times are combined and "+
			     "\n# throughput times s/b doubled)",numObjs,msg);

	    for (int iter=0; iter < numIters; iter++)
            {
	      i = 0;
	      timer.checkp();
	      while (i++ < numObjs) {
		space.write(msg, null, Lease.FOREVER);
		msg = (Message)space.take(snapshot,null,Long.MAX_VALUE);
	      }
	      reporter.writeReport(timer.checkp());
	      System.out.println("");
	    }

	    reporter.summarize();
	    System.exit(0);

        } catch (Exception e) { e.printStackTrace(); }
    }
}
