2 import java
.util
.Random
;
5 public static Random generator
= new Random();
6 public static int getRandom(int end
)
8 double val
= generator
.nextDouble();
13 public static int singleTuple(Connection con
, boolean flag
)throws Exception
15 PreparedStatement stmt
= null ;
19 String buf
= " SELECT unique1, unique2, stringu1, stringu2 from big1 where unique2=?;";
24 String buf
= "SELECT unique1, unique2, stringu1, stringu2 from big1 where unique1=?;";
28 stmt
= con
.prepareStatement(stmtStr
);
29 int count
=0, recordCount
=0;
31 long start
=0, end
=0, curr
=0;
36 for(int i
=0; i
<=100; i
++) {
39 start
= System
.nanoTime();
40 rs
= stmt
.executeQuery();
48 end
= System
.nanoTime();
51 System
.out
.println("No of records for Q1: "+ recordCount
);
61 return (int)tot
/100/1000;
64 public static int onePerSel(Connection con
, boolean flag
) throws Exception
66 PreparedStatement stmt
= null;
67 int searchVal
= getRandom(9999 - 100);
68 int searchValEnd
= searchVal
+ 99;
73 String buf
= "SELECT * from big1 where unique2 between "+
74 searchVal
+ " and "+ searchValEnd
+ ";";
77 String buf
= "SELECT * from big1 where unique1 between "+
78 searchVal
+ " and "+ searchValEnd
+ ";";
81 stmt
= con
.prepareStatement(stmtStr
);
82 int count
=0, recordCount
=0;
84 long start
=0, end
=0, curr
=0;
87 for (int i
=0 ; i
<= 100 ; i
++) {
88 start
= System
.nanoTime();
90 rs
= stmt
.executeQuery();
99 end
= System
.nanoTime();
102 if (recordCount
!= 100)
104 System
.out
.println("No. of records for Q2: "+recordCount
);
113 return (int)tot
/100/1000;
117 public static int aggregate(Connection con
, int val
, boolean flag
)throws Exception
119 PreparedStatement stmt
= null;
124 String buf
= "SELECT MIN(unique2) from big1;";
127 String buf
= "SELECT MIN(unique1) from big1;";
134 String buf
= "SELECT SUM(unique2) from big1;";
137 String buf
= "SELECT SUM(unique1) from big1;";
144 String buf
= "SELECT MIN(unique2) FROM big1 GROUP BY onepercent;";
147 String buf
= "SELECT MIN(unique1) FROM big1 GROUP BY onepercent;";
154 String buf
= "SELECT SUM(unique2) FROM big1 GROUP BY onepercent;";
157 String buf
= "SELECT SUM(unique1) FROM big1 GROUP BY onepercent;";
162 stmt
= con
.prepareStatement(stmtStr
);
163 int count
= 0, recordCount
;
165 long start
= 0,end
= 0,curr
= 0;
169 for(int i
=0;i
<10;i
++){
170 start
= System
.nanoTime();
171 rs
= stmt
.executeQuery();
180 end
= System
.nanoTime();
182 if((val
==1 || val
==2) && recordCount
!=1)
184 System
.out
.println("No. of records for Q4-7(" + val
+" ): " + recordCount
);
187 if((val
==4 || val
==3) && recordCount
!=100)
189 System
.out
.println("No. of records for Q4-7("+ val
+ "): " + recordCount
);
197 return (int)tot
/10/1000;
202 public static int dmlstatement(Connection con
, int val
, boolean flag
)throws Exception
204 PreparedStatement stmt
= null;
208 String buf
= "insert into big1 values(?,?,0,2,0,10,50,688,1950,4950,9950,1,100,'MXXXXXXXXXXXXXXXXXXXXXXXXXGXXXXXXXXXXXXXXXXXXXXXXXXC','GXXXXXXXXXXXXXXXXXXXXXXXXXCXXXXXXXXXXXXXXXXXXXXXXXXA','OXXXXXXXXXXXXXXXXXXXXXXXXXOXXXXXXXXXXXXXXXXXXXXXXXXO');";
214 String buf
= "UPDATE big1 SET two=1 WHERE unique2=?;";
217 String buf
= "UPDATE big1 SET two=1 WHERE unique1=?;";
224 String buf
= "DELETE FROM big1 WHERE unique2=?;";
227 String buf
= "DELETE FROM big1 WHERE unique1=?;";
232 stmt
= con
.prepareStatement(stmtStr
);
233 int count
= 0, recordCount
=0;
235 long start
= 0,end
= 0,curr
= 0;
237 for(int i
=0; i
<100; i
++){
238 stmt
.setInt(1, 10000+i
);
239 if (val
==1) stmt
.setInt(2, 10000+i
);
240 start
= System
.nanoTime();
241 ret
= stmt
.executeUpdate();
245 end
= System
.nanoTime();
251 return (int)tot
/100/1000;
255 public static int joining(Connection con
,int val
, boolean flag
)throws Exception
257 PreparedStatement stmt
= null;
258 String stmtStr
= null;
262 String buf
= "SELECT big1.unique1, big1.unique2, big2.unique2, big2.stringu1 FROM big2,big1 WHERE big1.unique2=big2.unique2 AND big2.unique2=1000;";
265 String buf
= "SELECT big1.unique1, big1.unique2, big2.unique1, big2.stringu1 FROM big2,big1 WHERE big1.unique1=big2.unique1 AND big2.unique1=1000;";
272 String buf
= "SELECT big1.unique1, big1.unique2, big2.unique2, big2.stringu1 FROM big2,big1 WHERE big1.unique2=big2.unique2 AND big2.unique2<100;";
275 String buf
= "SELECT big1.unique1, big1.unique2, big2.unique1, big2.stringu1 FROM big2,big1 WHERE big1.unique1=big2.unique1 AND big2.unique1<100;";
282 String buf
= "SELECT small.unique2, big1.unique2, big2.unique2, small.stringu1, big1.unique1, big2.unique1 FROM small,big1, big2 WHERE small.unique2=big1.unique2 AND big1.unique2=big2.unique2 AND big1.unique2 = 100;";
285 String buf
= "SELECT small.unique1, big1.unique1, big2.unique1, small.stringu1, big1.unique2, big2.unique2 FROM small,big1, big2 WHERE small.unique1=big1.unique1 AND big1.unique1=big2.unique1 AND big1.unique1 = 100;";
290 stmt
= con
.prepareStatement(stmtStr
);
291 int count
=0,recordCount
=0,ret
=0;
292 long start
=0,end
=0,curr
=0;
295 //for(int i=0;i<=100;i++)
297 start
= System
.nanoTime();
298 rs
= stmt
.executeQuery();
301 //System.out.println("Record:"+ rs.getInt(1)+ " "+ rs.getInt(2)+" " +rs.getInt(3)+ " "+ rs.getString(4));
306 end
=System
.nanoTime();
307 if(val
==1 && recordCount
!=1)
309 System
.out
.println("No. of records Join: "+recordCount
);
312 if(val
==2 && recordCount
!=100)
314 System
.out
.println("Stmt: "+stmtStr
);
315 System
.out
.println("No. of records Join: "+recordCount
);
323 return (int)tot
/1000;
327 /*public static int projection(Connection con, boolean flag)throws Exception
329 PreparedStatement stmt = null;
334 String buf = "SELECT DISTINCT two,four,ten,twenty,onepercent,string4 from big1;";
339 String buf = "SELECT DISTINCT two,four,ten,twenty,onepercent,tenpercent,twentypercent,fiftypercent,unique3,evenonepercent,oddonepercent,stringu1,stringu2,string4 FROM big1;";
342 stmt = con.prepareStatement(stmtStr);
343 int count = 0, recordCount;
345 long start = 0, end = 0, curr = 0;
348 for(int i=0 ; i<=100 ; i++)
350 start = System.nanoTime();
351 rs = stmt.executeQuery();
360 end = System.nanoTime();
362 if((flag && recordCount !=1000) || (!flag && recordCount !=10000))
364 System.out.println("No. of records returned is around for Q2" + recordCount);
375 return (int)tot/100/1000;
379 public static int tenPerSel(Connection con
, boolean flag
)throws Exception
381 PreparedStatement stmt
= null;
382 int searchVal
= getRandom(9999 - 1000);
383 int searchValEnd
= searchVal
+ 999;
388 String buf
= "SELECT * from big1 where unique2 between "+ searchVal
+ " and "+ searchValEnd
+ ";";
393 String buf
= "SELECT * from big1 where unique1 between "+searchVal
+" and "+ searchValEnd
+ ";";
396 stmt
= con
.prepareStatement(stmtStr
);
397 int count
= 0, recordCount
;
399 long start
= 0, end
= 0, curr
= 0;
402 for(int i
=0 ; i
<=100 ; i
++)
404 start
= System
.nanoTime();
405 rs
= stmt
.executeQuery();
414 end
= System
.nanoTime();
417 if(recordCount
!= 1000)
419 System
.out
.println("No. of records for Q3: " + recordCount
);
429 return (int)tot
/100/1000;
433 //*****************************************************
434 public static void main(String
[] args
)
438 if (args
.length
== 1)
440 if (args
[0].equals("csql")) flag
=1;
441 else if (args
[0].equals("mysql")) flag
=2;
443 System
.out
.println("Valid values: csql or mysql");
449 Connection con
= null;
451 Class
.forName("csql.jdbc.JdbcSqlDriver");
452 con
= DriverManager
.getConnection("jdbc:csql", "root", "manager");
453 }else if (flag
== 2) {
454 Class
.forName("com.mysql.jdbc.Driver");
455 con
= DriverManager
.getConnection("jdbc:mysql://localhost/test", "root", "");
457 con
.setAutoCommit(false);
458 con
.setTransactionIsolation(Connection
.TRANSACTION_READ_COMMITTED
);
459 Statement cStmt
= con
.createStatement();
461 int timeTaken
[][] = new int[15][3];
462 int ins
=1,upd
=2,del
=3;// function parameter
463 int min
=1, sum
=2, ming
=3, sumg
=4;// function parameter
466 timeTaken
[0][0] = singleTuple(con
,true);
467 timeTaken
[1][0] = onePerSel(con
, true);
468 timeTaken
[2][0] = tenPerSel(con
,true);
469 timeTaken
[3][0] = aggregate(con
,min
, true);
470 timeTaken
[4][0] = aggregate(con
,sum
, true);
471 timeTaken
[5][0] = aggregate(con
,ming
, true);
472 timeTaken
[6][0] = aggregate(con
,sumg
, true);
474 timeTaken
[7][0] = dmlstatement(con
,ins
, true);
475 //timeTaken[8][0] = dmlstatement(con,upd, true);
476 timeTaken
[9][0] = dmlstatement(con
,del
, true);
478 timeTaken
[10][0] = joining(con
,1, true);
480 timeTaken
[11][0] = joining(con
,2, true);
481 timeTaken
[12][0] = joining(con
,3, true);
482 System
.out
.println("Finished no index queries");
487 cStmt
.execute("CREATE INDEX idx1 ON big1(unique2) HASH");
489 cStmt
.execute("CREATE INDEX idx2 ON big1(unique1) TREE");
491 cStmt
.execute("CREATE INDEX idx3 ON big2(unique2) HASH");
493 cStmt
.execute("CREATE INDEX idx4 ON big2(unique1) TREE");
495 cStmt
.execute("CREATE INDEX idx5 ON small(unique2) HASH");
497 cStmt
.execute("CREATE INDEX idx6 ON small(unique1) TREE");
500 }else if (flag
==2) {
501 cStmt
.execute("CREATE INDEX mysqlc1 USING HASH ON big1(unique2)");
502 cStmt
.execute("CREATE INDEX mysqlc2 USING BTREE ON big1(unique1)");
503 cStmt
.execute("CREATE INDEX mysqlc3 USING HASH ON big2(unique2)");
504 cStmt
.execute("CREATE INDEX mysqlc4 USING BTREE ON big2(unique1)");
505 cStmt
.execute("CREATE INDEX mysqlc5 USING HASH ON small(unique2)");
506 cStmt
.execute("CREATE INDEX mysqlc6 USING BTREE ON small(unique1)");
510 }catch(Exception e
){e
.printStackTrace(); System
.out
.println("Error creating index");}
512 timeTaken
[0][1] = singleTuple(con
,true);
513 timeTaken
[1][1] = onePerSel(con
, true);
514 timeTaken
[2][1] = tenPerSel(con
,true);
515 timeTaken
[3][1] = aggregate(con
,min
, true);
516 timeTaken
[4][1] = aggregate(con
,sum
, true);
517 timeTaken
[5][1] = aggregate(con
,ming
, true);
518 timeTaken
[6][1] = aggregate(con
,sumg
, true);
519 timeTaken
[7][1] = dmlstatement(con
,ins
, true);
520 //timeTaken[8][1] = dmlstatement(con,upd, true);
521 timeTaken
[9][1] = dmlstatement(con
,del
, true);
522 timeTaken
[10][1] = joining(con
,1, true);
523 timeTaken
[11][1] = joining(con
,2, true);
524 timeTaken
[12][1] = joining(con
,3, true);
525 System
.out
.println("Finished hash index queries");
527 timeTaken
[0][2] = singleTuple(con
,false);
528 timeTaken
[1][2] = onePerSel(con
, false);
529 timeTaken
[2][2] = tenPerSel(con
,false);
530 timeTaken
[3][2] = aggregate(con
,min
, false);
531 timeTaken
[4][2] = aggregate(con
,sum
, false);
532 timeTaken
[5][2] = aggregate(con
,ming
, false);
533 timeTaken
[6][2] = aggregate(con
,sumg
, false);
534 timeTaken
[7][2] = dmlstatement(con
,ins
, false);
535 //timeTaken[8][2] = dmlstatement(con,upd, false);
536 timeTaken
[9][2] = dmlstatement(con
,del
, false);
537 timeTaken
[10][2] = joining(con
,1, false);
538 timeTaken
[11][2] = joining(con
,2, false);
539 timeTaken
[12][2] = joining(con
,3, false);
540 System
.out
.println("Finished tree index queries");
546 cStmt
.execute("DROP INDEX idx1;");
548 cStmt
.execute("DROP INDEX idx2;");
550 cStmt
.execute("DROP INDEX idx3;");
552 cStmt
.execute("DROP INDEX idx4;");
554 cStmt
.execute("DROP INDEX idx5;");
556 cStmt
.execute("DROP INDEX idx6;");
559 } else if (flag
==2) {
560 cStmt
.execute("DROP INDEX mysqlc1 on big1");
562 cStmt
.execute("DROP INDEX mysqlc2 on big1");
564 cStmt
.execute("DROP INDEX mysqlc3 on big2");
566 cStmt
.execute("DROP INDEX mysqlc4 on big2");
568 cStmt
.execute("DROP INDEX mysqlc5 on big2");
570 cStmt
.execute("DROP INDEX mysqlc6 on big2");
574 }catch(Exception e
){System
.out
.println("Error dropping indexes");}
576 System
.out
.println("Wisconsin Benchmark Report:");
578 System
.out
.println("**********************************************************");
579 System
.out
.println(" Statement \t NoIndex\tHash \tTree");
580 System
.out
.println("**********************************************************");
581 System
.out
.println(" 1 Tuple \t "+timeTaken
[0][0] +" \t \t"+ timeTaken
[0][1]+ " \t"+ timeTaken
[0][2]);
582 System
.out
.println(" 1% Sel \t "+timeTaken
[1][0] +" \t \t"+ timeTaken
[1][1]+ " \t"+ timeTaken
[1][2]);
583 System
.out
.println(" 10% Sel \t "+timeTaken
[2][0] +" \t \t"+ timeTaken
[2][1]+ " \t"+ timeTaken
[2][2]);
585 System
.out
.println(" Min All \t "+timeTaken
[3][0] +" \t \t"+ timeTaken
[3][1]+ " \t"+ timeTaken
[3][2]);
586 System
.out
.println(" Sum All \t "+timeTaken
[4][0] +" \t \t"+ timeTaken
[4][1]+ " \t"+ timeTaken
[4][2]);
587 System
.out
.println(" Min Grp(100) \t "+timeTaken
[5][0] +" \t \t"+ timeTaken
[5][1]+ "\t"+ timeTaken
[5][2]);
588 System
.out
.println(" Sum Grp(100) \t "+timeTaken
[6][0] +" \t \t"+ timeTaken
[6][1]+ "\t"+ timeTaken
[6][2]);
590 System
.out
.println(" Insert 1 \t "+timeTaken
[7][0] +" \t \t"+ timeTaken
[7][1]+ " \t"+ timeTaken
[7][2]);
591 System
.out
.println(" Update 1 \t "+timeTaken
[8][0] +" \t \t"+ timeTaken
[8][1]+ " \t"+ timeTaken
[8][2]);
592 System
.out
.println(" Delete 1 \t "+timeTaken
[9][0] +" \t \t"+ timeTaken
[9][1]+ " \t"+ timeTaken
[9][2]);
593 System
.out
.println(" Join(10K*10K)1 \t "+timeTaken
[10][0] +" \t \t"+ timeTaken
[10][1]+ "\t"+ timeTaken
[10][2]);
594 System
.out
.println(" Join(10K*10K)100 \t "+timeTaken
[11][0] +" \t"+ timeTaken
[11][1]+ "\t"+ timeTaken
[11][2]);
595 System
.out
.println(" Join(10K*10K*1K)1\t "+timeTaken
[12][0] +" \t"+ timeTaken
[12][1]+ "\t"+ timeTaken
[12][2]);
596 System
.out
.println("**********************************************************");
601 System
.out
.println("Exception in Test: "+e
);