📄 benchtest.java
字号:
/* =============================================================
* SmallSQL : a free Java DBMS library for the Java(tm) platform
* =============================================================
*
* (C) Copyright 2004-2006, by Volker Berlin.
*
* Project Info: http://www.smallsql.de/
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* BenchTest.java
* ---------------
* Author: Volker Berlin
*
*/
package smallsql.junit;
import java.sql.*;
public class BenchTest
{
static byte[] byteArray = {23, 34, 67 };
static byte[] largeByteArray = new byte[4000];
static String driverClassName = "smallsql.database.SSDriver";
static String userName = "sa";
static String password = "";
static String jdbcUrl = "jdbc:smallsql:AllTests";
static int rowCount = 10000;
static Connection con;
static final String tableName = "BenchTest2";
public static void main(String[] args) throws SQLException{
for(int i=0; i<args.length;){
String option = args[i++];
if (option.equals("-driver") ) driverClassName = args[i++];
else if (option.equals("-user") ) userName = args[i++];
else if (option.equals("-password")) password = args[i++];
else if (option.equals("-url") ) jdbcUrl = args[i++];
else if (option.equals("-rowcount")) rowCount = Integer.parseInt(args[i++]);
else if (option.equals("-?") | option.equals("-help")){
System.out.println( "Valid options are :\n\t-driver\n\t-url\n\t-user\n\t-password\n\t-rowcount");
System.exit(0);
}
else {System.out.println("Option " + option + " is ignored");i++;}
}
System.out.println( "Driver: \t" + driverClassName);
System.out.println( "Username:\t" + userName);
System.out.println( "Password:\t" + password);
System.out.println( "JDBC URL:\t" + jdbcUrl);
System.out.println( "Row Count:\t" + rowCount);
System.out.println();
try{
Class.forName(driverClassName).newInstance();
con = DriverManager.getConnection( jdbcUrl, userName,password);
System.out.println( con.getMetaData().getDriverName() + " " + con.getMetaData().getDriverVersion());
System.out.println();
createTestTable( con );
test_InsertClassic( con );
test_DeleteAll( con );
test_InsertEmptyRows( con );
test_DeleteRows( con );
test_InsertRows( con );
test_RowRequestPages( con );
test_UpdateRows( con );
test_UpdateRowsPrepare( con );
test_UpdateRowsPrepareSP( con );
test_UpdateRowsPrepareBatch( con );
test_Scroll_getXXX( con );
test_UpdateLargeBinary( con );
test_UpdateLargeBinaryWithSP( con );
}catch(Exception e){
e.printStackTrace();
}finally{
if (con != null){
//dropTestTable( con );
con.close();
}
}
}
/**
* 1. Test
* Insert rows with default values with a classic insert statement.
*/
static void test_InsertClassic(Connection con){
System.out.println();
System.out.println( "Test insert rows with default values with a classic insert statement: " + rowCount + " rows");
try{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
long time = -System.currentTimeMillis();
for (int i=0; i<rowCount; i++){
st.execute("INSERT INTO " + tableName + "(i) VALUES(" + i +")");
}
time += System.currentTimeMillis();
ResultSet rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
int count = rs.getInt(1);
if (count != rowCount)
System.out.println( " Failed: Only " + count + " rows were inserted.");
else System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 2. Test
* Delete all rows with a single statement.
*/
static void test_DeleteAll(Connection con){
System.out.println();
System.out.println( "Test delete all rows: " + rowCount + " rows");
try{
long time = -System.currentTimeMillis();
Statement st = con.createStatement();
st.execute("DELETE FROM " + tableName);
time += System.currentTimeMillis();
System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 3. Test
* Insert only empty rows with the default values of the row with the method insertRow().
*/
static void test_InsertEmptyRows(Connection con){
System.out.println();
System.out.println( "Test insert empty rows with insertRow(): " + rowCount + " rows");
try{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery("SELECT * FROM "+tableName);
long time = -System.currentTimeMillis();
for (int i=0; i<rowCount; i++){
rs.moveToInsertRow();
rs.insertRow();
}
time += System.currentTimeMillis();
rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
int count = rs.getInt(1);
if (count != rowCount)
System.out.println( " Failed: Only " + count + " rows were inserted.");
else System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 4. Test
* Delete rows with the method deleteRow().
*/
static void test_DeleteRows(Connection con){
System.out.println();
System.out.println( "Test delete rows with deleteRow(): " + rowCount + " rows");
try{
Statement st1 = con.createStatement();
ResultSet rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
int count = rs.getInt(1);
if (count != rowCount){
// There are not the correct count of rows.
if (count == 0){
createTestDataWithClassicInsert( con );
rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
count = rs.getInt(1);
}
if (count != rowCount){
System.out.println( " Failed: Only " + (rowCount-count) + " rows were deleted.");
return;
}
}
st1.close();
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
rs = st.executeQuery("SELECT * FROM "+tableName);
long time = -System.currentTimeMillis();
for (int i=0; i<rowCount; i++){
rs.next();
rs.deleteRow();
}
time += System.currentTimeMillis();
rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
count = rs.getInt(1);
if (count != 0)
System.out.println( " Failed: Only " + (rowCount-count) + " rows were deleted.");
else System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 5. Test
* Insert rows with the method insertRow().
*/
static void test_InsertRows(Connection con){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -