|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#41 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Quote:
__________________
In God we trust; all others bring data |
|
|
|
|
|
|
#42 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Fatto il test jdbc driver VS sqlite4java wrapper.
In sintesi, il risultato: Codice:
Test with jdbc driver... Total time: 21.997608 Test with wrapper... Total time: 7.7242694 Non mi sento di affermarlo con certezza a causa della mancanza di documentazione. Sono quindi andato per tentativi e non posso escludere che manchi qualcosa che una volta aggiunto possa peggiorare i tempi. Cmq i dati ci sono, nel database. Ho fatto il test in questo modo: - per quanto riguarda il jdbc driver, ho preso il codice di wingman e l'ho modificato leggermente (vale a dire, non mi interessava il tempo di creazione delle tabelle, ...). Inoltre, wingman aveva portato i cicli di set della prepared statement FUORI dal ciclo principale, visto che i dati che si scrivevano erano sempre gli stessi. Purtroppo questo non e' possibile con il wrapper; dunque, per uniformita' di vedute (e poi, anche se i dati sono sempre gli stessi, vogliamo far finta che non lo siano) ho riportato il set della prepared nel ciclo stesso. - per quanto riguarda il wrapper, ho fatto riferimento a quanto ho scritto precedentemente, cercando di portarlo nella maniera piu' simile possibile a quanto fatto da wingman. E' da notare che, nonostante la banalita' del programma, ho dovuto lottare per un bel po' contro eccezioni di vario tipo poiche' qualcosa mancava sempre o era fuori posto, ecc. Inoltre in un caso (una volta che credevo che ormai fosse tutto a posto) ho avuto un crash della jvm. Quindi, il driver jdbc e' piu' lento ma piu' sicuro. Una cosa su cui meditare... Qui l'applicazione che ho usato. Mi vergogno un po' vista la bassa qualita', trattasi di un'applicazione da pausa pranzo: Codice:
package sqliteperftest;
import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteStatement;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
/**
*
* @author MEGI
*/
public class SqlitePerfTest
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
System.out.println ("Test with jdbc driver...");
TestSqliteDriver.doit();
System.out.println ("Test with wrapper...");
TestSqliteWrapper.doit();
}
catch (Exception ex)
{
System.out.println ("" + ex);
}
}
}
class TestSqliteDriver
{
public static int NUM_TEXT = 100;
public static int NUM_INTEGER = 100;
public static int NUM_INSERT = 100000;
public static void doit() throws ClassNotFoundException
{
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sampledriver.db");
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists two_hundred_one");
String create = "create table two_hundred_one (id integer";
for(int i=1; i<=NUM_TEXT; i++)
{
create += ", text_" + i + " text";
}
for(int i=1; i<=NUM_INTEGER; i++)
{
create += ", int_" + i + " integer";
}
create += ")";
statement.executeUpdate(create);
String insert = "insert into two_hundred_one values(?";
for(int i=0; i<NUM_TEXT + NUM_INTEGER; i++)
{
insert += ", ?";
}
insert += ")";
long start = System.nanoTime();
PreparedStatement preparedStatement = connection.prepareStatement(insert);
for(int i=1; i<=NUM_INSERT; i++)
{
preparedStatement.setInt(1, i);
for(int j=1; j<=NUM_TEXT; j++)
{
preparedStatement.setString(j + 1, "Text_" + j);
}
for(int j=1; j<=NUM_INTEGER; j++)
{
preparedStatement.setInt(NUM_TEXT + j + 1, j);
}
preparedStatement.executeUpdate();
}
connection.commit();
long end = System.nanoTime();
System.out.println("Total time: " + (float)(end-start)/1000000000 );
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
e.printStackTrace(System.out);
}
finally
{
try
{
if(connection != null)
{
connection.close();
}
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
}
class TestSqliteWrapper
{
public static void doit()
{
String query;
SQLiteConnection db = null;
SQLiteStatement st = null;
try
{
/* Open connection */
db = new SQLiteConnection(new File("samplewrapper.db"));
db.open(true);
SQLiteStatement stmtDrop = null;
try
{
db.exec("BEGIN");
query = "drop table if exists two_hundred_one";
stmtDrop = db.prepare(query);
stmtDrop.step();
db.exec("COMMIT");
}
catch (Exception ee)
{
//System.out.println (ee.toString());
db.exec("ROLLBACK");
}
finally
{
if (stmtDrop != null)
{
stmtDrop.dispose();
}
}
db.exec("BEGIN");
String create = "create table two_hundred_one (recid integer";
for(int i=1; i<=TestSqliteDriver.NUM_TEXT; i++)
{
create += ", text_" + i + " text";
}
for(int i=1; i<=TestSqliteDriver.NUM_INTEGER; i++)
{
create += ", int_" + i + " integer";
}
create += ")";
db.exec(create);
db.exec("COMMIT");
db.exec("BEGIN");
query = "insert into two_hundred_one (recid";
for(int i=1; i<= TestSqliteDriver.NUM_TEXT; i++)
{
query += ", text_" + i;
}
for(int i=1; i<=TestSqliteDriver.NUM_INTEGER; i++)
{
query += ", int_" + i;
}
query += ") values(?";
for(int i=0; i < TestSqliteDriver.NUM_TEXT + TestSqliteDriver.NUM_INTEGER; i++)
{
query += ", ?";
}
query += ")";
long start = System.nanoTime();
st = db.prepare(query);
for(int i=1; i<=TestSqliteDriver.NUM_INSERT; i++)
{
for(int j=1; j<=TestSqliteDriver.NUM_TEXT; j++)
{
st.bind(j+1, "Text_" + j);
}
for(int j=1; j<=TestSqliteDriver.NUM_INTEGER; j++)
{
st.bind(TestSqliteDriver.NUM_TEXT + j + 1, j);
}
st.bind(1, i);
st.step();
st.reset();
}
db.exec("COMMIT");
long end = System.nanoTime();
System.out.println("Total time: " + (float)(end-start)/1000000000 );
}
catch (Exception ee)
{
System.out.println ("Exception inserting record: " + ee);
ee.printStackTrace(System.err); // test
System.exit(0); // test
}
finally
{
try
{
if (db != null)
{
db.dispose();
}
}
catch (Exception e) { }
try
{
if (st != null)
{
st.dispose();
}
}
catch (Exception e) {}
}
}
}
__________________
In God we trust; all others bring data |
|
|
|
|
|
#43 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
![]()
|
|
|
|
|
|
#44 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
http://code.google.com/p/sqlite4java/
sqlite4java is not a JDBC driver. Access to the database is made through the custom interfaces of com.almworks.sqlite4java package. Tighter integration with SQLite offers better performance and features not available through JDBC interfaces. ![]() http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers ![]()
Ultima modifica di Vincenzo1968 : 04-03-2013 alle 18:52. |
|
|
|
|
|
#45 | |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Quote:
|
|
|
|
|
|
|
#46 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Quote:
__________________
In God we trust; all others bring data |
|
|
|
|
|
|
#47 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Si beh, quando hai cinque minuti di tempo puoi aggiustarlo un attimino e postarlo su google code. Secondo me è molto meglio del "getting started" ufficiale.
Comunque, dico, posta non il confronto con jdbc, ma questo: http://www.hwupgrade.it/forum/showpo...3&postcount=35 |
|
|
|
|
|
#48 |
|
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
vincenzo@mar mar 05$ javac -cp '.:sqlite-jdbc-3.7.2.jar:sqlite4java.jar:h2-1.3.170.jar' SqlitePerfTest.java
vincenzo@mar mar 05$ java -cp '.:sqlite-jdbc-3.7.2.jar:sqlite4java.jar:h2-1.3.170.jar' SqlitePerfTest Test with jdbc driver... Total time: 21.36797 Test with wrapper... Total time: 7.377926 Test with h2 driver... Total time: 28.130777 ![]() H2 Codice:
//package sqliteperftest;
import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteStatement;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.*;
/**
*
* @author MEGI
*/
public class SqlitePerfTest
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
System.out.println ("Test with jdbc driver...");
TestSqliteDriver.doit();
System.out.println ("Test with wrapper...");
TestSqliteWrapper.doit();
System.out.println ("Test with h2 driver...");
TestH2Driver.doit();
}
catch (Exception ex)
{
System.out.println ("" + ex);
}
}
}
class TestH2Driver
{
public static int NUM_TEXT = 100;
public static int NUM_INTEGER = 100;
public static int NUM_INSERT = 100000;
public static void doit() throws ClassNotFoundException
{
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.h2.Driver");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:h2:sampleh2driver.db", "sa", "");
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists two_hundred_one");
String create = "create table two_hundred_one (id integer";
for(int i=1; i<=NUM_TEXT; i++)
{
create += ", text_" + i + " text";
}
for(int i=1; i<=NUM_INTEGER; i++)
{
create += ", int_" + i + " integer";
}
create += ")";
statement.executeUpdate(create);
String insert = "insert into two_hundred_one values(?";
for(int i=0; i<NUM_TEXT + NUM_INTEGER; i++)
{
insert += ", ?";
}
insert += ")";
long start = System.nanoTime();
PreparedStatement preparedStatement = connection.prepareStatement(insert);
for(int i=1; i<=NUM_INSERT; i++)
{
preparedStatement.setInt(1, i);
for(int j=1; j<=NUM_TEXT; j++)
{
preparedStatement.setString(j + 1, "Text_" + j);
}
for(int j=1; j<=NUM_INTEGER; j++)
{
preparedStatement.setInt(NUM_TEXT + j + 1, j);
}
preparedStatement.executeUpdate();
}
connection.commit();
long end = System.nanoTime();
System.out.println("Total time: " + (float)(end-start)/1000000000 );
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
e.printStackTrace(System.out);
}
finally
{
try
{
if(connection != null)
{
connection.close();
}
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
}
class TestSqliteDriver
{
public static int NUM_TEXT = 100;
public static int NUM_INTEGER = 100;
public static int NUM_INSERT = 100000;
public static void doit() throws ClassNotFoundException
{
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sampledriver.db");
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists two_hundred_one");
String create = "create table two_hundred_one (id integer";
for(int i=1; i<=NUM_TEXT; i++)
{
create += ", text_" + i + " text";
}
for(int i=1; i<=NUM_INTEGER; i++)
{
create += ", int_" + i + " integer";
}
create += ")";
statement.executeUpdate(create);
String insert = "insert into two_hundred_one values(?";
for(int i=0; i<NUM_TEXT + NUM_INTEGER; i++)
{
insert += ", ?";
}
insert += ")";
long start = System.nanoTime();
PreparedStatement preparedStatement = connection.prepareStatement(insert);
for(int i=1; i<=NUM_INSERT; i++)
{
preparedStatement.setInt(1, i);
for(int j=1; j<=NUM_TEXT; j++)
{
preparedStatement.setString(j + 1, "Text_" + j);
}
for(int j=1; j<=NUM_INTEGER; j++)
{
preparedStatement.setInt(NUM_TEXT + j + 1, j);
}
preparedStatement.executeUpdate();
}
connection.commit();
long end = System.nanoTime();
System.out.println("Total time: " + (float)(end-start)/1000000000 );
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
e.printStackTrace(System.out);
}
finally
{
try
{
if(connection != null)
{
connection.close();
}
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
}
class TestSqliteWrapper
{
public static void doit()
{
String query;
SQLiteConnection db = null;
SQLiteStatement st = null;
try
{
/* Open connection */
db = new SQLiteConnection(new File("samplewrapper.db"));
db.open(true);
SQLiteStatement stmtDrop = null;
try
{
db.exec("BEGIN");
query = "drop table if exists two_hundred_one";
stmtDrop = db.prepare(query);
stmtDrop.step();
db.exec("COMMIT");
}
catch (Exception ee)
{
//System.out.println (ee.toString());
db.exec("ROLLBACK");
}
finally
{
if (stmtDrop != null)
{
stmtDrop.dispose();
}
}
db.exec("BEGIN");
String create = "create table two_hundred_one (recid integer";
for(int i=1; i<=TestSqliteDriver.NUM_TEXT; i++)
{
create += ", text_" + i + " text";
}
for(int i=1; i<=TestSqliteDriver.NUM_INTEGER; i++)
{
create += ", int_" + i + " integer";
}
create += ")";
db.exec(create);
db.exec("COMMIT");
db.exec("BEGIN");
query = "insert into two_hundred_one (recid";
for(int i=1; i<= TestSqliteDriver.NUM_TEXT; i++)
{
query += ", text_" + i;
}
for(int i=1; i<=TestSqliteDriver.NUM_INTEGER; i++)
{
query += ", int_" + i;
}
query += ") values(?";
for(int i=0; i < TestSqliteDriver.NUM_TEXT + TestSqliteDriver.NUM_INTEGER; i++)
{
query += ", ?";
}
query += ")";
long start = System.nanoTime();
st = db.prepare(query);
for(int i=1; i<=TestSqliteDriver.NUM_INSERT; i++)
{
for(int j=1; j<=TestSqliteDriver.NUM_TEXT; j++)
{
st.bind(j+1, "Text_" + j);
}
for(int j=1; j<=TestSqliteDriver.NUM_INTEGER; j++)
{
st.bind(TestSqliteDriver.NUM_TEXT + j + 1, j);
}
st.bind(1, i);
st.step();
st.reset();
}
db.exec("COMMIT");
long end = System.nanoTime();
System.out.println("Total time: " + (float)(end-start)/1000000000 );
}
catch (Exception ee)
{
System.out.println ("Exception inserting record: " + ee);
ee.printStackTrace(System.err); // test
System.exit(0); // test
}
finally
{
try
{
if (db != null)
{
db.dispose();
}
}
catch (Exception e) { }
try
{
if (st != null)
{
st.dispose();
}
}
catch (Exception e) {}
}
}
}
Ultima modifica di Vincenzo1968 : 05-03-2013 alle 15:42. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:19.





















