KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > PerformanceTest


1 //$Id: PerformanceTest.java,v 1.4 2005/05/25 01:24:22 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Types JavaDoc;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12 import junit.textui.TestRunner;
13 import org.hibernate.classic.Session;
14 import org.hibernate.cfg.Environment;
15 import org.hibernate.connection.ConnectionProvider;
16 import org.hibernate.connection.ConnectionProviderFactory;
17 import org.hibernate.test.TestCase;
18
19 public class PerformanceTest extends TestCase {
20
21     public PerformanceTest(String JavaDoc arg0) {
22         super(arg0);
23     }
24
25     public void testMany() throws Exception JavaDoc {
26
27         ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
28
29         long hiber=0;
30         long jdbc=0;
31
32         for ( int n=0; n<20; n++ ) {
33
34             Simple[] simples = new Simple[n];
35             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
36             for ( int i=0; i<n; i++ ) {
37                 simples[i] = new Simple();
38                 simples[i].init();
39                 simples[i].setCount(i);
40                 ids[i] = new Long JavaDoc(i);
41             }
42
43             //allow cache to settle
44

45             Session s = openSession();
46             hibernate(s, simples, ids, n, "h0");
47             s.close();
48
49             Connection JavaDoc c = cp.getConnection();
50             directJDBC( c, simples, ids, n, "j0" );
51             cp.closeConnection(c);
52
53             s = openSession();
54             hibernate(s, simples, ids, n, "h0");
55             s.close();
56
57             c = cp.getConnection();
58             directJDBC( c, simples, ids, n, "j0" );
59             cp.closeConnection(c);
60
61             //Now do timings
62

63             int N=30;
64
65             long time = System.currentTimeMillis();
66             for (int i=0; i<N; i++) {
67                 s = openSession();
68                 hibernate(s, simples, ids, n, "h1");
69                 s.close();
70             }
71             hiber += System.currentTimeMillis() - time;
72
73             time = System.currentTimeMillis();
74             for (int i=0; i<N; i++) {
75                 c = cp.getConnection();
76                 directJDBC( c, simples, ids, n, "j1" );
77                 cp.closeConnection(c);
78             }
79             jdbc += System.currentTimeMillis() - time;
80
81             time = System.currentTimeMillis();
82             for (int i=0; i<N; i++) {
83                 s = openSession();
84                 hibernate(s, simples, ids, n, "h2");
85                 s.close();
86             }
87             hiber += System.currentTimeMillis() - time;
88
89             time = System.currentTimeMillis();
90             for (int i=0; i<N; i++) {
91                 c = cp.getConnection();
92                 directJDBC( c, simples, ids, n, "j2" );
93                 cp.closeConnection(c);
94             }
95             jdbc += System.currentTimeMillis() - time;
96
97             time = System.currentTimeMillis();
98             for (int i=0; i<N; i++) {
99                 s = openSession();
100                 hibernate(s, simples, ids, n, "h1");
101                 s.close();
102             }
103             hiber += System.currentTimeMillis() - time;
104
105             time = System.currentTimeMillis();
106             for (int i=0; i<N; i++) {
107                 c = cp.getConnection();
108                 directJDBC( c, simples, ids, n, "j1" );
109                 cp.closeConnection(c);
110             }
111             jdbc += System.currentTimeMillis() - time;
112
113         }
114
115         System.out.println( "Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );
116
117         cp.close();
118         System.gc();
119     }
120
121     public void testSimultaneous() throws Exception JavaDoc {
122
123         ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
124
125         for ( int n=2; n<4000; n*=2 ) {
126
127             Simple[] simples = new Simple[n];
128             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
129             for ( int i=0; i<n; i++ ) {
130                 simples[i] = new Simple();
131                 simples[i].init();
132                 simples[i].setCount(i);
133                 ids[i] = new Long JavaDoc(i);
134             }
135
136             //allow cache to settle
137

138             Session s = openSession();
139             hibernate(s, simples, ids, n, "h0");
140             s.close();
141
142             Connection JavaDoc c = cp.getConnection();
143             directJDBC( c, simples, ids, n, "j0" );
144             cp.closeConnection(c);
145
146             s = openSession();
147             hibernate(s, simples, ids, n, "h0");
148             s.close();
149
150             c = cp.getConnection();
151             directJDBC( c, simples, ids, n, "j0" );
152             cp.closeConnection(c);
153
154             //Now do timings
155

156             s = openSession();
157             long time = System.currentTimeMillis();
158             hibernate(s, simples, ids, n, "h1");
159             long hiber = System.currentTimeMillis() - time;
160             s.close();
161
162             c = cp.getConnection();
163             time = System.currentTimeMillis();
164             directJDBC( c, simples, ids, n, "j1" );
165             long jdbc = System.currentTimeMillis() - time;
166             cp.closeConnection(c);
167
168             s = openSession();
169             time = System.currentTimeMillis();
170             hibernate(s, simples, ids, n, "h2");
171             hiber += System.currentTimeMillis() - time;
172             s.close();
173
174             c = cp.getConnection();
175             time = System.currentTimeMillis();
176             directJDBC( c, simples, ids, n, "j2" );
177             jdbc += System.currentTimeMillis() - time;
178             cp.closeConnection(c);
179
180             s = openSession();
181             time = System.currentTimeMillis();
182             hibernate(s, simples, ids, n, "h2");
183             hiber += System.currentTimeMillis() - time;
184             s.close();
185
186             c = cp.getConnection();
187             time = System.currentTimeMillis();
188             directJDBC( c, simples, ids, n, "j2" );
189             jdbc += System.currentTimeMillis() - time;
190             cp.closeConnection(c);
191
192             System.out.println( "Objects: " + n + " - Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );
193
194         }
195
196         cp.close();
197         System.gc();
198     }
199
200     public void testHibernateOnly() throws Exception JavaDoc {
201
202         for ( int n=2; n<4000; n*=2 ) {
203
204             Simple[] simples = new Simple[n];
205             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
206             for ( int i=0; i<n; i++ ) {
207                 simples[i] = new Simple();
208                 simples[i].init();
209                 simples[i].setCount(i);
210                 ids[i] = new Long JavaDoc(i);
211             }
212
213             //Now do timings
214

215             Session s = openSession();
216             long time = System.currentTimeMillis();
217             hibernate(s, simples, ids, n, "h1");
218             long hiber = System.currentTimeMillis() - time;
219             s.close();
220
221             s = openSession();
222             time = System.currentTimeMillis();
223             hibernate(s, simples, ids, n, "h2");
224             hiber += System.currentTimeMillis() - time;
225             s.close();
226
227             s = openSession();
228             time = System.currentTimeMillis();
229             hibernate(s, simples, ids, n, "h2");
230             hiber += System.currentTimeMillis() - time;
231             s.close();
232
233             System.out.println( "Objects: " + n + " - Hibernate: " + hiber );
234
235         }
236
237         System.gc();
238     }
239
240     public void testJdbcOnly() throws Exception JavaDoc {
241
242         ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
243
244         for ( int n=2; n<4000; n*=2 ) {
245
246             Simple[] simples = new Simple[n];
247             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
248             for ( int i=0; i<n; i++ ) {
249                 simples[i] = new Simple();
250                 simples[i].init();
251                 simples[i].setCount(i);
252                 ids[i] = new Long JavaDoc(i);
253             }
254
255             //Now do timings
256

257             Connection JavaDoc c = cp.getConnection();
258             long time = System.currentTimeMillis();
259             directJDBC( c, simples, ids, n, "j1" );
260             long jdbc = System.currentTimeMillis() - time;
261             cp.closeConnection(c);
262
263             c = cp.getConnection();
264             time = System.currentTimeMillis();
265             directJDBC( c, simples, ids, n, "j2" );
266             jdbc += System.currentTimeMillis() - time;
267             cp.closeConnection(c);
268
269             c = cp.getConnection();
270             time = System.currentTimeMillis();
271             directJDBC( c, simples, ids, n, "j2" );
272             jdbc += System.currentTimeMillis() - time;
273             cp.closeConnection(c);
274
275             System.out.println( "Objects: " + n + " Direct JDBC: " + jdbc );
276
277         }
278
279         cp.close();
280         System.gc();
281     }
282
283     private void hibernate(Session s, Simple[] simples, Serializable JavaDoc[] ids, int N, String JavaDoc runname) throws Exception JavaDoc {
284         for ( int i=0; i<N; i++ ) {
285             s.save( simples[i], ids[i] );
286         }
287         for ( int i=0; i<N; i++ ) {
288             simples[0].setName("A Different Name!" + i + N + runname);
289         }
290         //s.flush();
291
// the results of this test are highly dependent upon
292
// how many times we flush!
293
assertTrue( "assertion", s.delete("from Simple s")==N );
294         s.flush();
295         s.connection().commit();
296     }
297
298     private void directJDBC(Connection JavaDoc c, Simple[] simples, Serializable JavaDoc[] ids, int N, String JavaDoc runname) throws SQLException JavaDoc {
299
300         PreparedStatement JavaDoc insert = c.prepareStatement("insert into Simple ( name, address, count_, date_, other, id_ ) values ( ?, ?, ?, ?, ?, ? )");
301         PreparedStatement JavaDoc delete = c.prepareStatement("delete from Simple where id_ = ?");
302         PreparedStatement JavaDoc select = c.prepareStatement("SELECT s.id_, s.name, s.address, s.count_, s.date_, s.other FROM Simple s");
303         PreparedStatement JavaDoc update = c.prepareStatement("update Simple set name = ?, address = ?, count_ = ?, date_ = ?, other = ? where id_ = ?");
304         for ( int i=0; i<N; i++ ) {
305             insert.setString(1, simples[i].getName() );
306             insert.setString(2, simples[i].getAddress() );
307             insert.setInt(3, simples[i].getCount() );
308             insert.setDate( 4, (java.sql.Date JavaDoc) simples[i].getDate() );
309             insert.setNull(5, Types.BIGINT);
310             insert.setLong( 6, ( (Long JavaDoc) ids[i] ).longValue() );
311             insert.executeUpdate();
312         }
313         insert.close();
314         for ( int i=0; i<N; i++ ) {
315             update.setString(1, "A Different Name!" + i + N + runname );
316             update.setString(2, simples[i].getAddress() );
317             update.setInt(3, simples[i].getCount() );
318             update.setDate( 4, (java.sql.Date JavaDoc) simples[i].getDate() );
319             update.setNull(5, Types.BIGINT);
320             update.setLong( 6, ( (Long JavaDoc) ids[i] ).longValue() );
321             update.executeUpdate();
322         }
323         update.close();
324         java.sql.ResultSet JavaDoc rs = select.executeQuery();
325         Long JavaDoc[] keys = new Long JavaDoc[N];
326         int j=0;
327         while ( rs.next() ) {
328             keys[j++] = new Long JavaDoc( rs.getLong(1) );
329             rs.getString(2);
330             rs.getString(3);
331             rs.getInt(4);
332             rs.getDate(5);
333             rs.getLong(6);
334         }
335         rs.close();
336         select.close();
337         for ( int i=0; i<N; i++ ) {
338             delete.setLong(1, keys[i].longValue() );
339             delete.executeUpdate();
340         }
341         delete.close();
342         c.commit();
343     }
344
345     public String JavaDoc[] getMappings() {
346         return new String JavaDoc[] { "legacy/Simple.hbm.xml" };
347     }
348
349     public static Test suite() throws Exception JavaDoc {
350         return new TestSuite(PerformanceTest.class);
351     }
352
353     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
354         TestRunner.run( suite() );
355     }
356 }
357
Popular Tags