KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 01-05-2004
3  *
4   */

5 package org.hibernate.test.legacy;
6
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.FileWriter JavaDoc;
12 import java.io.FilenameFilter JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
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 /**
30  * @author MAX
31  *
32  */

33 public class ConfigurationPerformanceTest extends TestCase /** purposely not extending hibernate test case to manage the configuration loading 100% on it's own */ {
34
35     String JavaDoc[] files = new String JavaDoc[] {
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 ; // set to true to be able to "coldstart" start.
80

81     public void testLoadingAndSerializationOfConfiguration() throws HibernateException, FileNotFoundException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc {
82
83         String JavaDoc prefix = "./test/org/hibernate/test/";
84         // first time
85
System.err.println("###FIRST SAVELOAD###");
86         saveAndLoad(prefix,files, "hibernate.cfg.bin");
87         // second time to validate
88
System.err.println("###SECOND SAVELOAD###");
89         saveAndLoad(prefix,files, "hibernate.cfg.bin");
90         
91         if(!keepFilesAround) {
92             // clean up
93
File JavaDoc cfgfile = new File JavaDoc("hibernate.cfg.bin");
94             assertTrue(cfgfile.delete());
95             
96             for (int i = 0; i < files.length; i++) {
97                 String JavaDoc file = files[i];
98                 prefix = "./test/org/hibernate/test/";
99                 File JavaDoc binFile = new File JavaDoc(prefix, file+".bin");
100                 assertTrue(binFile.delete());
101             }
102         }
103         
104     }
105     
106     // this method requires generation of test files (can be done with generateTestFiles) + their compile
107
public void xtestSessionFactoryCreationTime() throws FileNotFoundException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc {
108         File JavaDoc perfs = new File JavaDoc("perfsrc");
109         generateTestFiles(perfs, "perftest");
110         if(perfs.exists()) {
111             SessionFactory factory = saveAndLoad("perfsrc/perftest/", new File JavaDoc(perfs, "perftest").list(new FilenameFilter JavaDoc() {
112             
113                 public boolean accept(File JavaDoc dir, String JavaDoc name) {
114                     return name.endsWith(".hbm.xml");
115                 }
116             
117             }), "hibernateperftest.cfg.bin");
118             
119             Session session = factory.openSession();
120             Object JavaDoc o = session.load("perftest.Test1", new Long JavaDoc(42));
121             System.out.println(o);
122         } else {
123             System.err.println(perfs.getAbsoluteFile() + " not found");
124         }
125     }
126
127     private SessionFactory saveAndLoad(String JavaDoc prefix, String JavaDoc[] files, String JavaDoc cfgName) throws IOException JavaDoc, FileNotFoundException JavaDoc, ClassNotFoundException JavaDoc {
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 JavaDoc(cfgName).exists()) {
135             start = System.currentTimeMillis();
136             /*for (int i=0; i<files.length; i++) {
137              if ( !files[i].startsWith("net/") ) files[i] = "test/org/hibernate/test/" + files[i];
138              cfg.addFile(files[i]);
139              //cfg.addLazyFile(files[i]);
140               }*/

141             
142             for (int i = 0; i < files.length; i++) {
143                 String JavaDoc file = files[i];
144                 cfg.addCacheableFile(new File JavaDoc(prefix + file));
145             }
146             
147             System.err.println("Added " + (files.length) + " resources: " + (System.currentTimeMillis() - start) / 1000.0 + " sec.");
148             
149             ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(cfgName));
150             os.writeObject(cfg); // need to serialize Configuration *before* building sf since it would require non-mappings and cfg types to be serializable
151
os.flush();
152             os.close();
153             
154         } else {
155             start = System.currentTimeMillis();
156             ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(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 JavaDoc[] args) throws Exception JavaDoc {
174         TestRunner.run( suite() );
175     }
176     
177     public void generateTestFiles(File JavaDoc basedir, String JavaDoc pkgName) throws IOException JavaDoc {
178         
179         for(int count=0;count<100;count++) {
180             String JavaDoc name = "Test" + count;
181             File JavaDoc javaFile = new File JavaDoc(new File JavaDoc(basedir, pkgName), name + ".java");
182             File JavaDoc hbmFile = new File JavaDoc(new File JavaDoc(basedir, pkgName), name + ".hbm.xml");
183             
184             javaFile.getParentFile().mkdirs();
185             hbmFile.getParentFile().mkdirs();
186             
187             System.out.println("Generating " + javaFile.getAbsolutePath());
188             PrintWriter JavaDoc javaWriter = null;
189             PrintWriter JavaDoc hbmWriter = null;
190             try {
191                 javaWriter = new PrintWriter JavaDoc(new FileWriter JavaDoc(javaFile));
192                 hbmWriter = new PrintWriter JavaDoc(new FileWriter JavaDoc(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 JavaDoc 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 JavaDoc javaWriter, String JavaDoc 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