001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.db;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.Statement;
012: import java.util.Random;
013:
014: import org.h2.test.TestBase;
015: import org.h2.util.MemoryUtils;
016:
017: /**
018: * Tests the memory usage of the cache.
019: */
020: public class TestMemoryUsage extends TestBase {
021:
022: private Connection conn;
023:
024: private void reconnect() throws Exception {
025: if (conn != null) {
026: conn.close();
027: }
028: // Class.forName("org.hsqldb.jdbcDriver");
029: // conn = DriverManager.getConnection("jdbc:hsqldb:test", "sa", "");
030: conn = getConnection("memoryUsage");
031: }
032:
033: public void test() throws Exception {
034: deleteDb("memoryUsage");
035: testCreateIndex();
036: testClob();
037: deleteDb("memoryUsage");
038: testReconnectOften();
039: deleteDb("memoryUsage");
040: reconnect();
041: insertUpdateSelectDelete();
042: reconnect();
043: insertUpdateSelectDelete();
044: conn.close();
045: }
046:
047: private void testClob() throws Exception {
048: if (config.memory || !config.big) {
049: return;
050: }
051: Connection conn = getConnection("memoryUsage");
052: Statement stat = conn.createStatement();
053: stat.execute("SET MAX_LENGTH_INPLACE_LOB 32768");
054: stat.execute("SET CACHE_SIZE 8000");
055: stat.execute("CREATE TABLE TEST(ID IDENTITY, DATA CLOB)");
056: System.gc();
057: System.gc();
058: int start = MemoryUtils.getMemoryUsed();
059: for (int i = 0; i < 4; i++) {
060: stat
061: .execute("INSERT INTO TEST(DATA) SELECT SPACE(32000) FROM SYSTEM_RANGE(1, 200)");
062: System.gc();
063: System.gc();
064: int used = MemoryUtils.getMemoryUsed();
065: if ((used - start) > 16000) {
066: error("Used: " + (used - start));
067: }
068: }
069: conn.close();
070: }
071:
072: private void testCreateIndex() throws Exception {
073: if (config.memory) {
074: return;
075: }
076: Connection conn = getConnection("memoryUsage");
077: Statement stat = conn.createStatement();
078: stat.execute("create table test(id int, name varchar)");
079: PreparedStatement prep = conn
080: .prepareStatement("insert into test values(?, space(200) || ?)");
081: int len = getSize(10000, 100000);
082: for (int i = 0; i < len; i++) {
083: prep.setInt(1, i);
084: prep.setInt(2, i);
085: prep.executeUpdate();
086: }
087: int start = MemoryUtils.getMemoryUsed();
088: stat.execute("create index idx_test_id on test(id)");
089: System.gc();
090: System.gc();
091: int used = MemoryUtils.getMemoryUsed();
092: if ((used - start) > 4000) {
093: error("Used: " + (used - start));
094: }
095: stat.execute("drop table test");
096: conn.close();
097: }
098:
099: private void testReconnectOften() throws Exception {
100: int len = getSize(1, 2000);
101: Connection conn1 = getConnection("memoryUsage");
102: printTimeMemory("start", 0);
103: long time = System.currentTimeMillis();
104: for (int i = 0; i < len; i++) {
105: Connection conn2 = getConnection("memoryUsage");
106: conn2.close();
107: if (i % 10000 == 0) {
108: printTimeMemory("connect", System.currentTimeMillis()
109: - time);
110: }
111: }
112: printTimeMemory("connect", System.currentTimeMillis() - time);
113: conn1.close();
114: }
115:
116: void insertUpdateSelectDelete() throws Exception {
117: Statement stat = conn.createStatement();
118: long time;
119: int len = getSize(1, 2000);
120:
121: // insert
122: time = System.currentTimeMillis();
123: stat.execute("DROP TABLE IF EXISTS TEST");
124: trace("drop=" + (System.currentTimeMillis() - time));
125: stat
126: .execute("CREATE CACHED TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
127: PreparedStatement prep = conn
128: .prepareStatement("INSERT INTO TEST VALUES(?, 'Hello World')");
129: printTimeMemory("start", 0);
130: time = System.currentTimeMillis();
131: for (int i = 0; i < len; i++) {
132: prep.setInt(1, i);
133: prep.execute();
134: if (i % 50000 == 0) {
135: trace(" " + (100 * i / len) + "%");
136: }
137: }
138: printTimeMemory("insert", System.currentTimeMillis() - time);
139:
140: // update
141: time = System.currentTimeMillis();
142: prep = conn
143: .prepareStatement("UPDATE TEST SET NAME='Hallo Welt' || ID WHERE ID = ?");
144: for (int i = 0; i < len; i++) {
145: prep.setInt(1, i);
146: prep.execute();
147: if (i % 50000 == 0) {
148: trace(" " + (100 * i / len) + "%");
149: }
150: }
151: printTimeMemory("update", System.currentTimeMillis() - time);
152:
153: // select
154: time = System.currentTimeMillis();
155: prep = conn.prepareStatement("SELECT * FROM TEST WHERE ID = ?");
156: for (int i = 0; i < len; i++) {
157: prep.setInt(1, i);
158: ResultSet rs = prep.executeQuery();
159: rs.next();
160: if (rs.next()) {
161: error("one row expected, got more");
162: }
163: if (i % 50000 == 0) {
164: trace(" " + (100 * i / len) + "%");
165: }
166: }
167: printTimeMemory("select", System.currentTimeMillis() - time);
168:
169: // select randomized
170: Random random = new Random(1);
171: time = System.currentTimeMillis();
172: prep = conn.prepareStatement("SELECT * FROM TEST WHERE ID = ?");
173: for (int i = 0; i < len; i++) {
174: prep.setInt(1, random.nextInt(len));
175: ResultSet rs = prep.executeQuery();
176: rs.next();
177: if (rs.next()) {
178: error("one row expected, got more");
179: }
180: if (i % 50000 == 0) {
181: trace(" " + (100 * i / len) + "%");
182: }
183: }
184: printTimeMemory("select randomized", System.currentTimeMillis()
185: - time);
186:
187: // delete
188: time = System.currentTimeMillis();
189: prep = conn.prepareStatement("DELETE FROM TEST WHERE ID = ?");
190: for (int i = 0; i < len; i++) {
191: prep.setInt(1, random.nextInt(len));
192: prep.executeUpdate();
193: if (i % 50000 == 0) {
194: trace(" " + (100 * i / len) + "%");
195: }
196: }
197: printTimeMemory("delete", System.currentTimeMillis() - time);
198: }
199:
200: }
|