1 5 package org.hibernate.test.legacy; 6 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.FileOutputStream ; 11 import java.io.FileWriter ; 12 import java.io.FilenameFilter ; 13 import java.io.IOException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.io.PrintWriter ; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 import org.apache.tools.ant.IntrospectionHelper; 24 import org.hibernate.HibernateException; 25 import org.hibernate.SessionFactory; 26 import org.hibernate.cfg.Configuration; 27 import org.hibernate.classic.Session; 28 29 33 public class ConfigurationPerformanceTest extends TestCase { 34 35 String [] files = new String [] { 36 "legacy/ABC.hbm.xml", 37 "legacy/ABCExtends.hbm.xml", 38 "legacy/Baz.hbm.xml", 39 "legacy/Blobber.hbm.xml", 40 "legacy/Broken.hbm.xml", 41 "legacy/Category.hbm.xml", 42 "legacy/Circular.hbm.xml", 43 "legacy/Commento.hbm.xml", 44 "legacy/ComponentNotNullMaster.hbm.xml", 45 "legacy/Componentizable.hbm.xml", 46 "legacy/Container.hbm.xml", 47 "legacy/Custom.hbm.xml", 48 "legacy/CustomSQL.hbm.xml", 49 "legacy/Eye.hbm.xml", 50 "legacy/Fee.hbm.xml", 51 "legacy/Fo.hbm.xml", 52 "legacy/FooBar.hbm.xml", 53 "legacy/Fum.hbm.xml", 54 "legacy/Fumm.hbm.xml", 55 "legacy/Glarch.hbm.xml", 56 "legacy/Holder.hbm.xml", 57 "legacy/IJ2.hbm.xml", 58 "legacy/Immutable.hbm.xml", 59 "legacy/Location.hbm.xml", 60 "legacy/Many.hbm.xml", 61 "legacy/Map.hbm.xml", 62 "legacy/Marelo.hbm.xml", 63 "legacy/MasterDetail.hbm.xml", 64 "legacy/Middle.hbm.xml", 65 "legacy/Multi.hbm.xml", 66 "legacy/MultiExtends.hbm.xml", 67 "legacy/Nameable.hbm.xml", 68 "legacy/One.hbm.xml", 69 "legacy/ParentChild.hbm.xml", 70 "legacy/Qux.hbm.xml", 71 "legacy/Simple.hbm.xml", 72 "legacy/SingleSeveral.hbm.xml", 73 "legacy/Stuff.hbm.xml", 74 "legacy/UpDown.hbm.xml", 75 "legacy/Vetoer.hbm.xml", 76 "legacy/WZ.hbm.xml", 77 }; 78 79 boolean keepFilesAround = false ; 81 public void testLoadingAndSerializationOfConfiguration() throws HibernateException, FileNotFoundException , IOException , ClassNotFoundException { 82 83 String prefix = "./test/org/hibernate/test/"; 84 System.err.println("###FIRST SAVELOAD###"); 86 saveAndLoad(prefix,files, "hibernate.cfg.bin"); 87 System.err.println("###SECOND SAVELOAD###"); 89 saveAndLoad(prefix,files, "hibernate.cfg.bin"); 90 91 if(!keepFilesAround) { 92 File cfgfile = new File ("hibernate.cfg.bin"); 94 assertTrue(cfgfile.delete()); 95 96 for (int i = 0; i < files.length; i++) { 97 String file = files[i]; 98 prefix = "./test/org/hibernate/test/"; 99 File binFile = new File (prefix, file+".bin"); 100 assertTrue(binFile.delete()); 101 } 102 } 103 104 } 105 106 public void xtestSessionFactoryCreationTime() throws FileNotFoundException , IOException , ClassNotFoundException { 108 File perfs = new File ("perfsrc"); 109 generateTestFiles(perfs, "perftest"); 110 if(perfs.exists()) { 111 SessionFactory factory = saveAndLoad("perfsrc/perftest/", new File (perfs, "perftest").list(new FilenameFilter () { 112 113 public boolean accept(File dir, String name) { 114 return name.endsWith(".hbm.xml"); 115 } 116 117 }), "hibernateperftest.cfg.bin"); 118 119 Session session = factory.openSession(); 120 Object o = session.load("perftest.Test1", new Long (42)); 121 System.out.println(o); 122 } else { 123 System.err.println(perfs.getAbsoluteFile() + " not found"); 124 } 125 } 126 127 private SessionFactory saveAndLoad(String prefix, String [] files, String cfgName) throws IOException , FileNotFoundException , ClassNotFoundException { 128 long start = System.currentTimeMillis(); 129 130 Configuration cfg = new Configuration(); 131 System.err.println("Created configuration: " + (System.currentTimeMillis() - start) / 1000.0 + " sec."); 132 133 System.err.println("saveAndLoad from " + prefix + " with cfg = " + cfgName); 134 if(!new File (cfgName).exists()) { 135 start = System.currentTimeMillis(); 136 141 142 for (int i = 0; i < files.length; i++) { 143 String file = files[i]; 144 cfg.addCacheableFile(new File (prefix + file)); 145 } 146 147 System.err.println("Added " + (files.length) + " resources: " + (System.currentTimeMillis() - start) / 1000.0 + " sec."); 148 149 ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (cfgName)); 150 os.writeObject(cfg); os.flush(); 152 os.close(); 153 154 } else { 155 start = System.currentTimeMillis(); 156 ObjectInputStream is = new ObjectInputStream (new FileInputStream (cfgName)); 157 cfg = (Configuration) is.readObject(); 158 is.close(); 159 System.err.println("Loaded serializable configuration:" + (System.currentTimeMillis() - start) / 1000.0 + " sec."); 160 } 161 start = System.currentTimeMillis(); 162 System.err.println("Start build of session factory"); 163 SessionFactory factory = cfg.buildSessionFactory(); 164 System.err.println("Build session factory:" + (System.currentTimeMillis() - start) / 1000.0 + " sec."); 165 return factory; 166 } 167 168 169 public static Test suite() { 170 return new TestSuite(ConfigurationPerformanceTest.class); 171 } 172 173 public static void main(String [] args) throws Exception { 174 TestRunner.run( suite() ); 175 } 176 177 public void generateTestFiles(File basedir, String pkgName) throws IOException { 178 179 for(int count=0;count<100;count++) { 180 String name = "Test" + count; 181 File javaFile = new File (new File (basedir, pkgName), name + ".java"); 182 File hbmFile = new File (new File (basedir, pkgName), name + ".hbm.xml"); 183 184 javaFile.getParentFile().mkdirs(); 185 hbmFile.getParentFile().mkdirs(); 186 187 System.out.println("Generating " + javaFile.getAbsolutePath()); 188 PrintWriter javaWriter = null; 189 PrintWriter hbmWriter = null; 190 try { 191 javaWriter = new PrintWriter (new FileWriter (javaFile)); 192 hbmWriter = new PrintWriter (new FileWriter (hbmFile)); 193 194 javaWriter.println("package " + pkgName + ";"); 195 hbmWriter.println("<?xml version=\"1.0\"?>\r\n" + 196 "<!DOCTYPE hibernate-mapping PUBLIC \r\n" + 197 " \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\r\n" + 198 " \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\r\n"); 199 200 hbmWriter.println("<hibernate-mapping package=\"" + pkgName + "\">"); 201 202 javaWriter.println("public class " + name + " {"); 203 javaWriter.println(" static { System.out.println(\"" + name + " initialized!\"); }"); 204 hbmWriter.println("<class name=\"" + name + "\">"); 205 206 hbmWriter.println("<id type=\"long\"><generator class=\"assigned\"/></id>"); 207 for(int propCount=0;propCount<100;propCount++) { 208 String propName = "Prop" + propCount; 209 210 writeJavaProperty(javaWriter, propName); 211 212 hbmWriter.println("<property name=\"" + propName + "\" type=\"string\"/>"); 213 214 } 215 hbmWriter.println("</class>"); 216 javaWriter.println("}"); 217 hbmWriter.println("</hibernate-mapping>"); 218 } finally { 219 if(javaWriter!=null) { 220 javaWriter.flush(); 221 javaWriter.close(); 222 } 223 if(hbmWriter!=null) { 224 hbmWriter.flush(); 225 hbmWriter.close(); 226 } 227 } 228 } 229 230 } 231 232 233 private void writeJavaProperty(PrintWriter javaWriter, String propName) { 234 javaWriter.println(" String " + propName + ";"); 235 javaWriter.println(" String get" + propName + "() { return " + propName + "; }"); 236 javaWriter.println(" void set" + propName + "(String newVal) { " + propName + "=newVal; }"); 237 } 238 } 239 | Popular Tags |