KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: NewerPerformanceTest.java,v 1.1 2004/09/26 05:18:25 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.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.hibernate.classic.Session;
16 import org.hibernate.cfg.Environment;
17 import org.hibernate.connection.ConnectionProvider;
18 import org.hibernate.connection.ConnectionProviderFactory;
19 import org.hibernate.test.TestCase;
20
21 public class NewerPerformanceTest extends TestCase {
22
23     public NewerPerformanceTest(String JavaDoc arg0) {
24         super(arg0);
25     }
26
27     public void testMany() throws Exception JavaDoc {
28
29         ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
30
31         long hiber=0;
32         long jdbc=0;
33
34         for ( int n=0; n<20; n++ ) {
35
36             Session s = openSession();
37             s.delete("from Simple");
38             s.flush();
39             Simple[] simples = new Simple[n];
40             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
41             for ( int i=0; i<n; i++ ) {
42                 simples[i] = new Simple();
43                 simples[i].init();
44                 simples[i].setCount(i);
45                 ids[i] = new Long JavaDoc(i);
46                 s.save(simples[i], ids[i]);
47             }
48             s.flush();
49             s.connection().commit();
50             s.close();
51
52             //allow cache to settle
53

54             s = openSession();
55             hibernate(s, simples, ids, n, "h0");
56             s.close();
57
58             Connection JavaDoc c = cp.getConnection();
59             directJDBC( c, simples, ids, n, "j0" );
60             cp.closeConnection(c);
61
62             s = openSession();
63             hibernate(s, simples, ids, n, "h0");
64             s.close();
65
66             c = cp.getConnection();
67             directJDBC( c, simples, ids, n, "j0" );
68             cp.closeConnection(c);
69
70             //Now do timings
71

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

154             s = openSession();
155             hibernate(s, simples, ids, n, "h0");
156             s.close();
157
158             Connection JavaDoc c = cp.getConnection();
159             directJDBC( c, simples, ids, n, "j0" );
160             cp.closeConnection(c);
161
162             s = openSession();
163             hibernate(s, simples, ids, n, "h0");
164             s.close();
165
166             c = cp.getConnection();
167             directJDBC( c, simples, ids, n, "j0" );
168             cp.closeConnection(c);
169
170             //Now do timings
171

172             s = openSession();
173             long time = System.currentTimeMillis();
174             hibernate(s, simples, ids, n, "h1");
175             long hiber = System.currentTimeMillis() - time;
176             s.close();
177
178             c = cp.getConnection();
179             time = System.currentTimeMillis();
180             directJDBC( c, simples, ids, n, "j1" );
181             long jdbc = System.currentTimeMillis() - time;
182             cp.closeConnection(c);
183
184             s = openSession();
185             time = System.currentTimeMillis();
186             hibernate(s, simples, ids, n, "h2");
187             hiber += System.currentTimeMillis() - time;
188             s.close();
189
190             c = cp.getConnection();
191             time = System.currentTimeMillis();
192             directJDBC( c, simples, ids, n, "j2" );
193             jdbc += System.currentTimeMillis() - time;
194             cp.closeConnection(c);
195
196             s = openSession();
197             time = System.currentTimeMillis();
198             hibernate(s, simples, ids, n, "h2");
199             hiber += System.currentTimeMillis() - time;
200             s.close();
201
202             c = cp.getConnection();
203             time = System.currentTimeMillis();
204             directJDBC( c, simples, ids, n, "j2" );
205             jdbc += System.currentTimeMillis() - time;
206             cp.closeConnection(c);
207
208             System.out.println( "Objects: " + n + " - Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );
209
210         }
211
212         cp.close();
213         System.gc();
214     }
215
216     public void testHibernateOnly() throws Exception JavaDoc {
217
218         for ( int n=2; n<4000; n*=2 ) {
219
220             Session s = openSession();
221             Simple[] simples = new Simple[n];
222             s.delete("from Simple");
223             s.flush();
224             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
225             for ( int i=0; i<n; i++ ) {
226                 simples[i] = new Simple();
227                 simples[i].init();
228                 simples[i].setCount(i);
229                 ids[i] = new Long JavaDoc(i);
230                 s.save(simples[i], ids[i]);
231             }
232             s.flush();
233             s.connection().commit();
234             s.close();
235
236             //Now do timings
237

238             s = openSession();
239             long time = System.currentTimeMillis();
240             hibernate(s, simples, ids, n, "h1");
241             long hiber = System.currentTimeMillis() - time;
242             s.close();
243
244             s = openSession();
245             time = System.currentTimeMillis();
246             hibernate(s, simples, ids, n, "h2");
247             hiber += System.currentTimeMillis() - time;
248             s.close();
249
250             s = openSession();
251             time = System.currentTimeMillis();
252             hibernate(s, simples, ids, n, "h2");
253             hiber += System.currentTimeMillis() - time;
254             s.close();
255
256             System.out.println( "Objects: " + n + " - Hibernate: " + hiber );
257
258         }
259
260         System.gc();
261     }
262
263     public void testJdbcOnly() throws Exception JavaDoc {
264
265         ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
266
267         for ( int n=2; n<4000; n*=2 ) {
268
269             Session s = openSession();
270             Simple[] simples = new Simple[n];
271             s.delete("from Simple");
272             s.flush();
273             Serializable JavaDoc[] ids = new Serializable JavaDoc[n];
274             for ( int i=0; i<n; i++ ) {
275                 simples[i] = new Simple();
276                 simples[i].init();
277                 simples[i].setCount(i);
278                 ids[i] = new Long JavaDoc(i);
279                 s.save(simples[i], ids[i]);
280             }
281             s.flush();
282             s.connection().commit();
283             s.close();
284
285
286             //Now do timings
287

288             Connection JavaDoc c = cp.getConnection();
289             long time = System.currentTimeMillis();
290             directJDBC( c, simples, ids, n, "j1" );
291             long jdbc = System.currentTimeMillis() - time;
292             cp.closeConnection(c);
293
294             c = cp.getConnection();
295             time = System.currentTimeMillis();
296             directJDBC( c, simples, ids, n, "j2" );
297             jdbc += System.currentTimeMillis() - time;
298             cp.closeConnection(c);
299
300             c = cp.getConnection();
301             time = System.currentTimeMillis();
302             directJDBC( c, simples, ids, n, "j2" );
303             jdbc += System.currentTimeMillis() - time;
304             cp.closeConnection(c);
305
306             System.out.println( "Objects: " + n + " Direct JDBC: " + jdbc );
307
308         }
309
310         cp.close();
311         System.gc();
312     }
313
314     /*private void hibernate(Session s, Simple[] simples, Serializable[] ids, int N, String runname) throws Exception {
315         for ( int i=0; i<N; i++ ) {
316             s.get( Simple.class, ids[i] );
317             //s.find("from Simple s where s.id = ?", ids[i], Hibernate.LONG);
318         }
319         //s.flush();
320         s.connection().commit();
321     }
322
323     private void directJDBC(Connection c, Simple[] simples, Serializable[] ids, int N, String runname) throws SQLException {
324
325         for ( int i=0; i<N; i++ ) {
326             PreparedStatement select = c.prepareStatement("SELECT s.id_, s.name, s.address, s.count_, s.date_, s.pay, s.other FROM Simple s where s.id_=?");
327             select.setLong( 1, ( (Long) ids[i] ).longValue() );
328             ResultSet rs = select.executeQuery();
329             rs.next();
330             /*new Long( rs.getLong(1) );
331             rs.getString(2);
332             rs.getString(3);
333             rs.getInt(4);
334             rs.getDate(5);
335             rs.getFloat(6);
336             rs.getLong(7);*/

337             /*Simple s = new Simple();
338             new Long( rs.getLong("id_") ); rs.wasNull();
339             s.setName( rs.getString("name") ); rs.wasNull();
340             s.setAddress( rs.getString("address") ); rs.wasNull();
341             s.setCount( rs.getInt("count_") ); rs.wasNull();
342             s.setDate( rs.getTimestamp("date_") ); rs.wasNull();
343             s.setPay( new Float( rs.getFloat("pay") ) ); rs.wasNull();
344             rs.getLong("other"); rs.wasNull(); s.setOther(null);
345             rs.close();
346             select.close();
347         }
348         c.commit();
349     }*/

350
351     private void hibernate(Session s, Simple[] simples, Serializable JavaDoc[] ids, int N, String JavaDoc runname) throws Exception JavaDoc {
352         s.createQuery("from Simple s").list();
353         s.connection().commit();
354     }
355
356     private void directJDBC(Connection JavaDoc c, Simple[] simples, Serializable JavaDoc[] ids, int N, String JavaDoc runname) throws SQLException JavaDoc {
357         List JavaDoc result=new ArrayList JavaDoc();
358         PreparedStatement JavaDoc select = c.prepareStatement("SELECT s.id_ as id_, s.name as name, s.address as address, s.count_ as count_, s.date_ as date_, s.pay as pay, s.other as other FROM Simple s");
359         ResultSet JavaDoc rs = select.executeQuery();
360         while ( rs.next() ) {
361             /*new Long( rs.getLong(1) );
362             rs.getString(2);
363             rs.getString(3);
364             rs.getInt(4);
365             rs.getDate(5);
366             rs.getFloat(6);
367             rs.getLong(7);*/

368             Simple s = new Simple();
369             new Long JavaDoc( rs.getLong("id_") ); rs.wasNull();
370             s.setName( rs.getString("name") ); rs.wasNull();
371             s.setAddress( rs.getString("address") ); rs.wasNull();
372             s.setCount( rs.getInt("count_") ); rs.wasNull();
373             s.setDate( rs.getTimestamp("date_") ); rs.wasNull();
374             s.setPay( new Float JavaDoc( rs.getFloat("pay") ) ); rs.wasNull();
375             rs.getLong("other"); rs.wasNull(); s.setOther(null);
376             result.add(s);
377         }
378         rs.close();
379         select.close();
380         c.commit();
381     }
382
383     public String JavaDoc[] getMappings() {
384         return new String JavaDoc[] { "legacy/Simple.hbm.xml" };
385     }
386
387     public static Test suite() {
388         return new TestSuite(NewerPerformanceTest.class);
389     }
390
391     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
392         TestRunner.run( suite() );
393     }
394
395 }
396
397
398
399
400
401
402
403
Popular Tags