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