KickJava   Java API By Example, From Geeks To Geeks.

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


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.Statement JavaDoc;
11 import java.util.Random JavaDoc;
12
13 import org.h2.tools.DeleteDbFiles;
14 import org.h2.util.MemoryUtils;
15
16 public class TestLinearIndex {
17     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
18         Class.forName("org.h2.Driver");
19         // stat.execute("create unique index idxid on test(id)");
20
int len = 1000;
21         for(int a=0; ; a++) {
22             testLoop(true, len);
23             testLoop(false, len);
24             len += 1000;
25         }
26         // hash: 23156
27
// btree: 10250
28
}
29     
30     private static void testLoop(boolean hashIndex, int len) throws Exception JavaDoc {
31         DeleteDbFiles.execute(".", "test", true);
32         String JavaDoc url = "jdbc:h2:test";
33         Connection JavaDoc conn = DriverManager.getConnection(url, "sa", "sa");
34         Statement JavaDoc stat = conn.createStatement();
35         stat.execute("drop table if exists test");
36         stat.execute("create table test(id int, name varchar)");
37         if(hashIndex) {
38             stat.execute("create unique hash index idxid on test(id)");
39         } else {
40             stat.execute("create unique index idxid on test(id)");
41         }
42         stat.execute("insert into test select x, 'Hello World' from system_range(1, "+len+")");
43         PreparedStatement JavaDoc prep = conn.prepareStatement("select * from test where id=?");
44         Random JavaDoc random = new Random JavaDoc(1);
45         long time = System.currentTimeMillis();
46         for(int i=0; i<len*50; i++) {
47             prep.setInt(1, random.nextInt(len));
48             prep.executeQuery();
49         }
50         time = System.currentTimeMillis() - time;
51         System.out.println("len: " + len + " hash: " + hashIndex + " time: " + time+" used: " + MemoryUtils.getMemoryUsed());
52         conn.close();
53     }
54
55 }
56
Popular Tags