001: package org.apache.ojb.compare;
002:
003: import java.sql.Connection;
004: import java.sql.PreparedStatement;
005: import java.sql.Statement;
006: import java.sql.SQLException;
007:
008: import org.apache.ojb.broker.metadata.ClassDescriptor;
009: import org.apache.ojb.broker.util.logging.Logger;
010: import org.apache.ojb.broker.util.logging.LoggerFactory;
011: import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
012: import org.apache.ojb.broker.platforms.Platform;
013: import org.apache.ojb.broker.query.Query;
014: import org.apache.ojb.broker.query.QueryFactory;
015: import org.apache.ojb.broker.HsqldbShutdown;
016: import org.apache.ojb.junit.PBTestCase;
017:
018: /**
019: * This is the base class for single-threaded performance benchmarks.
020: *
021: * @author Thomas Mahler
022: */
023: abstract class PerformanceBaseTest extends PBTestCase {
024: protected Logger logger = LoggerFactory.getLogger("performance");
025:
026: private String nameOfTest = "Test performance";
027:
028: /**
029: * the number of PerformanceArticle objects to work with.
030: */
031: protected static int articleCount = 500;
032:
033: /**
034: * the number of iterations to perform.
035: */
036: protected static int iterations = 2;
037:
038: /**
039: * the offset value for PerformanceArticle primary keys
040: */
041: protected final static int offsetId = 12000;
042:
043: protected PerformanceArticle[] arr;
044:
045: /**
046: * BrokerTests constructor comment.
047: *
048: * @param name java.lang.String
049: */
050: public PerformanceBaseTest(String name) {
051: super (name);
052: // setNameOfTest("No name");
053: }
054:
055: /**
056: * setting up the test fixture.
057: */
058: public void setUp() throws Exception {
059: try {
060: super .setUp();
061: clearTable();
062:
063: arr = new PerformanceArticle[articleCount];
064: for (int i = 0; i < articleCount; i++) {
065: PerformanceArticle a = createArticle(offsetId + i);
066: arr[i] = a;
067: }
068: } catch (Exception e) {
069: e.printStackTrace();
070: }
071: }
072:
073: /**
074: * tearing down the test fixture.
075: */
076: public void tearDown() throws Exception {
077: try {
078: clearTable();
079: shutdown();
080: super .tearDown();
081: } catch (Exception e) {
082: e.printStackTrace();
083: }
084: }
085:
086: /**
087: * Set the name of the test.
088: *
089: * @param nameOfTest
090: */
091: public void setNameOfTest(String nameOfTest) {
092: this .nameOfTest = nameOfTest;
093: }
094:
095: protected void clearTable() throws Exception {
096: Connection conn = getConnection();
097: ClassDescriptor cld = broker
098: .getClassDescriptor(PerformanceArticle.class);
099: String table = cld.getFullTableName();
100: String column = cld.getFieldDescriptorByName("articleId")
101: .getColumnName();
102: String sql = "DELETE FROM " + table + " WHERE " + column
103: + " >= " + offsetId;
104: PreparedStatement stmt = conn.prepareStatement(sql);
105: stmt.execute();
106: returnConnection(conn);
107: }
108:
109: /**
110: * factory method that createa an PerformanceArticle with a given id.
111: *
112: * @param id the primary key value for the new object
113: * @return the created PerformanceArticle object
114: */
115: protected PerformanceArticle createArticle(int id) {
116: PerformanceArticle a = new PerformanceArticle();
117: a.setArticleId(new Integer(id));
118: a.setArticleName("New Performance Article " + id);
119: a.setMinimumStock(100);
120: a.setOrderedUnits(17);
121: a.setPrice(100.0);
122: a.setProductGroupId(1);
123: a.setStock(234);
124: a.setSupplierId(4);
125: a.setUnit("bottle");
126: return a;
127: }
128:
129: /**
130: * obtain a JDBC Connection. OJB API is used to make this code portable for
131: * other target dabases and different lookup methods.
132: *
133: * @return the Connection to be used
134: */
135: protected Connection getConnection() throws Exception {
136: // use the PB instance to get access to connection, this doesn't impact performance
137: Connection conn = broker.serviceConnectionManager()
138: .getConnection();
139: return conn;
140: }
141:
142: protected void returnConnection(Connection conn) throws Exception {
143:
144: }
145:
146: /**
147: * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
148: */
149: protected abstract void deleteArticles() throws Exception;
150:
151: /**
152: * create new PerformanceArticle objects and insert them into the RDBMS.
153: * The number of objects to create is defined by <code>articleCount</code>.
154: */
155: protected abstract void insertNewArticles() throws Exception;
156:
157: /**
158: * read in all the PerformanceArticles from the RDBMS that have
159: * been inserted by <code>insertNewArticles()</code>.
160: * The lookup is done one by one, that is: a primary key based lookup is used.
161: */
162: protected abstract void readArticles() throws Exception;
163:
164: /**
165: * read in all the PerformanceArticles from the RDBMS that have
166: * been inserted by <code>insertNewArticles()</code>.
167: * The lookup is done with a cursor fetch,
168: * that is: a between Statement is used to select all inserted PerformanceArticles
169: * and Objects are read in by fetching from the cursor (JDBC ResultSet).
170: */
171: protected abstract void readArticlesByCursor() throws Exception;
172:
173: /**
174: * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
175: * All objects are modified and changes are written to the RDBMS with an UPDATE.
176: */
177: protected abstract void updateExistingArticles() throws Exception;
178:
179: /**
180: * This method is the driver for the complete Benchmark.
181: * It performs the following steps:
182: * <p/>
183: * 1.) n objects are created and inserted to the RDBMS.
184: * 2.) the created objects are modified. Modifications are written to the RDBMS with updates.
185: * 3.) All objects created in 1.) are read in by primary key based SELECT statements.
186: * 4.) Step 3.) is repeated to test caching facilities.
187: * 5.) All objects created in 1.) are read by iterating over a ResultSet.
188: * 6.) All objects created in 1.) are deleted with n separate DELETE Statements.
189: */
190: public void testBenchmark() throws Exception {
191: try {
192: logger.info(nameOfTest);
193: for (int i = 0; i < iterations; i++) {
194: logger.info("");
195:
196: // store all Article objects
197: insertNewArticles();
198:
199: // update all objects
200: updateExistingArticles();
201:
202: // querying
203: readArticles();
204:
205: readArticles();
206:
207: // fetching objects
208: readArticlesByCursor();
209:
210: // delete all objects
211: deleteArticles();
212: }
213: } catch (Exception e) {
214: logger.error(e);
215: throw e;
216: }
217: }
218:
219: public void shutdown() {
220: Platform platform = broker.serviceConnectionManager()
221: .getSupportedPlatform();
222:
223: if (platform instanceof PlatformHsqldbImpl) {
224: Connection con = null;
225: Statement stmt = null;
226:
227: try {
228: con = broker.serviceConnectionManager().getConnection();
229: stmt = con.createStatement();
230: stmt.execute("shutdown");
231: } catch (Exception e) {
232: e.printStackTrace();
233: } finally {
234: try {
235: if (con != null)
236: con.close();
237: if (stmt != null)
238: stmt.close();
239:
240: } catch (SQLException e1) {
241: e1.printStackTrace();
242: }
243: }
244: }
245:
246: }
247: }
|