PageRenderTime 38ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/bench/kilim/bench/LotsOfTasks.java

http://github.com/kilim/kilim
Java | 88 lines | 67 code | 10 blank | 11 comment | 15 complexity | 50bb2ea7889b2fca42b4e3fba219991f 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
11// Usage: java kilim.bench.LotsOfTasks -ntasks  
12//                 creates ntasks and waits for them to finish
13// Or     
14//Usage: java kilim.bench.LotsOfTasks ntasks pause 
15//          creates ntasks, which in turn block indefinitely on their mailboxes.
16public class LotsOfTasks {
17    static boolean block;
18    static int nTasks = 100000;
19    static int nRounds = 10;
20
21    public static void main(String[] args) throws Exception {
22        try {
23            for (int i = 0; i < args.length; i++) {
24                String arg = args[i];
25                if (arg.equalsIgnoreCase("-nRounds")) {
26                    nRounds = Integer.parseInt(args[++i]);
27                } else if (arg.equalsIgnoreCase("-nTasks")) {
28                    nTasks = Integer.parseInt(args[++i]);
29                } else if (arg.equalsIgnoreCase("-block")) {
30                    block = true;
31                }
32            }
33        } catch (NumberFormatException e) {
34            e.printStackTrace();
35            System.exit(0);
36        }
37        System.out.println("kilim.bench.LotsOfTasks -nTasks " + nTasks + (block ? " -block": "") + " -nRounds " + nRounds);
38
39        final Stopwatch s = new Stopwatch("Tasks(" + nTasks + ")");
40        for (int round = 1; round <= nRounds; round++) {
41            System.out.println("Round #" + round + " ================= ");
42            s.tick();
43            final Mailbox<ExitMsg> exitmb = new Mailbox<ExitMsg>();
44
45            System.out.println("Creating " + nTasks + (block ? " blocking tasks" : " tasks"));
46            for (int i = 1; i <= nTasks; i++) {
47                Task t = new LTask();
48                t.informOnExit(exitmb);
49                t.start();
50                if (i % 100000 == 0) {
51                    System.out.println("  created " + i + " tasks .... (contd.)");
52                }
53            }
54            profilerMark(); // dummy method to study memory consumption at this stage
55            if (!block) {
56                System.out.println("Waiting for completion");
57                for (int i = 1; i <= nTasks; i++) {
58                    exitmb.getb();
59                    if (i % 100000 == 0) {
60                        System.out.println("  " + i + " tasks finished.... (contd.)");
61                    }
62                }
63                ;
64            }
65            System.out.println("Round #" + round + " done:");
66            System.out.print("  ");
67            s.tickPrint(nTasks);
68            System.gc();
69            Thread.sleep(100); // give the GC a chance.
70        }
71        System.exit(0);
72    }
73
74    public static void profilerMark() {
75        // dummy method to help as a profiler breakpoint in JProfiler.
76    }
77}
78
79class LTask extends Task {
80    Mailbox<String> mymb = new Mailbox<String>();
81
82    public void execute() throws Pausable {
83        if (LotsOfTasks.block) {
84            mymb.get();
85        }
86    }
87
88}