List:NDB Connectors« Previous MessageNext Message »
From:Monty Taylor Date:May 5 2007 8:15am
Subject:Rev 104: Added Java test code for director-based callbacks. in http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/exceptions
View as plain text  
------------------------------------------------------------
revno: 104
revision-id: mtaylor@stripped
parent: mtaylor@stripped
committer: Monty Taylor <mtaylor@stripped>
branch nick: exceptions
timestamp: Sat 2007-05-05 01:15:29 -0700
message:
  Added Java test code for director-based callbacks.
added:
  java/TestBaseCallback.java     testbasecallback.jav-20070505081526-30y7xsbok35xemlg-1
  java/testdirasync.java         testdirasync.java-20070505081526-30y7xsbok35xemlg-2
modified:
  java/testasync.java            test2.java-20070305174931-p3qddjy9i6h6y1f3-4
=== added file 'java/TestBaseCallback.java'
--- a/java/TestBaseCallback.java	1970-01-01 00:00:00 +0000
+++ b/java/TestBaseCallback.java	2007-05-05 08:15:29 +0000
@@ -0,0 +1,17 @@
+import com.mysql.cluster.ndbapi.*;
+
+class TestBaseCallback extends BaseCallback { 
+
+  NdbRecAttr myRecAttr; 
+  Ndb myNdb; 
+
+  public TestBaseCallback(Ndb theNdb, NdbRecAttr theRecAttr) { 
+    this.myRecAttr=theRecAttr;
+    this.myNdb=theNdb; 
+  }
+
+  public void callback(int result, NdbTransaction myTrans) { 
+    System.out.println("result " + result);
+    System.out.println("value " + this.myRecAttr.int32_value());
+  };
+}

=== added file 'java/testdirasync.java'
--- a/java/testdirasync.java	1970-01-01 00:00:00 +0000
+++ b/java/testdirasync.java	2007-05-05 08:15:29 +0000
@@ -0,0 +1,294 @@
+import java.sql.*;
+
+import java.lang.*; 
+import java.util.Date;
+import java.util.ArrayList;
+import com.mysql.cluster.ndbapi.*;
+
+
+public class testdirasync { 
+
+  private static Date beginTime;
+  private static Date initEndTime;
+  private static Date endTime;
+  private static long minTime = 10;
+  private static long maxTime = 0;
+
+  private static int num_iter = 0; 
+  private static int INSERT_NUM = 0; 
+
+  private Connection conn; 
+
+  static { 
+    System.loadLibrary("ndbj");
+  }
+
+
+  public static void main(String argv[]) throws SQLException{ 
+
+    if (argv.length<2) { 
+      System.out.println("Usage:\n\tjava test NUM_OF_ITERATIONS NUM_OF_ROWS "); 
+      System.exit(1);
+    }
+
+    num_iter = Integer.parseInt(argv[0]);
+    INSERT_NUM = Integer.parseInt(argv[1]);
+    int BATCH_SIZE=100;
+
+    //ndbapi.ndb_init();
+
+    /**************************************************************
+     * Connect to mysql server and create table                   *
+     **************************************************************/
+    
+    String table_name = "mytablename";
+    try { 
+      Class.forName("com.mysql.jdbc.Driver");
+    } catch (ClassNotFoundException e) { 
+      System.out.println("MySQL JDBC Driver not found"); 
+    }
+
+    Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
+
+    System.out.println("Dropping and recreating schema");
+
+    Statement s = conn.createStatement(); 
+
+    
+    try { 
+      s.executeUpdate("DROP TABLE if exists " + table_name);
+    } catch (SQLException e) { 
+    }
+    try { 
+      s.executeUpdate("CREATE TABLE if not exists " +
+		      table_name + 
+		      "    (ATTR1 INT UNSIGNED," +
+		      "     ATTR2 INT UNSIGNED NOT NULL," +
+		      "     PRIMARY KEY (ATTR1) )" + 
+		      "  ENGINE=NDBCLUSTER");
+    } catch (SQLException e) { 
+    }
+    
+
+    /**************************************************************
+     * Connect to ndb cluster                                     *
+     **************************************************************/
+
+    System.out.println("connecting to cluster");
+
+  try { 
+    NdbClusterConnection connection = NdbFactory.createNdbClusterConnection();
+
+    try {
+    if (connection.connect(5,3,1)==-1) { 
+      System.out.println("Connect to cluster management server failed.");
+      System.exit(-1);
+    }
+    } catch (Exception e) { 
+      System.out.println(e.getMessage());
+      System.exit(-1);
+    }
+      
+
+    
+    try { 
+       connection.wait_until_ready(30,30); 
+    } catch (Exception e) { 
+        System.out.println("Cluster was not ready within 30 secs.");
+    	System.exit(-1);
+    }
+
+    Ndb myNdb = new Ndb(connection,"test" ) ;
+
+    if (myNdb.init(4)==-1) { 
+      System.out.println(myNdb.getNdbError().getMessage());
+       System.exit(-1);
+    }
+
+    System.out.println("running tests:");
+
+    /**************************************************************************
+     * Fill table with tuples, using auto_increment IDs                       *
+     **************************************************************************/
+
+    beginTime = new Date(System.currentTimeMillis());
+
+
+    for(int t=0;t<Math.ceil(INSERT_NUM/BATCH_SIZE);t++) { 
+
+      NdbTransaction myTransaction = myNdb.startTransaction();
+
+      int val = ((t+1)*BATCH_SIZE)-INSERT_NUM;
+      int offset = 0;
+      if ( val > 0 ) {
+	offset = val; 
+      }
+
+      for(int i=0;i<BATCH_SIZE-offset;i++) { 
+
+	NdbOperation myOperation = myTransaction.getNdbOperation(table_name);
+	if (myOperation == null) { 
+	  System.out.println(myTransaction.getNdbError().getMessage());
+	  System.exit(1);
+	}
+
+	myOperation.insertTuple();
+	java.math.BigInteger auto_id = myNdb.getAutoIncrementValue(table_name,BATCH_SIZE);
+   
+	myOperation.equal("ATTR1",auto_id);
+	myOperation.setValue("ATTR2", t*BATCH_SIZE+i);
+
+      }      
+      
+      if (myTransaction.execute( ExecType.Commit )==-1) { 
+	System.out.println(myTransaction.getNdbError().getMessage());
+      }
+        
+
+      myTransaction.close();
+
+
+    }
+    endTime = new Date(System.currentTimeMillis());
+    System.out.println("Insert time for " + INSERT_NUM + ":");
+    System.out.println("   " + (endTime.getTime() - beginTime.getTime()) + "ms"); 
+
+
+    /*********************************
+     * Get list of ids
+     *********************************/
+
+    System.out.println();
+    System.out.println("Getting list of ids");
+
+    NdbTransaction myTransaction = myNdb.startTransaction();
+
+    NdbScanOperation myScanOperation=myTransaction.getNdbScanOperation(table_name);
+
+    if (myScanOperation == null) { 
+      System.out.println(myTransaction.getNdbError().getMessage());
+    }
+    
+    
+    if (myScanOperation.readTuples(NdbOperation.LockMode.LM_CommittedRead)==-1) {
+      System.out.println(myScanOperation.getNdbError().getMessage());
+    }
+    
+    
+    NdbRecAttr myRecAttr=myScanOperation.getValue("ATTR1");
+    
+    
+    myTransaction.execute(ExecType.NoCommit);
+
+    ArrayList<Integer> ids = new ArrayList<Integer>();
+
+    while (true) { 
+      
+      if (myScanOperation.nextResult(true) != 0) { 
+	break;
+      }
+      
+      int random_id = myRecAttr.int32_value();
+      ids.add(random_id);
+    }
+      
+    myTransaction.close();
+
+    /*******************
+     * Test NDB API Speed
+     *******************/
+    
+    System.out.println("Testing NDBAPI speed");
+
+    
+    long foo = 0;
+
+    beginTime = new Date(System.currentTimeMillis());
+    for(int x=0;x<num_iter;x++){ 
+      
+      int rand_id = (int) (Math.random() * ids.size() ) ; 
+      int id_num = (int)ids.get(rand_id);
+
+      NdbTransaction myTrans = myNdb.startTransaction(); //(table_name,id_num);
+      
+      if (myTrans==null) {
+	System.out.println(myNdb.getNdbError().getMessage());
+	System.exit(-1);
+      }
+      
+      NdbOperation myOper = myTrans.getNdbOperation(table_name);
+      myOper.readTuple(NdbOperation.LockMode.LM_Read);
+      
+      myOper.equal("ATTR1",id_num);
+      
+      myRecAttr= myOper.getValue("ATTR2"); 
+      if (myRecAttr==null) { 
+	System.out.println(myOper.getNdbError().getMessage());
+	System.exit(-1);
+      }
+
+
+      if (myTrans.execute(ExecType.Commit) == -1) { 
+	System.out.println(myTrans.getNdbError().getMessage());
+      System.exit(-1);
+      }
+
+      foo=myRecAttr.int32_value();
+      myTrans.close();
+    }
+    endTime = new Date(System.currentTimeMillis());
+    System.out.println("NDBAPI Execution time for " + num_iter + ": ");
+    System.out.println("   "+(endTime.getTime() - beginTime.getTime())+"ms");
+    s.close();
+    
+
+    System.out.println("Testing NDBAPI async speed");
+
+    
+    ArrayList<TestBaseCallback> cbs = new ArrayList<TestBaseCallback>();
+
+    beginTime = new Date(System.currentTimeMillis());
+    for(int x=0;x<num_iter;x++){ 
+      
+      int rand_id = (int) (Math.random() * ids.size() ) ; 
+      int id_num = (int)ids.get(rand_id);
+
+      NdbTransaction myTrans = myNdb.startTransaction(); //table_name,id_num);
+      
+      if (myTrans==null) {
+	System.out.println(myNdb.getNdbError().getMessage());
+	System.exit(-1);
+      }
+      
+      NdbOperation myOper = myTrans.getNdbOperation(table_name);
+      myOper.readTuple(NdbOperation.LockMode.LM_Read);
+      
+      myOper.equal("ATTR1",id_num);
+
+      NdbRecAttr theRecAttr= myOper.getValue("ATTR2"); 
+      if (myRecAttr==null) { 
+	System.out.println(myOper.getNdbError().getMessage());
+	System.exit(-1);
+      }
+
+      TestBaseCallback cb = new TestBaseCallback(myNdb,theRecAttr);
+      
+      myTrans.executeAsynchPrepare(ExecType.Commit, cb );
+
+//      foo=myRecAttr.int32_value();
+//      myNdb.closeTransaction(myTrans);
+    }
+
+    myNdb.sendPollNdb(5000);
+    endTime = new Date(System.currentTimeMillis());
+    System.out.println("NDBAPI Execution time for " + num_iter + ": ");
+    System.out.println("   "+(endTime.getTime() - beginTime.getTime())+"ms");
+    s.close();
+    
+  } catch ( NdbApiException e ) { 
+    e.printStackTrace();
+    System.exit(-1);
+  }
+
+  }
+}

=== modified file 'java/testasync.java'
--- a/java/testasync.java	2007-05-02 02:56:57 +0000
+++ b/java/testasync.java	2007-05-05 08:15:29 +0000
@@ -76,6 +76,7 @@
 
     System.out.println("connecting to cluster");
 
+  try { 
     NdbClusterConnection connection = NdbFactory.createNdbClusterConnection();
 
     try {
@@ -284,6 +285,10 @@
     System.out.println("   "+(endTime.getTime() - beginTime.getTime())+"ms");
     s.close();
     
+  } catch ( NdbApiException e ) { 
+    e.printStackTrace();
+    System.exit(-1);
+  }
 
   }
 }

Thread
Rev 104: Added Java test code for director-based callbacks. in http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/exceptionsMonty Taylor5 May