001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.jdbc;
017:
018: import scriptella.DBTestCase;
019:
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.List;
025:
026: /**
027: * Performance tests for {@link scriptella.jdbc.StatementCache}.
028: *
029: * @author Fyodor Kupolov
030: * @version 1.0
031: */
032: public class StatementCachePerfTest extends DBTestCase {
033: private static final int LOOP_COUNT = 2000;
034:
035: private static class TestableStatementCache extends StatementCache {
036:
037: public TestableStatementCache(Connection connection,
038: final int size) {
039: super (connection, size);
040: }
041:
042: @Override
043: protected StatementWrapper.Simple create(final String sql,
044: final JdbcTypesConverter converter) {
045: return new StatementWrapper.Simple(sql) {
046: public void close() {
047: }
048: };
049: }
050:
051: @Override
052: protected StatementWrapper.Prepared prepare(final String sql,
053: final JdbcTypesConverter converter) {
054: return new StatementWrapper.Prepared() {
055: @Override
056: public void setParameters(List<Object> params) {
057: }
058:
059: @Override
060: public void clear() {
061: }
062:
063: };
064: }
065:
066: }
067:
068: protected void setUp() {
069: sc = new TestableStatementCache(null, 100);
070: }
071:
072: StatementCache sc;
073:
074: /**
075: * History:
076: * 01.10.2006 - Duron 1.7Mhz - 1093 ms
077: */
078: public void testCacheMiss() throws SQLException {
079: //Testing cache miss
080: JdbcTypesConverter converter = new JdbcTypesConverter();
081: List<Object> params = new ArrayList<Object>();
082: params.add(1);
083:
084: for (int i = 0; i < LOOP_COUNT; i++) {
085: setUp();
086: runStatements(sc, params, converter);
087: }
088:
089: }
090:
091: private void runStatements(StatementCache cache,
092: List<Object> params, JdbcTypesConverter converter)
093: throws SQLException {
094: StringBuilder sb = new StringBuilder(150);
095: for (int j = 0; j < 150; j++) {
096: sb.append('.');
097: StatementWrapper s = cache.prepare(sb.toString(),
098: Collections.emptyList(), converter);
099: cache.releaseStatement(s);
100: StatementWrapper s2 = cache.prepare(sb.toString(), params,
101: converter);
102: cache.releaseStatement(s2);
103: }
104:
105: }
106:
107: /**
108: * History:
109: * 01.10.2006 - Duron 1.7Mhz - 1032 ms
110: */
111: public void testCacheHit() throws SQLException {
112: //Testing cache miss
113: JdbcTypesConverter converter = new JdbcTypesConverter();
114: List<Object> params = new ArrayList<Object>();
115: params.add(1);
116:
117: for (int i = 0; i < LOOP_COUNT; i++) {
118: runStatements(sc, params, converter);
119: }
120:
121: }
122:
123: /**
124: * History:
125: * 01.10.2006 - Duron 1.7Mhz - 1032 ms
126: */
127: public void testCacheDisable() throws SQLException {
128: //Testing disabled cache
129: JdbcTypesConverter converter = new JdbcTypesConverter();
130: List<Object> params = new ArrayList<Object>();
131: params.add(1);
132: StatementCache disabled = new TestableStatementCache(null, -1);
133:
134: for (int i = 0; i < LOOP_COUNT; i++) {
135: runStatements(disabled, params, converter);
136: }
137:
138: }
139:
140: }
|