KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > StatementCachePerfTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.DBTestCase;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Performance tests for {@link scriptella.jdbc.StatementCache}.
28  *
29  * @author Fyodor Kupolov
30  * @version 1.0
31  */

32 public class StatementCachePerfTest extends DBTestCase {
33     private static final int LOOP_COUNT = 2000;
34
35     private static class TestableStatementCache extends StatementCache {
36
37         public TestableStatementCache(Connection JavaDoc connection, final int size) {
38             super(connection, size);
39         }
40         @Override JavaDoc
41         protected StatementWrapper.Simple create(final String JavaDoc sql, final JdbcTypesConverter converter) {
42             return new StatementWrapper.Simple(sql) {
43                 public void close() {
44                 }
45             };
46         }
47
48         @Override JavaDoc
49         protected StatementWrapper.Prepared prepare(final String JavaDoc sql, final JdbcTypesConverter converter) {
50             return new StatementWrapper.Prepared() {
51                 @Override JavaDoc
52                 public void setParameters(List JavaDoc<Object JavaDoc> params) {
53                 }
54
55                 @Override JavaDoc
56                 public void clear() {
57                 }
58
59             };
60         }
61
62     }
63
64     protected void setUp() {
65         sc = new TestableStatementCache(null, 100);
66     }
67
68     StatementCache sc;
69
70     /**
71      * History:
72      * 01.10.2006 - Duron 1.7Mhz - 1093 ms
73      */

74     public void testCacheMiss() throws SQLException JavaDoc {
75         //Testing cache miss
76
JdbcTypesConverter converter = new JdbcTypesConverter();
77         List JavaDoc<Object JavaDoc> params = new ArrayList JavaDoc<Object JavaDoc>();
78         params.add(1);
79
80         for (int i = 0; i < LOOP_COUNT; i++) {
81             setUp();
82             runStatements(sc, params, converter);
83         }
84
85     }
86
87     private void runStatements(StatementCache cache, List JavaDoc<Object JavaDoc> params, JdbcTypesConverter converter) throws SQLException JavaDoc {
88         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(150);
89         for (int j = 0; j < 150; j++) {
90             sb.append('.');
91             StatementWrapper s = cache.prepare(sb.toString(), Collections.emptyList(), converter);
92             cache.releaseStatement(s);
93             StatementWrapper s2 = cache.prepare(sb.toString(), params, converter);
94             cache.releaseStatement(s2);
95         }
96
97     }
98
99     /**
100      * History:
101      * 01.10.2006 - Duron 1.7Mhz - 1032 ms
102      */

103     public void testCacheHit() throws SQLException JavaDoc {
104         //Testing cache miss
105
JdbcTypesConverter converter = new JdbcTypesConverter();
106         List JavaDoc<Object JavaDoc> params = new ArrayList JavaDoc<Object JavaDoc>();
107         params.add(1);
108
109         for (int i = 0; i < LOOP_COUNT; i++) {
110             runStatements(sc, params, converter);
111         }
112
113     }
114     /**
115      * History:
116      * 01.10.2006 - Duron 1.7Mhz - 1032 ms
117      */

118     public void testCacheDisable() throws SQLException JavaDoc {
119         //Testing disabled cache
120
JdbcTypesConverter converter = new JdbcTypesConverter();
121         List JavaDoc<Object JavaDoc> params = new ArrayList JavaDoc<Object JavaDoc>();
122         params.add(1);
123         StatementCache disabled = new TestableStatementCache(null, -1);
124
125         for (int i = 0; i < LOOP_COUNT; i++) {
126             runStatements(disabled, params, converter);
127         }
128
129     }
130
131
132 }
133
Popular Tags