*** empty log message ***
[IRC.git] / Robust / Transactions / dstm2src / benchmark / Main_for_Book_BenchMArk.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package dstm2.benchmark;
7
8 import dstm2.Defaults;
9 import dstm2.Thread;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import java.util.Vector;
14 import javax.swing.KeyStroke;
15
16 /**
17  *
18  * @author navid
19  */
20 public class Main_for_Book_BenchMArk {
21     public static void main(String args[]){
22            // Code For Inserting Words From Random File to The Binary Tree
23     int numThreads = 2;//THREADS;
24     int numMillis  = Defaults.TIME;
25     int experiment = Defaults.EXPERIMENT;
26     String managerClassName = Defaults.MANAGER;
27     Class managerClass = null;
28     String benchmarkClassName = null;
29     Class benchmarkClass = null;
30     
31     
32     
33     String adapterClassName = Defaults.ADAPTER;
34     
35     // discard statistics from previous runs
36     Thread.clear();
37     // Parse and check the args
38     int argc = 0;
39     try {
40       while (argc < args.length) {
41         String option = args[argc++];
42         if (option.equals("-m"))
43           managerClassName = args[argc];
44         else if (option.equals("-b"))
45           benchmarkClassName = args[argc];
46         else if (option.equals("-t"))
47           numThreads = Integer.parseInt(args[argc]);
48         else if (option.equals("-n"))
49           numMillis = Integer.parseInt(args[argc]);
50         else if (option.equals("-e"))
51           experiment = Integer.parseInt(args[argc]);
52         else if (option.equals("-a"))
53           adapterClassName = args[argc];
54         else
55           reportUsageErrorAndDie();
56         argc++;
57       }
58     } catch (NumberFormatException e) {
59       System.out.println("Expected a number: " + args[argc]);
60       System.exit(0);
61     } catch (Exception e) {
62       reportUsageErrorAndDie();
63     }
64     
65     // Initialize contention manager.
66     try {
67       managerClass = Class.forName(Defaults.MANAGER);
68       Thread.setContentionManagerClass(managerClass);
69     } catch (ClassNotFoundException ex) {
70       reportUsageErrorAndDie();
71     }
72     
73     // Initialize adapter class
74     Thread.setAdapterClass(adapterClassName);
75     
76     // initialize benchmark
77     CustomBenchmark benchmark = null;
78     try {
79       benchmarkClass = Class.forName(benchmarkClassName);
80       benchmark = (CustomBenchmark) benchmarkClass.newInstance();
81       
82     } catch (InstantiationException e) {
83       System.out.format("%s does not implement dstm.benchmark.Benchmark: %s\n", benchmarkClass, e);
84       System.exit(0);
85     } catch (ClassCastException e) {
86       System.out.format("Exception when creating class %s: %s\n", benchmarkClass, e);
87       System.exit(0);
88     } catch (Exception e) {
89       e.printStackTrace(System.out);
90       System.exit(0);
91     }
92     
93     // Set up the benchmark
94     long startTime = 0;
95     
96     CustomThread[] thread = new CustomThread[numThreads];
97     System.out.println("Benchmark: " + benchmarkClass);
98     System.out.println("Adapter: " + adapterClassName);
99     System.out.println("Contention manager: " + managerClassName);
100     System.out.println("Threads: " + numThreads);
101     System.out.println("Mix: " + experiment + "% updates");
102     TransactionalIO.benchmarks.benchmark.init();
103     
104   
105   //  for(int k=0 ; k<400; k++){
106     
107     
108    // System.out.println((char)97);
109     int j = 97;
110     try {
111        for (int i = 0; i < numThreads; i++){
112              
113             //thread[i] = benchmark.createThread(experiment, (char)j);
114            
115             thread[i] = new CustomThread(benchmark);
116             j++;
117        }
118       
119        startTime = System.currentTimeMillis();
120        for (int i = 0; i < numThreads; i++)
121             thread[i].start();
122      // Thread.sleep(numMillis);
123     //  Thread.stop = true;     // notify threads to stop
124        for (int i = 0; i < numThreads; i++) {
125             thread[i].join();
126        }
127        
128     } catch (Exception e) {
129       e.printStackTrace(System.out);
130       System.exit(0);
131     }
132     long stopTime = System.currentTimeMillis();
133     
134     double elapsed = (double)(stopTime - startTime) / 1000.0;
135     
136     // Run the sanity check for this benchmark
137     try {
138       benchmark.sanityCheck();
139     } catch (Exception e) {
140       e.printStackTrace(System.out);
141     }
142     
143     long committed = Thread.totalCommitted;
144     long total = Thread.totalTotal;
145     if (total > 0) {
146       System.out.printf("Committed: %d\nTotal: %d\nPercent committed: (%d%%)\n",
147           committed,
148           total,
149           (100 * committed) / total);
150     } else {
151       System.out.println("No transactions executed!");
152     }
153     benchmark.report();
154     System.out.println("Elapsed time: " + elapsed + " seconds.");
155     System.out.println("------------------------------------------");
156     
157    /* BufferedReader dataIn = new BufferedReader(new
158                 InputStreamReader( System.in) );
159       
160     String name = "";
161     try{
162     name = dataIn.readLine();
163         }catch( IOException e ){
164             System.out.println("Error!");
165         }*/
166  //   }
167   //  benchmark.printResults();
168         
169     }
170     private static void reportUsageErrorAndDie() {
171         System.out.println("usage: dstm2.Main -b <benchmarkclass> [-m <managerclass>] [-t <#threads>] [-n <#time-in-ms>] [-e <experiment#>] [-a <adapter>]");
172         System.exit(0); 
173     }
174
175 }