HHH-4569 - Split focus of ConfigurationPerformanceTest

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17964 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-11-12 19:46:04 +00:00
parent 655be65063
commit d558dac5dc
5 changed files with 258 additions and 186 deletions

View File

@ -424,70 +424,85 @@ public class Configuration implements Serializable {
* the non-cached file.
*/
public Configuration addCacheableFile(File xmlFile) throws MappingException {
File cachedFile = determineCachedDomFile( xmlFile );
try {
File cachedFile = new File( xmlFile.getAbsolutePath() + ".bin" );
org.dom4j.Document doc = null;
return addCacheableFileStrictly( xmlFile );
}
catch ( SerializationException e ) {
log.warn( "Could not deserialize cache file: " + cachedFile.getPath() + " : " + e );
}
catch ( FileNotFoundException e ) {
log.warn( "I/O reported cached file could not be found : " + cachedFile.getPath() + " : " + e );
}
final boolean useCachedFile = xmlFile.exists() &&
cachedFile.exists() &&
xmlFile.lastModified() < cachedFile.lastModified();
if ( !xmlFile.exists() ) {
throw new MappingNotFoundException( "file", xmlFile.toString() );
}
if ( useCachedFile ) {
try {
log.info( "Reading mappings from cache file: " + cachedFile );
doc = ( org.dom4j.Document ) SerializationHelper.deserialize( new FileInputStream( cachedFile ) );
}
catch ( SerializationException e ) {
log.warn( "Could not deserialize cache file: " + cachedFile.getPath(), e );
}
catch ( FileNotFoundException e ) {
log.warn( "I/O reported cached file could not be found : " + cachedFile.getPath(), e );
}
log.info( "Reading mappings from file: " + xmlFile );
List errors = new ArrayList();
try {
org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver ).read( xmlFile );
if ( errors.size() != 0 ) {
throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0 ) );
}
// if doc is null, then for whatever reason, the cached file cannot be used...
if ( doc == null ) {
if ( !xmlFile.exists() ) {
throw new MappingNotFoundException( "file", xmlFile.toString() );
}
log.info( "Reading mappings from file: " + xmlFile );
List errors = new ArrayList();
try {
doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver ).read( xmlFile );
if ( errors.size() != 0 ) {
throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0 ) );
}
}
catch( DocumentException e){
throw new MappingException( "invalid mapping", e );
}
try {
log.debug( "Writing cache file for: " + xmlFile + " to: " + cachedFile );
SerializationHelper.serialize( ( Serializable ) doc, new FileOutputStream( cachedFile ) );
}
catch ( SerializationException e ) {
log.warn( "Could not write cached file: " + cachedFile, e );
}
catch ( FileNotFoundException e ) {
log.warn( "I/O reported error writing cached file : " + cachedFile.getPath(), e );
}
try {
log.debug( "Writing cache file for: " + xmlFile + " to: " + cachedFile );
SerializationHelper.serialize( ( Serializable ) doc, new FileOutputStream( cachedFile ) );
}
catch ( SerializationException e ) {
log.warn( "Could not write cached file: " + cachedFile, e );
}
catch ( FileNotFoundException e ) {
log.warn( "I/O reported error writing cached file : " + cachedFile.getPath(), e );
}
add( doc );
return this;
}
catch( DocumentException e){
throw new MappingException( "invalid mapping", e );
}
return this;
}
private File determineCachedDomFile(File xmlFile) {
return new File( xmlFile.getAbsolutePath() + ".bin" );
}
/**
* <b>INTENDED FOR TESTSUITE USE ONLY!</b>
* <p/>
* Much like {@link addCacheableFile(File)} except that here we will fail immediately if
* the cache version cannot be found or used for whatever reason
*
* @param xmlFile The xml file, not the bin!
*
* @return The dom "deserialized" from the cached file.
*
* @throws MappingException Indicates a problem in the underlyiong call to {@link #add(org.dom4j.Document)}
* @throws SerializationException Indicates a problem deserializing the cached dom tree
* @throws FileNotFoundException Indicates that the cached file was not found or was not usable.
*/
public Configuration addCacheableFileStrictly(File xmlFile)
throws MappingException, SerializationException, FileNotFoundException {
final File cachedFile = determineCachedDomFile( xmlFile );
final boolean useCachedFile = xmlFile.exists()
&& cachedFile.exists()
&& xmlFile.lastModified() < cachedFile.lastModified();
if ( ! useCachedFile ) {
throw new FileNotFoundException( "Cached file could not be found or could not be used" );
}
catch ( InvalidMappingException e ) {
throw e;
}
catch ( MappingNotFoundException e ) {
throw e;
}
catch ( Exception e ) {
throw new InvalidMappingException( "file", xmlFile.toString(), e );
}
log.info( "Reading mappings from cache file: " + cachedFile );
org.dom4j.Document document =
( org.dom4j.Document ) SerializationHelper.deserialize( new FileInputStream( cachedFile ) );
add( document );
return this;
}
/**

View File

@ -1,20 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cfg;
import java.io.File;
import org.hibernate.cfg.Configuration;
import org.hibernate.junit.UnitTestCase;
import org.hibernate.util.SerializationHelper;
/**
* {@inheritDoc}
* Tests using of cacheable configuration files.
*
* @author Steve Ebersole
*/
public class CacheableFileTest extends UnitTestCase {
public static final String MAPPING = "org/hibernate/test/cfg/Cacheable.hbm.xml";
private File mappingFile;
private File mappingBinFile;
public CacheableFileTest(String string) {
super( string );
@ -24,21 +48,31 @@ public class CacheableFileTest extends UnitTestCase {
super.setUp();
mappingFile = new File( getClass().getClassLoader().getResource( MAPPING ).toURI() );
assertTrue( mappingFile.exists() );
File cached = new File( mappingFile.getParentFile(), mappingFile.getName() + ".bin" );
if ( cached.exists() ) {
cached.delete();
mappingBinFile = new File( mappingFile.getParentFile(), mappingFile.getName() + ".bin" );
if ( mappingBinFile.exists() ) {
//noinspection ResultOfMethodCallIgnored
mappingBinFile.delete();
}
}
protected void tearDown() throws Exception {
if ( mappingBinFile != null && mappingBinFile.exists() ) {
// be nice
//noinspection ResultOfMethodCallIgnored
mappingBinFile.delete();
}
mappingBinFile = null;
mappingFile = null;
super.tearDown();
}
public void testCachedFiles() {
Configuration cfg = new Configuration();
cfg.addCacheableFile( mappingFile );
Configuration cfg2 = new Configuration();
cfg2.addCacheableFile( mappingFile );
public void testCachedFiles() throws Exception {
assertFalse( mappingBinFile.exists() );
// This call should create the cached file
new Configuration().addCacheableFile( mappingFile );
assertTrue( mappingBinFile.exists() );
Configuration cfg = new Configuration().addCacheableFileStrictly( mappingFile );
SerializationHelper.clone( cfg );
}
}

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
* third-party contributors as indicated by either @author tags or express
* copyright attribution statements applied by the authors. All
* third-party contributions are distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA\
*/
package org.hibernate.test.cfg;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.junit.UnitTestCase;
import org.hibernate.cfg.Configuration;
import org.hibernate.util.SerializationHelper;
import org.hibernate.SessionFactory;
/**
* Copied over mostly from ConfigurationPerformanceTest
*
* @author Steve Ebersole
* @author Max Andersen
*/
public class ConfigurationSerializationTest extends UnitTestCase {
public ConfigurationSerializationTest(String string) {
super( string );
}
public static Test suite() {
return new TestSuite( ConfigurationSerializationTest.class );
}
private static final String[] FILES = new String[] {
"legacy/ABC.hbm.xml",
"legacy/ABCExtends.hbm.xml",
"legacy/Baz.hbm.xml",
"legacy/Blobber.hbm.xml",
"legacy/Broken.hbm.xml",
"legacy/Category.hbm.xml",
"legacy/Circular.hbm.xml",
"legacy/Commento.hbm.xml",
"legacy/ComponentNotNullMaster.hbm.xml",
"legacy/Componentizable.hbm.xml",
"legacy/Container.hbm.xml",
"legacy/Custom.hbm.xml",
"legacy/CustomSQL.hbm.xml",
"legacy/Eye.hbm.xml",
"legacy/Fee.hbm.xml",
"legacy/Fo.hbm.xml",
"legacy/FooBar.hbm.xml",
"legacy/Fum.hbm.xml",
"legacy/Fumm.hbm.xml",
"legacy/Glarch.hbm.xml",
"legacy/Holder.hbm.xml",
"legacy/IJ2.hbm.xml",
"legacy/Immutable.hbm.xml",
"legacy/Location.hbm.xml",
"legacy/Many.hbm.xml",
"legacy/Map.hbm.xml",
"legacy/Marelo.hbm.xml",
"legacy/MasterDetail.hbm.xml",
"legacy/Middle.hbm.xml",
"legacy/Multi.hbm.xml",
"legacy/MultiExtends.hbm.xml",
"legacy/Nameable.hbm.xml",
"legacy/One.hbm.xml",
"legacy/ParentChild.hbm.xml",
"legacy/Qux.hbm.xml",
"legacy/Simple.hbm.xml",
"legacy/SingleSeveral.hbm.xml",
"legacy/Stuff.hbm.xml",
"legacy/UpDown.hbm.xml",
"legacy/Vetoer.hbm.xml",
"legacy/WZ.hbm.xml",
};
public void testConfiguraionSerializability() {
Configuration cfg = new Configuration();
for ( String file : FILES ) {
cfg.addResource( "org/hibernate/test/" + file );
}
byte[] bytes = SerializationHelper.serialize( cfg );
cfg = ( Configuration ) SerializationHelper.deserialize( bytes );
// try to build SF
SessionFactory factory = cfg.buildSessionFactory();
factory.close();
}
}

View File

@ -1,3 +1,26 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cfg;
import java.util.Set;

View File

@ -1,29 +1,33 @@
/*
* Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, v. 2.1. This program is distributed in the
* hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. You should have received a
* copy of the GNU Lesser General Public License, v.2.1 along with this
* distribution; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* Lesser General Public License, as published by the Free Software Foundation.
*
* Red Hat Author(s): Max Andersen, Steve Ebersole
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cfg;
package org.hibernate.test.perf;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import junit.framework.Test;
@ -46,12 +50,10 @@ public class ConfigurationPerformanceTest extends UnitTestCase {
private final String workPackageName = "org.hibernate.test.cfg.work";
private File compilationBaseDir;
private File mappingBaseDir;
private File workPackageDir;
protected void setUp() throws Exception {
compilationBaseDir = getTestComplileDirectory();
mappingBaseDir = new File( compilationBaseDir, "org/hibernate/test" );
workPackageDir = new File( compilationBaseDir, workPackageName.replace( '.', '/' ) );
if ( workPackageDir.exists() ) {
//noinspection ResultOfMethodCallIgnored
@ -67,50 +69,6 @@ public class ConfigurationPerformanceTest extends UnitTestCase {
super.tearDown();
}
private static final String[] FILES = new String[] {
"legacy/ABC.hbm.xml",
"legacy/ABCExtends.hbm.xml",
"legacy/Baz.hbm.xml",
"legacy/Blobber.hbm.xml",
"legacy/Broken.hbm.xml",
"legacy/Category.hbm.xml",
"legacy/Circular.hbm.xml",
"legacy/Commento.hbm.xml",
"legacy/ComponentNotNullMaster.hbm.xml",
"legacy/Componentizable.hbm.xml",
"legacy/Container.hbm.xml",
"legacy/Custom.hbm.xml",
"legacy/CustomSQL.hbm.xml",
"legacy/Eye.hbm.xml",
"legacy/Fee.hbm.xml",
"legacy/Fo.hbm.xml",
"legacy/FooBar.hbm.xml",
"legacy/Fum.hbm.xml",
"legacy/Fumm.hbm.xml",
"legacy/Glarch.hbm.xml",
"legacy/Holder.hbm.xml",
"legacy/IJ2.hbm.xml",
"legacy/Immutable.hbm.xml",
"legacy/Location.hbm.xml",
"legacy/Many.hbm.xml",
"legacy/Map.hbm.xml",
"legacy/Marelo.hbm.xml",
"legacy/MasterDetail.hbm.xml",
"legacy/Middle.hbm.xml",
"legacy/Multi.hbm.xml",
"legacy/MultiExtends.hbm.xml",
"legacy/Nameable.hbm.xml",
"legacy/One.hbm.xml",
"legacy/ParentChild.hbm.xml",
"legacy/Qux.hbm.xml",
"legacy/Simple.hbm.xml",
"legacy/SingleSeveral.hbm.xml",
"legacy/Stuff.hbm.xml",
"legacy/UpDown.hbm.xml",
"legacy/Vetoer.hbm.xml",
"legacy/WZ.hbm.xml",
};
public ConfigurationPerformanceTest(String string) {
super( string );
}
@ -123,33 +81,6 @@ public class ConfigurationPerformanceTest extends UnitTestCase {
TestRunner.run( suite() );
}
public void testLoadingAndSerializationOfConfiguration() throws Throwable {
final File cachedCfgFile = new File( workPackageDir, "hibernate.cfg.bin" );
try {
System.err.println( "#### Preparing serialized configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
prepareSerializedConfiguration( mappingBaseDir, FILES, cachedCfgFile );
System.err.println( "#### Preparing serialized configuration complete ~~~~~~~~~~~~~~~~~~~~~~~~" );
// now make sure we can reload the serialized configuration...
System.err.println( "#### Reading serialized configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
readSerializedConfiguration( cachedCfgFile );
System.err.println( "#### Reading serialized configuration complete ~~~~~~~~~~~~~~~~~~~~~~~~~~" );
}
finally {
System.err.println( "###CLEANING UP###" );
if ( ! cachedCfgFile.delete() ) {
System.err.println( "Unable to cleanup file " + cachedCfgFile.getAbsolutePath() );
}
//noinspection ForLoopReplaceableByForEach
for ( int i = 0; i < FILES.length; i++ ) {
File file = new File( mappingBaseDir, FILES[i] + ".bin" );
if ( ! file.delete() ) {
System.err.println( "Unable to cleanup file " + file.getAbsolutePath() );
}
}
}
}
public void testSessionFactoryCreationTime() throws Throwable {
generateTestFiles();
if ( !workPackageDir.exists() ) {
@ -198,20 +129,6 @@ public class ConfigurationPerformanceTest extends UnitTestCase {
System.err.println( "Subsequent SessionFactory load time : " + subsequent );
}
private void prepareSerializedConfiguration(
File mappingFileBase,
String[] files,
File cachedCfgFile) throws IOException {
Configuration cfg = buildConfigurationFromCacheableFiles( mappingFileBase, files );
ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream( cachedCfgFile ) );
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();
timeBuildingSessionFactory( cfg );
}
private Configuration buildConfigurationFromCacheableFiles(File mappingFileBase, String[] files) {
long start = System.currentTimeMillis();
Configuration cfg = new Configuration();
@ -232,27 +149,6 @@ public class ConfigurationPerformanceTest extends UnitTestCase {
return cfg;
}
private void timeBuildingSessionFactory(Configuration configuration) {
long start = System.currentTimeMillis();
System.err.println( "Start build of session factory" );
SessionFactory factory = configuration.buildSessionFactory();
System.err.println( "Built session factory :" + ( System.currentTimeMillis() - start ) / 1000.0 + " sec." );
factory.close();
}
private void readSerializedConfiguration(File cachedCfgFile) throws ClassNotFoundException, IOException {
long start = System.currentTimeMillis();
ObjectInputStream is = new ObjectInputStream( new FileInputStream( cachedCfgFile ) );
Configuration cfg = ( Configuration ) is.readObject();
is.close();
System.err.println(
"Loaded serializable configuration :" +
( System.currentTimeMillis() - start ) / 1000.0 + " sec."
);
timeBuildingSessionFactory( cfg );
}
public void generateTestFiles() throws Throwable {
String filesToCompile = "";
for ( int count = 0; count < 100; count++ ) {