1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 import java.util.List ; 6 7 import junit.framework.Test; 8 import junit.framework.TestSuite; 9 import junit.textui.TestRunner; 10 import org.hibernate.classic.Session; 11 import org.hibernate.test.TestCase; 12 13 public class NewPerformanceTest extends TestCase { 14 15 public NewPerformanceTest(String arg0) { 16 super(arg0); 17 } 18 19 public void testPerformance() throws Exception { 20 21 for ( int n=2; n<4000; n*=2 ) { 22 23 Simple[] simples = new Simple[n]; 24 Serializable [] ids = new Serializable [n]; 25 for ( int i=0; i<n; i++ ) { 26 simples[i] = new Simple(); 27 simples[i].init(); 28 simples[i].setCount(i); 29 ids[i] = new Long (i); 30 } 31 32 Session s = openSession(); 33 prepare(s, simples, ids, n); 34 s.close(); 35 36 long find = 0; 37 long flush = 0; 38 39 for ( int i=0; i<100; i++ ) { 40 41 s = openSession(); 42 long time = System.currentTimeMillis(); 43 List list = s.createQuery("from Simple s where not s.name='osama bin laden' and s.other is null").list(); 44 find += System.currentTimeMillis() - time; 45 assertTrue( list.size()==n ); 46 time = System.currentTimeMillis(); 47 s.flush(); 48 flush += System.currentTimeMillis() - time; 49 time = System.currentTimeMillis(); 50 s.connection().commit(); 51 find += System.currentTimeMillis() - time; 52 s.close(); 53 54 } 55 56 System.out.println( "Objects: " + n + " - find(): " + find + "ms / flush(): " + flush + "ms / Ratio: " + ( (float) flush )/find ); 57 System.out.println( "Objects: " + n + " flush time per object: " + flush / 100.0 / n ); 58 System.out.println( "Objects: " + n + " load time per object: " + find / 100.0 / n ); 59 s = openSession(); 60 delete(s); 61 s.close(); 62 63 } 64 } 65 66 private void prepare(Session s, Simple[] simples, Serializable [] ids, int N) throws Exception { 67 for ( int i=0; i<N; i++ ) { 68 s.save( simples[i], ids[i] ); 69 } 70 s.flush(); 71 s.connection().commit(); 72 } 73 74 private void delete(Session s) throws Exception { 75 s.delete("from Simple s"); 76 s.flush(); 77 s.connection().commit(); 78 } 79 80 public String [] getMappings() { 81 return new String [] { "legacy/Simple.hbm.xml" }; 82 } 83 84 public static Test suite() throws Exception { 85 return new TestSuite(NewPerformanceTest.class); 86 } 87 88 public static void main(String [] args) throws Exception { 89 TestRunner.run( suite() ); 90 } 91 92 } 93 | Popular Tags |