b611e14106a32a23e22e555e4bdd5f7d4edf1d29
[IRC.git] / Robust / Transactions / TransactionalIO / src / TransactionalIO / benchmarks / benchmark.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package TransactionalIO.benchmarks;
7
8 import TransactionalIO.core.Defaults;
9 import TransactionalIO.core.TransactionalFile;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.RandomAccessFile;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Random;
17 import java.util.Vector;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
20 import java.util.concurrent.locks.ReentrantReadWriteLock;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24
25 /**
26  *
27  * @author navid
28  */
29 public class benchmark {
30     static public HashMap TransactionalFiles = new HashMap();
31      static public HashMap hotwords = new HashMap();
32      static public HashMap names = new HashMap();
33      static public HashMap reversenames = new HashMap();
34     static int count = 0;
35     public static String msg = new String();
36     public static ReentrantLock lock = new ReentrantLock();
37     public static Vector transacctions = new Vector();
38     public static ReentrantLock filelock = new ReentrantLock();
39     public static Map m;
40     public static Map m2;
41     public static Map m3;
42     public static Map m4;
43     public static String[] stocks;
44     
45
46     public benchmark() {
47         
48     }
49      
50     private static void preparenamelist(){
51         try {
52             byte[] data = new byte[1];
53             char[] name = new char[20];
54             RandomAccessFile file = new RandomAccessFile("/home/navid/namelist.text", "rw");
55             RandomAccessFile file2 = new RandomAccessFile("/home/navid/financialtransaction.text", "rw");
56             RandomAccessFile file3 = new RandomAccessFile("/home/navid/accountbalance.text", "rw");
57             
58            
59           stocks  = new String[20];
60           stocks[0] = "Yahoo";
61           stocks[1] = "Google";
62           stocks[2] = "Microsoft";
63           stocks[3] = "Broadcom";
64           stocks[4] = "Sun";
65           stocks[5] = "Qualcom";
66           stocks[6] = "Intel";
67           stocks[7] = "WaMU";
68           stocks[8] = "BoA";
69           stocks[9] = "IMU";
70           stocks[10] = "BMW";
71           stocks[11] = "Nokia";
72           stocks[12] = "Motorolla";
73           stocks[13] = "Samsung";
74           stocks[14] = "TMobile";
75           stocks[15] = "ATT";
76           stocks[16] = "PRops";
77           stocks[17] = "Asia";
78           stocks[18] = "LOLa";
79           stocks[19] = "Brita";
80         /*          boolean hh = false; 
81            boolean found = false;    
82            while (true) {
83      
84                 if (found){
85                     System.out.println(file4.getFilePointer()-1);
86                     file4.seek(file4.getFilePointer()-1);
87                     file4.write(' ');
88                     file4.write(stocks[(int)(Math.random()*10)].getBytes());
89                     file4.write('\n');
90                 }
91                 if (hh)
92                     break;
93                 found = false;
94                 data[0] = 'a';
95                 while (data[0] != '\n') {
96                     int tt =0;
97                     tt = file4.read(data);
98                     found = true;
99                     if (tt == -1) {
100                         hh = true;
101                         break;
102                     } 
103                 }
104            }*/
105             
106             boolean flag = false;
107             boolean done = false;
108             int wordcounter = 0;
109             int counter =0;
110             while(true){
111                 if (flag)
112                     break;
113                 if (done){
114              //       System.out.println("At " + wordcounter + " inserted " +String.copyValueOf(name, 0, counter));
115                     m3.put(Integer.valueOf(wordcounter), String.copyValueOf(name, 0, counter));
116                     m4.put(String.copyValueOf(name, 0, counter), Integer.valueOf(wordcounter));
117                     wordcounter++;
118                     done = false;
119                 }
120                 counter = 0;    
121                 data[0] = 'a';
122                 while (data[0] != '\n') {
123                     int res;
124                     res = file.read(data);
125                     if (res == -1) {
126                         flag = true;
127                         break;
128                     }
129                     //System.out.println((char)data[0]);
130                     if (!(Character.isLetter((char) data[0]))) {
131                         continue;
132                    }
133                    name[counter] = (char)data[0];
134                    done = true;
135                    counter++;
136                 }
137           }
138             
139             
140       /*    counter = 0;  
141           while (counter <30000)  {
142               int index1 = (int)(Math.random()*50);
143               int stocktrade = (int)(Math.random()*100);
144               while (stocktrade == 0)
145                   stocktrade = (int)(Math.random()*100);
146               int index2 = (int)(Math.random()*50);
147               while (index2 == index1)
148                   index2 = (int)(Math.random()*50);
149               //System.out.println(index);
150               String towrite = (String)m3.get(Integer.valueOf(index1)) + " ";
151               towrite += String.valueOf(stocktrade) + " ";
152               towrite += (String)m3.get(Integer.valueOf(index2)) + " ";
153               towrite += stocks[(int)(Math.random()*20)] + "\n";
154               
155               file2.write(towrite.getBytes());
156             //  System.out.println(towrite);
157               counter++;
158           }*/
159          // for (int i=0; i<50*Defaults.FILEFRAGMENTSIZE; i++)
160               //file3.write('');
161         
162           
163           for (int i=0; i<50; i++){
164               String towrite = (String)m3.get(Integer.valueOf(i)) +"\n";
165               for (int j=0; j<stocks.length; j++)
166                 towrite +=  stocks[j] + " Stock Balance: " + ((int)(Math.random()*100+100)) + "             \n";
167               System.out.println(towrite);
168               file3.write(towrite.getBytes());
169               while (file3.getFilePointer()<(i+1)*Defaults.FILEFRAGMENTSIZE)
170                   file3.write(new String(" ").getBytes());    
171           }
172           
173
174         /*  for (int i=0; i<10; i++)
175               System.out.println((char)f[i]);*/
176           file.close();
177 //          file2.close();
178           file3.close();
179         } catch (IOException ex) {
180             Logger.getLogger(benchmark.class.getName()).log(Level.SEVERE, null, ex);
181         } 
182        
183     
184     }
185
186     public static void init(){
187         try {
188
189             m3 = Collections.synchronizedMap(names);
190             m4 = Collections.synchronizedMap(reversenames);
191
192
193
194
195            preparenamelist();
196             count = 0;
197             m = Collections.synchronizedMap(TransactionalFiles);
198             TransactionalFile tr = new TransactionalFile("/home/navid/randomwords.text", "rw");
199             m.put(String.valueOf(count), tr);
200             count++;
201             TransactionalFile tr2 = new TransactionalFile("/home/navid/input.text", "rw");
202             m.put(String.valueOf(count), tr2);
203             count++;
204             TransactionalFile tr3 = new TransactionalFile("/home/navid/iliad.text", "rw");
205             m.put(String.valueOf(count), tr3);
206             count++;
207             TransactionalFile tr4 = new TransactionalFile("/home/navid/counter_benchmark_output.text", "rw");
208             m.put(String.valueOf(count), tr4);
209             count++;
210
211             TransactionalFile tr5 = new TransactionalFile("/home/navid/financialtransaction.text", "rw");
212             m.put(String.valueOf(count), tr5);
213
214             count++;
215
216             TransactionalFile tr6 = new TransactionalFile("/home/navid/accountbalance.text", "rw");
217             m.put(String.valueOf(count), tr6);
218
219             count++;
220
221             TransactionalFile tr7 = new TransactionalFile("/home/navid/financialtransactionlog.text", "rw");
222             m.put(String.valueOf(count), tr7);
223
224             count++;
225
226             RandomAccessFile tr8 = new RandomAccessFile("/home/navid/accountbalance.text", "rw");
227             m.put(String.valueOf(count), tr8);
228 //
229             count++;
230
231             RandomAccessFile tr9 = new RandomAccessFile("/home/navid/financialtransactionlog.text", "rw");
232             m.put(String.valueOf(count), tr9);
233
234             count++;
235
236             int index = 97;
237             for (int i = 0; i < 26; i++) {
238                 //     System.out.println("here " +String.valueOf((char)(index+i)));
239                 m.put(String.valueOf((char) (index+i)), new TransactionalFile("/home/navid/" + String.valueOf((char) (index+i)) + ".text", "rw"));
240                 count++;
241             }
242             count = 0;
243             m2 = Collections.synchronizedMap(hotwords);
244             m2.put(Integer.valueOf(count), "Polydamas");
245             count++;
246             m2.put(Integer.valueOf(count), "Cebriones");
247             count++;
248             m2.put(Integer.valueOf(count), "Eurybates");
249             count++;
250             m2.put(Integer.valueOf(count), "Menoetius");
251             count++;
252             m2.put(Integer.valueOf(count), "countless");
253             count++;
254             m2.put(Integer.valueOf(count), "huntsman");
255             count++;
256             m2.put(Integer.valueOf(count), "presence");
257             count++;
258             m2.put(Integer.valueOf(count), "pursuit");
259             count++;
260             m2.put(Integer.valueOf(count), "masterfully");
261             count++;
262             m2.put(Integer.valueOf(count), "unweariable");
263         } catch (FileNotFoundException ex) {
264             Logger.getLogger(benchmark.class.getName()).log(Level.SEVERE, null, ex);
265         }
266         
267     }
268     
269
270 }