KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > cases > TestFullTextSpeed


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.cases;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DriverManager JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.Statement JavaDoc;
12 import java.util.Random JavaDoc;
13
14 import org.h2.tools.DeleteDbFiles;
15
16 public class TestFullTextSpeed {
17     
18     private static final int RECORD_COUNT = 100;
19     private Connection JavaDoc conn;
20     
21     // java -Xrunhprof:cpu=samples,depth=6 org.h2.test.cases.TestFullTextSpeed
22

23     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
24 // new TestFullTextSpeed().test(true);
25
new TestFullTextSpeed().test(false);
26 // new TestFullTextSpeed().test(true);
27
new TestFullTextSpeed().test(false);
28     }
29
30     public void test(boolean lucene) throws Exception JavaDoc {
31         String JavaDoc type = lucene ? "FTL_" : "FT_";
32         String JavaDoc 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 JavaDoc stat = conn.createStatement();
38         stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
39         PreparedStatement JavaDoc prep = conn.prepareStatement("INSERT INTO TEST(NAME) VALUES(?)");
40         Random JavaDoc random = new Random JavaDoc(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 JavaDoc 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 JavaDoc getRandomWord(Random JavaDoc random) {
68         return "" + (char)(random.nextInt('z'-'a') + 'a') + random.nextInt(RECORD_COUNT);
69     }
70
71     private String JavaDoc getRandomText(Random JavaDoc random) {
72         int words = random.nextInt(1000);
73         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(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