/frontend/lib/src/hibernate-distribution-3.6.7.Final/project/hibernate-testsuite/src/test/perf/org/hibernate/test/perf/PerformanceTest.java

https://github.com/masteinhauser/connectedkitchen · Java · 357 lines · 276 code · 72 blank · 9 comment · 20 complexity · b0e2f6b9d1336a5db19a666eadefef86 MD5 · raw file

  1. package org.hibernate.test.perf;
  2. import java.io.Serializable;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import java.sql.Types;
  7. import junit.framework.Test;
  8. import junit.framework.TestSuite;
  9. import junit.textui.TestRunner;
  10. import org.hibernate.cfg.Environment;
  11. import org.hibernate.classic.Session;
  12. import org.hibernate.connection.ConnectionProvider;
  13. import org.hibernate.connection.ConnectionProviderFactory;
  14. import org.hibernate.testing.junit.functional.FunctionalTestCase;
  15. import org.hibernate.test.legacy.Simple;
  16. public class PerformanceTest extends FunctionalTestCase {
  17. public PerformanceTest(String arg0) {
  18. super(arg0);
  19. }
  20. public String[] getMappings() {
  21. return new String[] { "legacy/Simple.hbm.xml" };
  22. }
  23. public static Test suite() throws Exception {
  24. return new TestSuite(PerformanceTest.class);
  25. }
  26. public static void main(String[] args) throws Exception {
  27. TestRunner.run( suite() );
  28. }
  29. public void testMany() throws Exception {
  30. ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
  31. long hiber=0;
  32. long jdbc=0;
  33. for ( int n=0; n<20; n++ ) {
  34. Simple[] simples = new Simple[n];
  35. Serializable[] ids = new Serializable[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(i);
  41. }
  42. //allow cache to settle
  43. Session s = openSession();
  44. hibernate(s, simples, ids, n, "h0");
  45. s.close();
  46. Connection c = cp.getConnection();
  47. directJDBC( c, simples, ids, n, "j0" );
  48. cp.closeConnection(c);
  49. s = openSession();
  50. hibernate(s, simples, ids, n, "h0");
  51. s.close();
  52. c = cp.getConnection();
  53. directJDBC( c, simples, ids, n, "j0" );
  54. cp.closeConnection(c);
  55. //Now do timings
  56. int N=30;
  57. long time = System.currentTimeMillis();
  58. for (int i=0; i<N; i++) {
  59. s = openSession();
  60. hibernate(s, simples, ids, n, "h1");
  61. s.close();
  62. }
  63. hiber += System.currentTimeMillis() - time;
  64. time = System.currentTimeMillis();
  65. for (int i=0; i<N; i++) {
  66. c = cp.getConnection();
  67. directJDBC( c, simples, ids, n, "j1" );
  68. cp.closeConnection(c);
  69. }
  70. jdbc += System.currentTimeMillis() - time;
  71. time = System.currentTimeMillis();
  72. for (int i=0; i<N; i++) {
  73. s = openSession();
  74. hibernate(s, simples, ids, n, "h2");
  75. s.close();
  76. }
  77. hiber += System.currentTimeMillis() - time;
  78. time = System.currentTimeMillis();
  79. for (int i=0; i<N; i++) {
  80. c = cp.getConnection();
  81. directJDBC( c, simples, ids, n, "j2" );
  82. cp.closeConnection(c);
  83. }
  84. jdbc += System.currentTimeMillis() - time;
  85. time = System.currentTimeMillis();
  86. for (int i=0; i<N; i++) {
  87. s = openSession();
  88. hibernate(s, simples, ids, n, "h1");
  89. s.close();
  90. }
  91. hiber += System.currentTimeMillis() - time;
  92. time = System.currentTimeMillis();
  93. for (int i=0; i<N; i++) {
  94. c = cp.getConnection();
  95. directJDBC( c, simples, ids, n, "j1" );
  96. cp.closeConnection(c);
  97. }
  98. jdbc += System.currentTimeMillis() - time;
  99. }
  100. System.out.println( "Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );
  101. cp.close();
  102. System.gc();
  103. }
  104. public void testSimultaneous() throws Exception {
  105. ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
  106. for ( int n=2; n<4000; n*=2 ) {
  107. Simple[] simples = new Simple[n];
  108. Serializable[] ids = new Serializable[n];
  109. for ( int i=0; i<n; i++ ) {
  110. simples[i] = new Simple();
  111. simples[i].init();
  112. simples[i].setCount(i);
  113. ids[i] = new Long(i);
  114. }
  115. //allow cache to settle
  116. Session s = openSession();
  117. hibernate(s, simples, ids, n, "h0");
  118. s.close();
  119. Connection c = cp.getConnection();
  120. directJDBC( c, simples, ids, n, "j0" );
  121. cp.closeConnection(c);
  122. s = openSession();
  123. hibernate(s, simples, ids, n, "h0");
  124. s.close();
  125. c = cp.getConnection();
  126. directJDBC( c, simples, ids, n, "j0" );
  127. cp.closeConnection(c);
  128. //Now do timings
  129. s = openSession();
  130. long time = System.currentTimeMillis();
  131. hibernate(s, simples, ids, n, "h1");
  132. long hiber = System.currentTimeMillis() - time;
  133. s.close();
  134. c = cp.getConnection();
  135. time = System.currentTimeMillis();
  136. directJDBC( c, simples, ids, n, "j1" );
  137. long jdbc = System.currentTimeMillis() - time;
  138. cp.closeConnection(c);
  139. s = openSession();
  140. time = System.currentTimeMillis();
  141. hibernate(s, simples, ids, n, "h2");
  142. hiber += System.currentTimeMillis() - time;
  143. s.close();
  144. c = cp.getConnection();
  145. time = System.currentTimeMillis();
  146. directJDBC( c, simples, ids, n, "j2" );
  147. jdbc += System.currentTimeMillis() - time;
  148. cp.closeConnection(c);
  149. s = openSession();
  150. time = System.currentTimeMillis();
  151. hibernate(s, simples, ids, n, "h2");
  152. hiber += System.currentTimeMillis() - time;
  153. s.close();
  154. c = cp.getConnection();
  155. time = System.currentTimeMillis();
  156. directJDBC( c, simples, ids, n, "j2" );
  157. jdbc += System.currentTimeMillis() - time;
  158. cp.closeConnection(c);
  159. System.out.println( "Objects: " + n + " - Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );
  160. }
  161. cp.close();
  162. System.gc();
  163. }
  164. public void testHibernateOnly() throws Exception {
  165. for ( int n=2; n<4000; n*=2 ) {
  166. Simple[] simples = new Simple[n];
  167. Serializable[] ids = new Serializable[n];
  168. for ( int i=0; i<n; i++ ) {
  169. simples[i] = new Simple();
  170. simples[i].init();
  171. simples[i].setCount(i);
  172. ids[i] = new Long(i);
  173. }
  174. //Now do timings
  175. Session s = openSession();
  176. long time = System.currentTimeMillis();
  177. hibernate(s, simples, ids, n, "h1");
  178. long hiber = System.currentTimeMillis() - time;
  179. s.close();
  180. s = openSession();
  181. time = System.currentTimeMillis();
  182. hibernate(s, simples, ids, n, "h2");
  183. hiber += System.currentTimeMillis() - time;
  184. s.close();
  185. s = openSession();
  186. time = System.currentTimeMillis();
  187. hibernate(s, simples, ids, n, "h2");
  188. hiber += System.currentTimeMillis() - time;
  189. s.close();
  190. System.out.println( "Objects: " + n + " - Hibernate: " + hiber );
  191. }
  192. System.gc();
  193. }
  194. public void testJdbcOnly() throws Exception {
  195. ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
  196. for ( int n=2; n<4000; n*=2 ) {
  197. Simple[] simples = new Simple[n];
  198. Serializable[] ids = new Serializable[n];
  199. for ( int i=0; i<n; i++ ) {
  200. simples[i] = new Simple();
  201. simples[i].init();
  202. simples[i].setCount(i);
  203. ids[i] = new Long(i);
  204. }
  205. //Now do timings
  206. Connection c = cp.getConnection();
  207. long time = System.currentTimeMillis();
  208. directJDBC( c, simples, ids, n, "j1" );
  209. long jdbc = System.currentTimeMillis() - time;
  210. cp.closeConnection(c);
  211. c = cp.getConnection();
  212. time = System.currentTimeMillis();
  213. directJDBC( c, simples, ids, n, "j2" );
  214. jdbc += System.currentTimeMillis() - time;
  215. cp.closeConnection(c);
  216. c = cp.getConnection();
  217. time = System.currentTimeMillis();
  218. directJDBC( c, simples, ids, n, "j2" );
  219. jdbc += System.currentTimeMillis() - time;
  220. cp.closeConnection(c);
  221. System.out.println( "Objects: " + n + " Direct JDBC: " + jdbc );
  222. }
  223. cp.close();
  224. System.gc();
  225. }
  226. private void hibernate(Session s, Simple[] simples, Serializable[] ids, int N, String runname) throws Exception {
  227. for ( int i=0; i<N; i++ ) {
  228. s.save( simples[i], ids[i] );
  229. }
  230. for ( int i=0; i<N; i++ ) {
  231. simples[0].setName("A Different Name!" + i + N + runname);
  232. }
  233. //s.flush();
  234. // the results of this test are highly dependent upon
  235. // how many times we flush!
  236. assertTrue( "assertion", s.delete("from Simple s")==N );
  237. s.flush();
  238. s.connection().commit();
  239. }
  240. private void directJDBC(Connection c, Simple[] simples, Serializable[] ids, int N, String runname) throws SQLException {
  241. PreparedStatement insert = c.prepareStatement("insert into Simple ( name, address, count_, date_, other, id_ ) values ( ?, ?, ?, ?, ?, ? )");
  242. PreparedStatement delete = c.prepareStatement("delete from Simple where id_ = ?");
  243. PreparedStatement select = c.prepareStatement("SELECT s.id_, s.name, s.address, s.count_, s.date_, s.other FROM Simple s");
  244. PreparedStatement update = c.prepareStatement("update Simple set name = ?, address = ?, count_ = ?, date_ = ?, other = ? where id_ = ?");
  245. for ( int i=0; i<N; i++ ) {
  246. insert.setString(1, simples[i].getName() );
  247. insert.setString(2, simples[i].getAddress() );
  248. insert.setInt(3, simples[i].getCount() );
  249. insert.setDate( 4, (java.sql.Date) simples[i].getDate() );
  250. insert.setNull(5, Types.BIGINT);
  251. insert.setLong( 6, ( (Long) ids[i] ).longValue() );
  252. insert.executeUpdate();
  253. }
  254. insert.close();
  255. for ( int i=0; i<N; i++ ) {
  256. update.setString(1, "A Different Name!" + i + N + runname );
  257. update.setString(2, simples[i].getAddress() );
  258. update.setInt(3, simples[i].getCount() );
  259. update.setDate( 4, (java.sql.Date) simples[i].getDate() );
  260. update.setNull(5, Types.BIGINT);
  261. update.setLong( 6, ( (Long) ids[i] ).longValue() );
  262. update.executeUpdate();
  263. }
  264. update.close();
  265. java.sql.ResultSet rs = select.executeQuery();
  266. Long[] keys = new Long[N];
  267. int j=0;
  268. while ( rs.next() ) {
  269. keys[j++] = new Long( rs.getLong(1) );
  270. rs.getString(2);
  271. rs.getString(3);
  272. rs.getInt(4);
  273. rs.getDate(5);
  274. rs.getLong(6);
  275. }
  276. rs.close();
  277. select.close();
  278. for ( int i=0; i<N; i++ ) {
  279. delete.setLong(1, keys[i].longValue() );
  280. delete.executeUpdate();
  281. }
  282. delete.close();
  283. c.commit();
  284. }
  285. }