Source Code Cross Referenced for PerformanceBaseTest.java in  » Database-ORM » db-ojb » org » apache » ojb » compare » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.compare 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.