001: package org.apache.ojb.compare;
002:
003: import java.sql.Connection;
004: import java.sql.PreparedStatement;
005: import java.sql.ResultSet;
006:
007: import org.apache.ojb.broker.metadata.ClassDescriptor;
008: import org.apache.ojb.broker.query.Criteria;
009: import org.apache.ojb.broker.query.Query;
010: import org.apache.ojb.broker.query.QueryByCriteria;
011:
012: /**
013: * This TestCase contains the OJB single-threaded performance benchmarks for the
014: * JDBC API. This is the reference for other benchmarks.
015: *
016: * @author Thomas Mahler
017: */
018: public class PerformanceJdbcTest extends PerformanceBaseTest {
019: /**
020: * BrokerTests constructor comment.
021: *
022: * @param name java.lang.String
023: */
024: public PerformanceJdbcTest(String name) {
025: super (name);
026: setNameOfTest("Test for JDBC");
027: }
028:
029: /**
030: * launches the TestCase.
031: * The number of Objects to work with and the number of iterations
032: * to be performed can be adjusted by setting them as commandline parameters.
033: *
034: * @param args the String[] holding the commandline parameters.
035: */
036: public static void main(String[] args) {
037: if (args.length > 0) {
038: articleCount = Integer.parseInt(args[0]);
039: }
040: if (args.length > 1) {
041: iterations = Integer.parseInt(args[1]);
042: }
043:
044: String[] arr = { PerformanceJdbcTest.class.getName() };
045: junit.textui.TestRunner.main(arr);
046: }
047:
048: public void testBenchmark() throws Exception {
049: super .testBenchmark();
050: }
051:
052: /**
053: * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
054: */
055: protected void deleteArticles() throws Exception {
056: Connection conn = getConnection();
057:
058: // Use the OJB SqlGenerator to generate SQL Statements. All details about
059: // Table and column names are read from the repository.xml file.
060: ClassDescriptor cld = broker
061: .getClassDescriptor(PerformanceArticle.class);
062: String sql = broker.serviceSqlGenerator()
063: .getPreparedDeleteStatement(cld).getStatement();
064:
065: logger.debug("delete stmt: " + sql);
066:
067: long start = System.currentTimeMillis();
068: try {
069: conn.setAutoCommit(false);
070: PreparedStatement stmt = conn.prepareStatement(sql);
071: for (int i = 0; i < articleCount; i++) {
072: PerformanceArticle a = arr[i];
073: stmt.setInt(1, a.articleId.intValue());
074: stmt.execute();
075: }
076: conn.commit();
077: } catch (Throwable t) {
078: logger.error(t);
079: fail(t.getMessage());
080: } finally {
081: if (conn != null)
082: returnConnection(conn);
083: }
084: long stop = System.currentTimeMillis();
085: logger.info("deleting " + articleCount + " Objects: "
086: + (stop - start) + " msec");
087: }
088:
089: /**
090: * create new PerformanceArticle objects and insert them into the RDBMS.
091: * The number of objects to create is defined by <code>articleCount</code>.
092: */
093: protected void insertNewArticles() throws Exception {
094: Connection conn = getConnection();
095: // Use the OJB SqlGenerator to generate SQL Statements. All details about
096: // Table and column names are read from the repository.xml file.
097: ClassDescriptor cld = broker
098: .getClassDescriptor(PerformanceArticle.class);
099: String sql = broker.serviceSqlGenerator()
100: .getPreparedInsertStatement(cld).getStatement();
101:
102: logger.debug("insert stmt: " + sql);
103:
104: long start = System.currentTimeMillis();
105: try {
106: conn.setAutoCommit(false);
107: PreparedStatement stmt = conn.prepareStatement(sql);
108:
109: for (int i = 0; i < articleCount; i++) {
110: PerformanceArticle a = arr[i];
111:
112: stmt.setInt(1, a.articleId.intValue());
113: stmt.setString(2, a.articleName);
114: stmt.setInt(3, a.supplierId);
115: stmt.setInt(4, a.productGroupId);
116: stmt.setString(5, a.unit);
117: stmt.setDouble(6, a.price);
118: stmt.setInt(7, a.stock);
119: stmt.setInt(8, a.orderedUnits);
120: stmt.setInt(9, a.minimumStock);
121:
122: stmt.execute();
123: }
124: conn.commit();
125: } catch (Throwable t) {
126: logger.error(t);
127: fail(t.getMessage());
128: } finally {
129: if (conn != null)
130: returnConnection(conn);
131: }
132: long stop = System.currentTimeMillis();
133: logger.info("inserting " + articleCount + " Objects: "
134: + (stop - start) + " msec");
135:
136: }
137:
138: protected void clearTable() throws Exception {
139: Connection conn = getConnection();
140: ClassDescriptor cld = broker
141: .getClassDescriptor(PerformanceArticle.class);
142: String table = cld.getFullTableName();
143: String column = cld.getFieldDescriptorByName("articleId")
144: .getColumnName();
145: String sql = "DELETE FROM " + table + " WHERE " + column
146: + " >= " + offsetId;
147: PreparedStatement stmt = conn.prepareStatement(sql);
148: stmt.execute();
149: returnConnection(conn);
150: }
151:
152: /**
153: * read in all the PerformanceArticles from the RDBMS that have
154: * been inserted by <code>insertNewArticles()</code>.
155: * The lookup is done one by one, that is: a primary key based lookup is used.
156: */
157: protected void readArticles() throws Exception {
158: Connection conn = getConnection();
159:
160: // Use the OJB SqlGenerator to generate SQL Statements. All details about
161: // Table and column names are read from the repository.xml file.
162: ClassDescriptor cld = broker
163: .getClassDescriptor(PerformanceArticle.class);
164: String sql = broker.serviceSqlGenerator()
165: .getPreparedSelectByPkStatement(cld).getStatement();
166: logger.debug("select stmt: " + sql);
167:
168: String colId = cld.getFieldDescriptorByName("articleId")
169: .getColumnName();
170: String colName = cld.getFieldDescriptorByName("articleName")
171: .getColumnName();
172: String colSupplier = cld.getFieldDescriptorByName("supplierId")
173: .getColumnName();
174: String colGroup = cld
175: .getFieldDescriptorByName("productGroupId")
176: .getColumnName();
177: String colUnit = cld.getFieldDescriptorByName("unit")
178: .getColumnName();
179: String colPrice = cld.getFieldDescriptorByName("price")
180: .getColumnName();
181: String colStock = cld.getFieldDescriptorByName("stock")
182: .getColumnName();
183: String colOrdered = cld
184: .getFieldDescriptorByName("orderedUnits")
185: .getColumnName();
186: String colMin = cld.getFieldDescriptorByName("minimumStock")
187: .getColumnName();
188:
189: long start = System.currentTimeMillis();
190: try {
191: conn.setAutoCommit(false);
192: PreparedStatement stmt = conn.prepareStatement(sql);
193: for (int i = 0; i < articleCount; i++) {
194: stmt.setInt(1, offsetId + i);
195: ResultSet rs = stmt.executeQuery();
196: rs.next();
197:
198: PerformanceArticle a = new PerformanceArticle();
199: a.articleId = new Integer(rs.getInt(colId));
200: a.articleName = rs.getString(colName);
201: a.supplierId = rs.getInt(colSupplier);
202: a.productGroupId = rs.getInt(colGroup);
203: a.unit = rs.getString(colUnit);
204: a.price = rs.getFloat(colPrice);
205: a.stock = rs.getInt(colStock);
206: a.orderedUnits = rs.getInt(colOrdered);
207: a.minimumStock = rs.getInt(colMin);
208: }
209: } catch (Throwable t) {
210: logger.error(t);
211: fail(t.getMessage());
212: } finally {
213: if (conn != null)
214: returnConnection(conn);
215: }
216:
217: long stop = System.currentTimeMillis();
218: logger.info("querying " + articleCount + " Objects: "
219: + (stop - start) + " msec");
220:
221: }
222:
223: /**
224: * read in all the PerformanceArticles from the RDBMS that have
225: * been inserted by <code>insertNewArticles()</code>.
226: * The lookup is done with a cursor fetch,
227: * that is: a between Statement is used to select all inserted PerformanceArticles
228: * and Objects are read in by fetching from the cursor (JDBC ResultSet).
229: */
230: protected void readArticlesByCursor() throws Exception {
231: Connection conn = getConnection();
232:
233: Criteria c = new Criteria();
234: c.addBetween("articleId", new Integer(offsetId), new Integer(
235: offsetId + articleCount));
236: Query query = new QueryByCriteria(PerformanceArticle.class, c);
237:
238: // Use the OJB SqlGenerator to generate SQL Statements. All details about
239: // Table and column names are read from the repository.xml file.
240: ClassDescriptor cld = broker
241: .getClassDescriptor(PerformanceArticle.class);
242: String sql = broker.serviceSqlGenerator()
243: .getPreparedSelectStatement(query, cld).getStatement();
244:
245: logger.debug("select stmt: " + sql);
246:
247: String colId = cld.getFieldDescriptorByName("articleId")
248: .getColumnName();
249: String colName = cld.getFieldDescriptorByName("articleName")
250: .getColumnName();
251: String colSupplier = cld.getFieldDescriptorByName("supplierId")
252: .getColumnName();
253: String colGroup = cld
254: .getFieldDescriptorByName("productGroupId")
255: .getColumnName();
256: String colUnit = cld.getFieldDescriptorByName("unit")
257: .getColumnName();
258: String colPrice = cld.getFieldDescriptorByName("price")
259: .getColumnName();
260: String colStock = cld.getFieldDescriptorByName("stock")
261: .getColumnName();
262: String colOrdered = cld
263: .getFieldDescriptorByName("orderedUnits")
264: .getColumnName();
265: String colMin = cld.getFieldDescriptorByName("minimumStock")
266: .getColumnName();
267:
268: int fetchCount = 0;
269: long start = System.currentTimeMillis();
270: try {
271: conn.setAutoCommit(false);
272: PreparedStatement stmt = conn.prepareStatement(sql);
273: stmt.setInt(1, offsetId);
274: stmt.setInt(2, offsetId + articleCount);
275: ResultSet rs = stmt.executeQuery();
276: while (rs.next()) {
277: fetchCount++;
278:
279: PerformanceArticle a = new PerformanceArticle();
280: a.articleId = new Integer(rs.getInt(colId));
281: a.articleName = rs.getString(colName);
282: a.supplierId = rs.getInt(colSupplier);
283: a.productGroupId = rs.getInt(colGroup);
284: a.unit = rs.getString(colUnit);
285: a.price = rs.getFloat(colPrice);
286: a.stock = rs.getInt(colStock);
287: a.orderedUnits = rs.getInt(colOrdered);
288: a.minimumStock = rs.getInt(colMin);
289: }
290: } catch (Throwable t) {
291: logger.error(t);
292: fail(t.getMessage());
293: } finally {
294: if (conn != null)
295: returnConnection(conn);
296: }
297:
298: long stop = System.currentTimeMillis();
299: logger.info("fetching " + fetchCount + " Objects: "
300: + (stop - start) + " msec");
301:
302: }
303:
304: /**
305: * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
306: * All objects are modified and changes are written to the RDBMS with an UPDATE.
307: */
308: protected void updateExistingArticles() throws Exception {
309: Connection conn = getConnection();
310:
311: // Use the OJB SqlGenerator to generate SQL Statements. All details about
312: // Table and column names are read from the repository.xml file.
313: ClassDescriptor cld = broker
314: .getClassDescriptor(PerformanceArticle.class);
315: String sql = broker.serviceSqlGenerator()
316: .getPreparedUpdateStatement(cld).getStatement();
317: logger.debug("update stmt: " + sql);
318:
319: // update all objects
320: for (int i = 0; i < articleCount; i++) {
321: arr[i].setPrice(arr[i].getPrice() * 1.95583);
322: }
323:
324: long start = System.currentTimeMillis();
325: try {
326: conn.setAutoCommit(false);
327: PreparedStatement stmt = conn.prepareStatement(sql);
328: for (int i = 0; i < articleCount; i++) {
329: PerformanceArticle a = arr[i];
330: stmt.setString(1, a.articleName);
331: stmt.setInt(2, a.supplierId);
332: stmt.setInt(3, a.productGroupId);
333: stmt.setString(4, a.unit);
334: stmt.setDouble(5, a.price);
335: stmt.setInt(6, a.stock);
336: stmt.setInt(7, a.orderedUnits);
337: stmt.setInt(8, a.minimumStock);
338: stmt.setInt(9, a.articleId.intValue());
339: stmt.execute();
340: }
341: conn.commit();
342: } catch (Throwable t) {
343: logger.error(t);
344: fail(t.getMessage());
345: } finally {
346: if (conn != null)
347: returnConnection(conn);
348: }
349: long stop = System.currentTimeMillis();
350: logger.info("updating " + articleCount + " Objects: "
351: + (stop - start) + " msec");
352: }
353: }
|