KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > junit > BenchTest


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * BenchTest.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.junit;
34
35 import java.sql.*;
36
37 public class BenchTest
38 {
39     static byte[] byteArray = {23, 34, 67 };
40     static byte[] largeByteArray = new byte[4000];
41     
42     static String JavaDoc driverClassName = "smallsql.database.SSDriver";
43     static String JavaDoc userName = "sa";
44     static String JavaDoc password = "";
45     static String JavaDoc jdbcUrl = "jdbc:smallsql:AllTests";
46     static int rowCount = 10000;
47     
48     static Connection con;
49     static final String JavaDoc tableName = "BenchTest2";
50         
51     
52     public static void main(String JavaDoc[] args) throws SQLException{
53         for(int i=0; i<args.length;){
54             String JavaDoc option = args[i++];
55             if (option.equals("-driver") ) driverClassName = args[i++];
56             else if (option.equals("-user") ) userName = args[i++];
57             else if (option.equals("-password")) password = args[i++];
58             else if (option.equals("-url") ) jdbcUrl = args[i++];
59             else if (option.equals("-rowcount")) rowCount = Integer.parseInt(args[i++]);
60             else if (option.equals("-?") | option.equals("-help")){
61                 System.out.println( "Valid options are :\n\t-driver\n\t-url\n\t-user\n\t-password\n\t-rowcount");
62                 System.exit(0);
63             }
64             else {System.out.println("Option " + option + " is ignored");i++;}
65         }
66         System.out.println( "Driver: \t" + driverClassName);
67         System.out.println( "Username:\t" + userName);
68         System.out.println( "Password:\t" + password);
69         System.out.println( "JDBC URL:\t" + jdbcUrl);
70         System.out.println( "Row Count:\t" + rowCount);
71         System.out.println();
72         try{
73             Class.forName(driverClassName).newInstance();
74             con = DriverManager.getConnection( jdbcUrl, userName,password);
75             System.out.println( con.getMetaData().getDriverName() + " " + con.getMetaData().getDriverVersion());
76             System.out.println();
77             createTestTable( con );
78             test_InsertClassic( con );
79             test_DeleteAll( con );
80             test_InsertEmptyRows( con );
81             test_DeleteRows( con );
82             test_InsertRows( con );
83             test_RowRequestPages( con );
84             test_UpdateRows( con );
85             test_UpdateRowsPrepare( con );
86             test_UpdateRowsPrepareSP( con );
87             test_UpdateRowsPrepareBatch( con );
88             test_Scroll_getXXX( con );
89             test_UpdateLargeBinary( con );
90             test_UpdateLargeBinaryWithSP( con );
91         }catch(Exception JavaDoc e){
92             e.printStackTrace();
93         }finally{
94             if (con != null){
95                 //dropTestTable( con );
96
con.close();
97             }
98         }
99     }
100     
101     
102     
103     /**
104       * 1. Test
105       * Insert rows with default values with a classic insert statement.
106       */

107     static void test_InsertClassic(Connection con){
108         System.out.println();
109         System.out.println( "Test insert rows with default values with a classic insert statement: " + rowCount + " rows");
110         
111         try{
112             Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
113             long time = -System.currentTimeMillis();
114             for (int i=0; i<rowCount; i++){
115                 st.execute("INSERT INTO " + tableName + "(i) VALUES(" + i +")");
116             }
117             time += System.currentTimeMillis();
118             ResultSet rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
119             rs.next();
120             int count = rs.getInt(1);
121             if (count != rowCount)
122                 System.out.println( " Failed: Only " + count + " rows were inserted.");
123             else System.out.println( " Test time: " + time + " ms");
124             st.close();
125         }catch(Exception JavaDoc e){
126             System.out.println(" Failed:"+e);
127         }finally{
128             System.out.println();
129             System.out.println("===================================================================");
130         }
131     }
132     
133     
134     
135     /**
136       * 2. Test
137       * Delete all rows with a single statement.
138       */

139     static void test_DeleteAll(Connection con){
140         System.out.println();
141         System.out.println( "Test delete all rows: " + rowCount + " rows");
142         
143         try{
144             long time = -System.currentTimeMillis();
145             Statement st = con.createStatement();
146             st.execute("DELETE FROM " + tableName);
147             time += System.currentTimeMillis();
148             System.out.println( " Test time: " + time + " ms");
149             st.close();
150         }catch(Exception JavaDoc e){
151             System.out.println(" Failed:"+e);
152         }finally{
153             System.out.println();
154             System.out.println("===================================================================");
155         }
156     }
157     
158     
159     
160     /**
161       * 3. Test
162       * Insert only empty rows with the default values of the row with the method insertRow().
163       */

164     static void test_InsertEmptyRows(Connection con){
165         System.out.println();
166         System.out.println( "Test insert empty rows with insertRow(): " + rowCount + " rows");
167         
168         try{
169             Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
170             ResultSet rs = st.executeQuery("SELECT * FROM "+tableName);
171             long time = -System.currentTimeMillis();
172             for (int i=0; i<rowCount; i++){
173                 rs.moveToInsertRow();
174                 rs.insertRow();
175             }
176             time += System.currentTimeMillis();
177             rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
178             rs.next();
179             int count = rs.getInt(1);
180             if (count != rowCount)
181                  System.out.println( " Failed: Only " + count + " rows were inserted.");
182             else System.out.println( " Test time: " + time + " ms");
183             st.close();
184         }catch(Exception JavaDoc e){
185             System.out.println(" Failed:"+e);
186         }finally{
187             System.out.println();
188             System.out.println("===================================================================");
189         }
190     }
191     
192     
193     
194     /**
195       * 4. Test
196       * Delete rows with the method deleteRow().
197       */

198     static void test_DeleteRows(Connection con){
199         System.out.println();
200         System.out.println( "Test delete rows with deleteRow(): " + rowCount + " rows");
201         
202         try{
203             Statement st1 = con.createStatement();
204             ResultSet rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
205             rs.next();
206             int count = rs.getInt(1);
207             if (count != rowCount){
208                 // There are not the correct count of rows.
209
if (count == 0){
210                     createTestDataWithClassicInsert( con );
211                     rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
212                     rs.next();
213                     count = rs.getInt(1);
214                 }
215                 if (count != rowCount){
216                     System.out.println( " Failed: Only " + (rowCount-count) + " rows were deleted.");
217                     return;
218                 }
219             }
220             st1.close();
221             
222             Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
223             rs = st.executeQuery("SELECT * FROM "+tableName);
224             long time = -System.currentTimeMillis();
225             for (int i=0; i<rowCount; i++){
226                 rs.next();
227                 rs.deleteRow();
228             }
229             time += System.currentTimeMillis();
230             rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
231             rs.next();
232             count = rs.getInt(1);
233             if (count != 0)
234                  System.out.println( " Failed: Only " + (rowCount-count) + " rows were deleted.");
235             else System.out.println( " Test time: " + time + " ms");
236             st.close();
237         }catch(Exception JavaDoc e){
238             System.out.println(" Failed:"+e);
239         }finally{
240             System.out.println();
241             System.out.println("===================================================================");
242         }
243     }
244     
245     
246     
247     /**
248       * 5. Test
249       * Insert rows with the method insertRow().
250       */

251     static void test_InsertRows(Connection con){
252         System.out.println();
253         System.out.println( "Test insert rows with insertRow(): " + rowCount + " rows");
254         
255         try{
256             Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
257             ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
258             long time = -System.currentTimeMillis();
259             for (int i=0; i<rowCount; i++){
260                 rs.moveToInsertRow();
261                 rs.updateBytes ( "bi", byteArray );
262                 rs.updateString( "c" , "Test" );
263                 rs.updateDate ( "d" , new Date( System.currentTimeMillis() ) );
264                 rs.updateFloat ( "de", (float)1234.56789 );
265                 rs.updateFloat ( "f" , (float)9876.54321 );
266                 rs.updateBytes ( "im", largeByteArray );
267                 rs.updateInt ( "i" , i );
268                 rs.updateDouble( "m" , 23.45 );
269                 rs.updateDouble( "n" , 567.45 );
270                 rs.updateFloat ( "r" , (float)78.89 );
271                 rs.updateTime ( "sd", new Time( System.currentTimeMillis() ) );
272                 rs.updateShort ( "si", (short)i );
273                 rs.updateFloat ( "sm", (float)34.56 );
274                 rs.updateString( "sy", "sysname (30) NULL" );
275                 rs.updateString( "t" , "ntext NULL, sample to save in the field" );
276                 rs.updateByte ( "ti", (byte)i );
277                 rs.updateBytes ( "vb", byteArray );
278                 rs.updateString( "vc", "nvarchar (255) NULL" );
279                 rs.insertRow();
280             }
281             time += System.currentTimeMillis();
282             rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
283             rs.next();
284             int count = rs.getInt(1);
285             if (count != rowCount){
286                   st.execute("DELETE FROM " + tableName);
287                   System.out.println( " Failed: Only " + count + " rows were inserted.");
288             }else System.out.println( " Test time: " + time + " ms");
289             st.close();
290         }catch(Exception JavaDoc e){
291             e.printStackTrace();
292             try{
293                 // reset for the next test
294
Statement st = con.createStatement();
295                 st.execute("DELETE FROM " + tableName);
296                 st.close();
297             }catch(Exception JavaDoc ee){/* ignore it */}
298             System.out.println(" Failed:"+e);
299         }finally{
300             System.out.println();
301             System.out.println("===================================================================");
302         }
303     }
304     
305     
306     
307     /**
308       * 6. Test
309       * Request one page of rows from a large ResultSet.
310       */

311     static void test_RowRequestPages(Connection con){
312         int pages = 100;
313         int rows = rowCount / pages;
314         System.out.println();
315         System.out.println( "Test request row pages : " + pages + " pages, " +rows + " rows per page");
316         try{
317             Statement st1 = con.createStatement();
318             ResultSet rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
319             rs.next();
320             int count = rs.getInt(1);
321             if (count != rowCount){
322                 // There are not the correct count of rows.
323
if (count == 0){
324                     createTestDataWithClassicInsert( con );
325                     rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
326                     rs.next();
327                     count = rs.getInt(1);
328                 }
329                 if (count != rowCount){
330                     System.out.println( " Failed: Only " + (rowCount-count) + " rows were found.");
331                     return;
332                 }
333             }
334             st1.close();
335             
336             long time = -System.currentTimeMillis();
337             Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
338             st.setFetchSize( rows );
339             for (int i=0; i<pages; i++){
340                 rs = st.executeQuery("SELECT * FROM " + tableName);
341                 rs.absolute( i*rows+1 );
342                 for (int r=1; r<rows; r++){
343                     // only (rows-1) rows because absolute has already the first row
344
if (!rs.next()){
345                         System.out.println( " Failed: No rows were found at page " + i + " page and row " + r);
346                         return;
347                     }
348                     int col_i = rs.getInt("i");
349                     if (col_i != (i*rows+r)){
350                         System.out.println( " Failed: Wrong row " + col_i + ", it should be row " + (i*rows+r));
351                         return;
352                     }
353                 }
354             }
355             time += System.currentTimeMillis();
356             System.out.println( " Test time: " + time + " ms");
357             st.close();
358         }catch(Exception JavaDoc e){
359             System.out.println(" Failed:"+e);
360         }finally{
361             System.out.println();
362             System.out.println("===================================================================");
363         }
364     }
365
366     
367     
368     /**
369       * 7. Test
370       * Update rows with the method updateRow().
371       */

372     static void test_UpdateRows(Connection con){
373         System.out.println();
374         System.out.println( "Test update rows with updateRow(): " + rowCount + " rows");
375         
376         try{
377             Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
378             ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
379             int colCount = rs.getMetaData().getColumnCount();
380             long time = -System.currentTimeMillis();
381             int count = 0;
382             while(rs.next()){
383                 for (int i=2; i<=colCount; i++){
384                     rs.updateObject( i, rs.getObject(i) );
385                 }
386                 rs.updateRow();
387                 count++;
388             }
389             time += System.currentTimeMillis();
390             if (count != rowCount)
391                  System.out.println( " Failed: Only " + count + " rows were updated.");
392             else System.out.println( " Test time: " + time + " ms");
393             st.close();
394         }catch(Exception JavaDoc e){
395             System.out.println(" Failed:" + e);
396         }finally{
397             System.out.println();
398             System.out.println("===================================================================");
399         }
400     }
401     
402     
403     
404     /**
405       * 8. Test
406       * Update rows with a PreparedStatement.
407       */

408     static void test_UpdateRowsPrepare(Connection con){
409         System.out.println();
410         System.out.println( "Test update rows with a PreparedStatement: " + rowCount + " rows");
411         try{
412             PreparedStatement pr = con.prepareStatement( "UPDATE " + tableName + " SET bi=?,c=?,d=?,de=?,f=?,im=?,i=?,m=?,n=?,r=?,sd=?,si=?,sm=?,sy=?,t=?,ti=?,vb=?,vc=? WHERE i=?" );
413             long time = -System.currentTimeMillis();
414             for (int i=0; i<rowCount; i++){
415                 pr.setBytes ( 1, byteArray );
416                 pr.setString( 2 , "Test" );
417                 pr.setDate ( 3 , new Date( System.currentTimeMillis() ) );
418                 pr.setFloat ( 4, (float)1234.56789 );
419                 pr.setFloat ( 5 , (float)9876.54321 );
420                 pr.setBytes ( 6, largeByteArray );
421                 pr.setInt ( 7 , i );
422                 pr.setDouble( 8 , 23.45 );
423                 pr.setDouble( 9 , 567.45 );
424                 pr.setFloat ( 10 , (float)78.89 );
425                 pr.setTime ( 11, new Time( System.currentTimeMillis() ) );
426                 pr.setShort ( 12, (short)23456 );
427                 pr.setFloat ( 13, (float)34.56 );
428                 pr.setString( 14, "sysname (30) NULL" );
429                 pr.setString( 15 , "text NULL" );
430                 pr.setByte ( 16, (byte)28 );
431                 pr.setBytes ( 17, byteArray );
432                 pr.setString( 18, "varchar (255) NULL" );
433                 pr.setInt ( 19 , i );
434                 int updateCount = pr.executeUpdate();
435                 if (updateCount != 1){
436                     System.out.println( " Failed: Update count should be 1 but it is " + updateCount + ".");
437                     return;
438                 }
439             }
440             time += System.currentTimeMillis();
441             System.out.println( " Test time: " + time + " ms");
442             pr.close();
443         }catch(Exception JavaDoc e){
444             System.out.println(" Failed:"+e);
445         }finally{
446             System.out.println();
447             System.out.println("===================================================================");
448         }
449     }
450     
451     
452     
453     /**
454       * 9. Test
455       * Update rows with a PreparedStatement and a stored procedure.
456       */

457     static void test_UpdateRowsPrepareSP(Connection con){
458         System.out.println();
459         System.out.println( "Test update rows with a PreparedStatement and a stored procedure: " + rowCount + " rows");
460         
461         try{
462             Statement st = con.createStatement();
463             try{st.execute("drop procedure sp_"+tableName);}catch(Exception JavaDoc e){/* ignore it */}
464             st.execute("create procedure sp_"+tableName+" (@bi binary,@c nchar(255),@d datetime,@de decimal,@f float,@im image,@i int,@m money,@n numeric(18, 0),@r real,@sd smalldatetime,@si smallint,@sm smallmoney,@sy sysname,@t ntext,@ti tinyint,@vb varbinary(255),@vc nvarchar(255)) as UPDATE " + tableName + " SET bi=@bi,c=@c,d=@d,de=@de,f=@f,im=@im,i=@i,m=@m,n=@n,r=@r,sd=@sd,si=@si,sm=@sm,sy=@sy,t=@t,ti=@ti,vb=@vb,vc=@vc WHERE i=@i");
465
466             PreparedStatement pr = con.prepareStatement( "exec sp_" + tableName + " ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?" );
467             long time = -System.currentTimeMillis();
468             for (int i=0; i<rowCount; i++){
469                 pr.setBytes ( 1, byteArray );
470                 pr.setString( 2 , "Test" );
471                 pr.setDate ( 3 , new Date( System.currentTimeMillis() ) );
472                 pr.setFloat ( 4, (float)1234.56789 );
473                 pr.setFloat ( 5 , (float)9876.54321 );
474                 pr.setBytes ( 6, largeByteArray );
475                 pr.setInt ( 7 , i );
476                 pr.setDouble( 8 , 23.45 );
477                 pr.setDouble( 9 , 567.45 );
478                 pr.setFloat ( 10 , (float)78.89 );
479                 pr.setTime ( 11, new Time( System.currentTimeMillis() ) );
480                 pr.setShort ( 12, (short)23456 );
481                 pr.setFloat ( 13, (float)34.56 );
482                 pr.setString( 14, "sysname (30) NULL" );
483                 pr.setString( 15 , "text NULL" );
484                 pr.setByte ( 16, (byte)28 );
485                 pr.setBytes ( 17, byteArray );
486                 pr.setString( 18, "varchar (255) NULL" );
487                 int updateCount = pr.executeUpdate();
488                 if (updateCount != 1){
489                     System.out.println( " Failed: Update count should be 1 but it is " + updateCount + ".");
490                     return;
491                 }
492             }
493             time += System.currentTimeMillis();
494             System.out.println( " Test time: " + time + " ms");
495             st.execute("drop procedure sp_"+tableName);
496             st.close();
497             pr.close();
498         }catch(Exception JavaDoc e){
499             System.out.println(" Failed:"+e);
500         }finally{
501             System.out.println();
502             System.out.println("===================================================================");
503         }
504     }
505     
506
507     
508     /**
509       * 10. Test
510       * Update rows with a PreparedStatement and Batch.
511       */

512     static void test_UpdateRowsPrepareBatch(Connection con){
513         int batchSize = 10;
514         int batches = rowCount / batchSize;
515         System.out.println();
516         System.out.println( "Test update rows with PreparedStatement and Batches: " + batches + " batches, " + batchSize + " batch size");
517         
518         try{
519             PreparedStatement pr = con.prepareStatement( "UPDATE " + tableName + " SET bi=?,c=?,d=?,de=?,f=?,im=?,i=?,m=?,n=?,r=?,sd=?,si=?,sm=?,sy=?,t=?,ti=?,vb=?,vc=? WHERE i=?" );
520             long time = -System.currentTimeMillis();
521             for (int i=0; i<batches; i++){
522                 for (int r=0; r<batchSize; r++){
523                     pr.setBytes ( 1, byteArray );
524                     pr.setString( 2 , "Test" );
525                     pr.setDate ( 3 , new Date( System.currentTimeMillis() ) );
526                     pr.setFloat ( 4, (float)1234.56789 );
527                     pr.setFloat ( 5 , (float)9876.54321 );
528                     pr.setBytes ( 6, largeByteArray );
529                     pr.setInt ( 7 , i*batchSize + r );
530                     pr.setDouble( 8 , 23.45 );
531                     pr.setDouble( 9 , 567.45 );
532                     pr.setFloat ( 10 , (float)78.89 );
533                     pr.setTime ( 11, new Time( System.currentTimeMillis() ) );
534                     pr.setShort ( 12, (short)23456 );
535                     pr.setFloat ( 13, (float)34.56 );
536                     pr.setString( 14, "sysname (30) NULL" );
537                     pr.setString( 15 , "text NULL" );
538                     pr.setByte ( 16, (byte)28 );
539                     pr.setBytes ( 17, byteArray );
540                     pr.setString( 18, "varchar (255) NULL" );
541                     pr.setInt ( 19 , i );
542                     pr.addBatch();
543                 }
544                 int[] updateCount = pr.executeBatch();
545                 if (updateCount.length != batchSize){
546                     System.out.println( " Failed: Update count size should be " + batchSize + " but it is " + updateCount.length + ".");
547                     return;
548                 }
549             }
550             time += System.currentTimeMillis();
551             System.out.println( " Test time: " + time + " ms");
552             pr.close();
553         }catch(Exception JavaDoc e){
554             System.out.println(" Failed:"+e);
555         }finally{
556             System.out.println();
557             System.out.println("===================================================================");
558         }
559     }
560      
561     
562     
563     /**
564       * 11. Test
565       * Scroll and call the getXXX methods for every columns.
566       */

567     static void test_Scroll_getXXX(Connection con){
568         System.out.println();
569         System.out.println( "Test scroll and call the getXXX methods for every columns: " + rowCount + " rows");
570         
571         try{
572             Statement st = con.createStatement();
573             long time = -System.currentTimeMillis();
574             ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
575             for (int i=0; i<rowCount; i++){
576                     rs.next();
577                     rs.getInt ( 1 );
578                     rs.getBytes ( 2 );
579                     rs.getString( 3 );
580                     rs.getDate ( 4 );
581                     rs.getFloat ( 5 );
582                     rs.getFloat ( 6 );
583                     rs.getBytes ( 7 );
584                     rs.getInt ( 8 );
585                     rs.getDouble( 9 );
586                     rs.getDouble( 10 );
587                     rs.getFloat ( 11 );
588                     rs.getTime ( 12 );
589                     rs.getShort ( 13 );
590                     rs.getFloat ( 14 );
591                     rs.getString( 15 );
592                     rs.getString( 16 );
593                     rs.getByte ( 17 );
594                     rs.getBytes ( 18 );
595                     rs.getString( 19 );
596             }
597             time += System.currentTimeMillis();
598             System.out.println( " Test time: " + time + " ms");
599             st.close();
600         }catch(Exception JavaDoc e){
601             System.out.println(" Failed:"+e);
602         }finally{
603             System.out.println();
604             System.out.println("===================================================================");
605         }
606     }
607      
608     
609     /**
610       * 12. Test
611       * Update large binary data.
612       */

613     static void test_UpdateLargeBinary(Connection con){
614         System.out.println();
615         System.out.println( "Test update large binary data: " + rowCount + "KB bytes");
616         
617         try{
618             java.io.FileOutputStream JavaDoc fos = new java.io.FileOutputStream JavaDoc(tableName+".bin");
619             byte bytes[] = new byte[1024];
620             for(int i=0; i<rowCount; i++){
621                 fos.write(bytes);
622             }
623             fos.close();
624             java.io.FileInputStream JavaDoc fis = new java.io.FileInputStream JavaDoc(tableName+".bin");
625             long time = -System.currentTimeMillis();
626             PreparedStatement pr = con.prepareStatement("Update " + tableName + " set im=? WHERE pr=1");
627             pr.setBinaryStream( 1, fis, rowCount*1024 );
628             pr.execute();
629             pr.close();
630             time += System.currentTimeMillis();
631             System.out.println( " Test time: " + time + " ms");
632             fis.close();
633             java.io.File JavaDoc file = new java.io.File JavaDoc(tableName+".bin");
634             file.delete();
635         }catch(Exception JavaDoc e){
636             System.out.println(" Failed:"+e);
637         }finally{
638             System.out.println();
639             System.out.println("===================================================================");
640         }
641     }
642      
643     
644
645     
646     /**
647       * 12. Test
648       * Update large binary data with a SP.
649       */

650     static void test_UpdateLargeBinaryWithSP(Connection con){
651         System.out.println();
652         System.out.println( "Test update large binary data with a SP: " + rowCount + "KB bytes");
653         
654         try{
655             java.io.FileOutputStream JavaDoc fos = new java.io.FileOutputStream JavaDoc(tableName+".bin");
656             byte bytes[] = new byte[1024];
657             for(int i=0; i<rowCount; i++){
658                 fos.write(bytes);
659             }
660             fos.close();
661             java.io.FileInputStream JavaDoc fis = new java.io.FileInputStream JavaDoc(tableName+".bin");
662             long time = -System.currentTimeMillis();
663             Statement st = con.createStatement();
664             st.execute("CREATE PROCEDURE #UpdateLargeBinary(@im image) as Update " + tableName + " set im=@im WHERE pr=2");
665             PreparedStatement pr = con.prepareStatement("exec #UpdateLargeBinary ?");
666             pr.setBinaryStream( 1, fis, rowCount*1024 );
667             pr.execute();
668             st.execute("DROP PROCEDURE #UpdateLargeBinary");
669             st.close();
670             pr.close();
671             time += System.currentTimeMillis();
672             System.out.println( " Test time: " + time + " ms");
673             fis.close();
674             java.io.File JavaDoc file = new java.io.File JavaDoc(tableName+".bin");
675             file.delete();
676         }catch(Exception JavaDoc e){
677             System.out.println(" Failed:"+e);
678         }finally{
679             System.out.println();
680             System.out.println("===================================================================");
681         }
682     }
683      
684     
685
686     
687     /**
688       * Create a new Table for testing
689       */

690     static void createTestTable(Connection con) throws SQLException{
691             Statement st;
692             st = con.createStatement();
693             //delete old table
694
dropTestTable( con );
695
696             //create table
697
st.execute(
698                 "CREATE TABLE " + tableName + " ("+
699                 " pr numeric IDENTITY,"+
700                 " bi binary (255) NULL ,"+
701                 " c nchar (255) NULL ,"+
702                 " d datetime NULL ,"+
703                 " de decimal(18, 0) NULL ,"+
704                 " f float NULL ,"+
705                 " im image NULL ,"+
706                 " i int NULL ,"+
707                 " m money NULL ,"+
708                 " n numeric(18, 0) NULL ,"+
709                 " r real NULL ,"+
710                 " sd smalldatetime NULL ,"+
711                 " si smallint NULL ,"+
712                 " sm smallmoney NULL ,"+
713                 " sy sysname NULL ,"+
714                 " t ntext NULL ,"+
715                 " ti tinyint NULL ,"+
716                 " vb varbinary (255) NULL ,"+
717                 " vc nvarchar (255) NULL, "+
718                 "CONSTRAINT PK_BenchTest2 PRIMARY KEY CLUSTERED (pr) "+
719                 ")");
720             st.close();
721     }
722     
723
724     
725     static void deleteTestTable(Connection con){
726         try{
727             Statement st = con.createStatement();
728             st.execute("DELETE FROM " + tableName);
729             st.close();
730         }catch(Exception JavaDoc e){/* ignore it */}
731     }
732
733     static void dropTestTable(Connection con){
734         try{
735             Statement st = con.createStatement();
736             st.execute("drop table " + tableName);
737             st.close();
738         }catch(Exception JavaDoc e){/* ignore it */}
739     }
740     
741     // create test data after the insert test is failed
742
static void createTestDataWithClassicInsert(Connection con) throws SQLException{
743         String JavaDoc sql = "INSERT INTO " + tableName + "(bi,c,d,de,f,im,i,m,n,r,si,sd,sm,sy,t,ti,vb,vc) VALUES(0x172243,'Test','20010101',1234.56789,9876.54321,0x";
744         for(int i=0; i<largeByteArray.length; i++){
745             sql += "00";
746         }
747         Statement st = con.createStatement();
748         for (int i=0; i<rowCount; i++){
749             st.execute(sql + ","+i+",23.45,567.45,78.89,"+i+",'11:11:11',34.56,'sysname (30) NULL','ntext NULL, sample to save in the field',"+(i & 0xFF)+",0x172243,'nvarchar (255) NULL')" );
750         }
751         st.close();
752     }
753 }
Popular Tags