FileDocCategorySizeDatePackage
ConfigurationPerformanceTest.javaAPI DocHibernate 3.2.58189Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.legacy

ConfigurationPerformanceTest

public class ConfigurationPerformanceTest extends org.hibernate.junit.UnitTestCase
author
MAX

Fields Summary
String[]
files
boolean
keepFilesAround
Constructors Summary
public ConfigurationPerformanceTest(String string)

 // set to true to be able to "coldstart" start.

	   
		super( string );
	
Methods Summary
public voidgenerateTestFiles(java.io.File basedir, java.lang.String pkgName)

		
		for(int count=0;count<100;count++) {
			String name = "Test" + count;
			File javaFile = new File(new File(basedir, pkgName), name + ".java");
			File hbmFile = new File(new File(basedir, pkgName), name + ".hbm.xml");
			
			javaFile.getParentFile().mkdirs();
			hbmFile.getParentFile().mkdirs();
			
			System.out.println("Generating " + javaFile.getAbsolutePath());
			PrintWriter javaWriter = null;
			PrintWriter hbmWriter = null;
			try {
				javaWriter = new PrintWriter(new FileWriter(javaFile));
				hbmWriter = new PrintWriter(new FileWriter(hbmFile));
				
				javaWriter.println("package " + pkgName + ";");
				hbmWriter.println("<?xml version=\"1.0\"?>\r\n" + 
						"<!DOCTYPE hibernate-mapping PUBLIC \r\n" + 
						"	\"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\r\n" + 
				"	\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\r\n");
				
				hbmWriter.println("<hibernate-mapping package=\"" + pkgName + "\">");
				
				javaWriter.println("public class " + name + " {");
				javaWriter.println(" static { System.out.println(\"" + name + " initialized!\"); }");
				hbmWriter.println("<class name=\"" + name + "\">");
				
				hbmWriter.println("<id type=\"long\"><generator class=\"assigned\"/></id>");
				for(int propCount=0;propCount<100;propCount++) {
					String propName = "Prop" + propCount;
					
					writeJavaProperty(javaWriter, propName);
					
					hbmWriter.println("<property name=\"" + propName + "\" type=\"string\"/>");
					
				}
				hbmWriter.println("</class>");
				javaWriter.println("}");
				hbmWriter.println("</hibernate-mapping>");
			} finally {
				if(javaWriter!=null) {
					javaWriter.flush();
					javaWriter.close();
				} 
				if(hbmWriter!=null) {
					hbmWriter.flush();
					hbmWriter.close();
				}
			}		
		}
		
	
public static voidmain(java.lang.String[] args)

		TestRunner.run( suite() );
	
private org.hibernate.SessionFactorysaveAndLoad(java.lang.String prefix, java.lang.String[] files, java.lang.String cfgName)

		long start = System.currentTimeMillis();
		
		Configuration cfg = new Configuration();
		System.err.println("Created configuration: " + (System.currentTimeMillis() - start) / 1000.0 + " sec.");
		
		System.err.println("saveAndLoad from " + prefix + " with cfg = " + cfgName);
		if(!new File(cfgName).exists()) {
			start = System.currentTimeMillis();
			/*for (int i=0; i<files.length; i++) {
			 if ( !files[i].startsWith("net/") ) files[i] = "test/org/hibernate/test/" + files[i];
			 cfg.addFile(files[i]);
			 //cfg.addLazyFile(files[i]);
			  }*/
			
			for (int i = 0; i < files.length; i++) {
				String file = files[i];
				cfg.addCacheableFile(new File(prefix + file));
			}
			
			System.err.println("Added " + (files.length) + " resources: " + (System.currentTimeMillis() - start) / 1000.0 + " sec.");
			
			ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(cfgName));
			os.writeObject(cfg); // need to serialize Configuration *before* building sf since it would require non-mappings and cfg types to be serializable
			os.flush();
			os.close();
			
		} else {
			start = System.currentTimeMillis();
			ObjectInputStream is = new ObjectInputStream(new FileInputStream(cfgName));
			cfg = (Configuration) is.readObject();
			is.close();
			System.err.println("Loaded serializable configuration:" + (System.currentTimeMillis() - start) / 1000.0 + " sec.");
		}
		start = System.currentTimeMillis();
		System.err.println("Start build of session factory");
		SessionFactory factory = cfg.buildSessionFactory();
		System.err.println("Build session factory:" + (System.currentTimeMillis() - start) / 1000.0 + " sec.");
		return factory;
	
public static junit.framework.Testsuite()

		return new TestSuite(ConfigurationPerformanceTest.class);
	
public voidtestLoadingAndSerializationOfConfiguration()


		String prefix = "./test/org/hibernate/test/";
		try {
			// first time
			System.err.println("###FIRST SAVELOAD###");
			saveAndLoad(prefix,files, "hibernate.cfg.bin");
			// second time to validate
			System.err.println("###SECOND SAVELOAD###");
			saveAndLoad(prefix,files, "hibernate.cfg.bin");
		}
		finally {
			System.err.println( "###CLEANING UP###" );
			if(!keepFilesAround) {
				File file = null;
				try {
				// clean up
					file = new File("hibernate.cfg.bin");
					file.delete();
				}
				catch( Throwable t ) {
					System.err.println( "Unable to cleanup [" + file + "] : " + t );
				}

				for (int i = 0; i < files.length; i++) {
					try {
						String fileName = files[i];
						file = new File( prefix, fileName + ".bin" );
						file.delete();
					}
					catch( Throwable t ) {
						System.err.println( "Unable to cleanup [" + file + "] : " + t );
					}
				}
			}
		}
		
	
private voidwriteJavaProperty(java.io.PrintWriter javaWriter, java.lang.String propName)

		javaWriter.println(" String " + propName + ";");
		javaWriter.println(" String get" + propName + "() { return " + propName + "; }");
		javaWriter.println(" void set" + propName + "(String newVal) { " + propName + "=newVal; }");
	
public voidxtestSessionFactoryCreationTime()

		File perfs = new File("perfsrc");
		generateTestFiles(perfs, "perftest");
		if(perfs.exists()) {
			SessionFactory factory = saveAndLoad("perfsrc/perftest/", new File(perfs, "perftest").list(new FilenameFilter() {
			
				public boolean accept(File dir, String name) {
					return name.endsWith(".hbm.xml");
				}
			
			}), "hibernateperftest.cfg.bin");
			
			Session session = factory.openSession();
			Object o = session.load("perftest.Test1", new Long(42));
			System.out.println(o);
		} else {
			System.err.println(perfs.getAbsoluteFile() + " not found");
		}