| 1 5 package org.h2.test.cases; 6 7 import java.sql.Connection ; 8 import java.sql.DriverManager ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.Statement ; 12 import java.util.Random ; 13 14 import org.h2.tools.DeleteDbFiles; 15 16 public class TestFullTextSpeed { 17 18 private static final int RECORD_COUNT = 100; 19 private Connection conn; 20 21 23 public static void main(String [] args) throws Exception { 24 new TestFullTextSpeed().test(false); 26 new TestFullTextSpeed().test(false); 28 } 29 30 public void test(boolean lucene) throws Exception { 31 String type = lucene ? "FTL_" : "FT_"; 32 String init = lucene ? "org.h2.fulltext.FullTextLucene.init" : "org.h2.fulltext.FullText.init"; 33 System.out.println((lucene ? "Lucene" : "Native") + " full text search"); 34 DeleteDbFiles.execute(null, "test", true); 35 Class.forName("org.h2.Driver"); 36 conn = DriverManager.getConnection("jdbc:h2:test;ASSERT=FALSE;LOG=0", "sa", "sa"); 37 Statement stat = conn.createStatement(); 38 stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)"); 39 PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(NAME) VALUES(?)"); 40 Random random = new Random (0); 41 for(int i=0; i<RECORD_COUNT; i++) { 42 prep.setString(1, getRandomText(random)); 43 prep.execute(); 44 } 45 long time; 46 time = System.currentTimeMillis(); 47 stat.execute("CREATE ALIAS IF NOT EXISTS "+type+"INIT FOR \""+init+"\""); 48 stat.execute("CALL "+type+"INIT()"); 49 stat.execute("CALL "+type+"DROP_ALL()"); 50 stat.execute("CALL "+type+"CREATE_INDEX('PUBLIC', 'TEST', NULL)"); 51 System.out.println("index: " + (System.currentTimeMillis() - time)); 52 prep = conn.prepareStatement("SELECT * FROM "+type+"SEARCH(?, 0, 0)"); 53 time = System.currentTimeMillis(); 54 int totalResults = 0; 55 for(int i=0; i<1000 * RECORD_COUNT; i++) { 56 prep.setString(1, getRandomWord(random)); 57 ResultSet rs = prep.executeQuery(); 58 while(rs.next()) { 59 rs.getString(1); 60 totalResults++; 61 } 62 } 63 System.out.println("query: " + (System.currentTimeMillis() - time) + " results: " + totalResults); 64 conn.close(); 65 } 66 67 private String getRandomWord(Random random) { 68 return "" + (char)(random.nextInt('z'-'a') + 'a') + random.nextInt(RECORD_COUNT); 69 } 70 71 private String getRandomText(Random random) { 72 int words = random.nextInt(1000); 73 StringBuffer buff = new StringBuffer (words * 6); 74 for(int i=0; i<words; i++) { 75 buff.append(getRandomWord(random)); 76 buff.append(' '); 77 } 78 return buff.toString(); 79 } 80 81 } 82 | Popular Tags |