1 30 31 32 package org.hsqldb.test; 33 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.sql.PreparedStatement ; 37 import java.sql.Statement ; 38 import java.sql.DatabaseMetaData ; 39 import java.sql.ResultSet ; 40 import java.util.Random ; 41 42 45 public class TestStressInsert { 46 47 private Connection con; 48 private PreparedStatement insertStmt; 49 private static final int MAX_SIZE = 800000; 50 private final Random random = new Random (0); 51 byte[] data = getRandomBytes(MAX_SIZE); 52 53 public void init() throws Exception { 54 55 String driver = "org.hsqldb.jdbcDriver"; 56 String url = "jdbc:hsqldb:file:testing/test"; 57 58 Class.forName(driver); 59 60 con = DriverManager.getConnection(url, "sa", ""); 61 62 con.setAutoCommit(true); 63 64 Statement stmt = con.createStatement(); 66 67 try { 68 stmt.execute("set property \"hsqldb.cache_scale\" 8"); 70 stmt.execute("set property \"hsqldb.cache_size_scale\" 10"); 71 stmt.execute("set write_delay 0"); 72 stmt.execute("set logsize " + 100); 73 74 DatabaseMetaData metaData = con.getMetaData(); 75 ResultSet rs = metaData.getTables(null, null, "A", null); 76 boolean schemaExists; 77 78 try { 79 schemaExists = rs.next(); 80 } finally { 81 rs.close(); 82 } 83 84 if (!schemaExists) { 85 stmt.execute( 86 "create cached table A (ID binary(16) PRIMARY KEY, DATA varbinary not null)"); 87 } 88 89 stmt.execute("checkpoint"); 90 } finally { 91 stmt.close(); 92 } 93 94 insertStmt = 96 con.prepareStatement("insert into A (DATA, ID) values (?, ?)"); 97 } 98 99 public void shutdown() throws Exception { 100 insertStmt.close(); 101 con.close(); 102 } 103 104 public void insert(byte[] id) throws Exception { 105 106 try { 107 insertStmt.setBytes(1, data); 108 insertStmt.setBytes(2, id); 109 insertStmt.execute(); 110 } finally { 111 insertStmt.clearParameters(); 112 insertStmt.clearWarnings(); 113 } 114 } 115 116 public static void main(String [] args) { 117 118 try { 119 TestStressInsert test = new TestStressInsert(); 120 long t1 = System.currentTimeMillis(); 121 122 System.out.print("Initializing..."); 123 test.init(); 124 125 long t2 = System.currentTimeMillis(); 126 127 System.out.println("done " + (t2 - t1)); 128 129 for (int i = 0; i < MAX_SIZE; i++) { 130 test.insert(test.getRandomBytes(16)); 131 132 133 if (i %100 == 0 ) { 134 long t3 = System.currentTimeMillis(); 135 System.out.println("inserted " + i + " in " + (t3 - t2)); 136 t2 = t3; 137 } 138 } 139 140 test.shutdown(); 141 } catch (Exception e) { 142 e.printStackTrace(); 143 } 144 } 145 146 private byte[] getRandomBytes(int length) { 147 148 byte[] ret = new byte[length]; 149 150 random.nextBytes(ret); 151 152 return ret; 153 } 154 } 155 | Popular Tags |