From: Bryan T. <tho...@us...> - 2006-03-09 18:37:14
|
Update of /cvsroot/cweb/extser/src/java/org/CognitiveWeb/extser/profiler In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22010/src/java/org/CognitiveWeb/extser/profiler Added Files: Profiler.java package.html Statistics.java VersionStatistics.java ClassStatistics.java Log Message: Initial import. The extensible serialization framework was also contributed to the jdbm project and exists under that license as well. --- NEW FILE: package.html --- <html> <body> <p>Profiler for the extensible serialization facility. This can be used to identify places where serialization is "fat", to report on the different serializer versions in use for a given class, etc. The profiler has minimal impact on serialization performance and reports automatically when it is finalized.</p> <dl> <dt><b>Version: </b></dt><dd>$Revision: 1.1 $ $Date: 2006/03/09 18:37:09 $</dd> <dt><b>Author: </b></dt><dd><a href="mailto:tho...@us...">Bryan Thompson</a></dd> </dl> </body> </html> --- NEW FILE: Statistics.java --- /** The Notice below must appear in each file of the Source Code of any copy you distribute of the Licensed Product. Contributors to any Modifications may add their own copyright notices to identify their own contributions. License: The contents of this file are subject to the CognitiveWeb Open Source License Version 1.0 (the License). You may not copy or use this file, in either source code or executable form, except in compliance with the License. You may obtain a copy of the License from http://www.CognitiveWeb.org/legal/license/ Software distributed under the License is distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyrights: Portions created by or assigned to CognitiveWeb, Inc. are Copyright (c) 2003-2003 CognitiveWeb, Inc. All Rights Reserved. Contact information for CognitiveWeb, Inc. is available at http://www.CognitiveWeb.org Portions Copyright (c) 2002-2003 Bryan Thompson. Acknowledgements: Special thanks to the developers of the Jabber Open Source License 1.0 (JOSL), from which this License was derived. This License contains terms that differ from JOSL. Special thanks to the CognitiveWeb Open Source Contributors for their suggestions and support of the Cognitive Web. Modifications: */ /* * Created on Nov 4, 2005 */ package org.CognitiveWeb.extser.profiler; import java.io.PrintStream; import org.CognitiveWeb.extser.AbstractExtensibleSerializer; /** * Abstract base class for collecting statistics on (de-)serialization. * * @author thompsonbry */ public abstract class Statistics { private final AbstractExtensibleSerializer serializer; public AbstractExtensibleSerializer getSerializer() { return serializer; } /** * @param serializer */ public Statistics(AbstractExtensibleSerializer serializer) { this.serializer = serializer; } public long nread; public long bytesRead; public long nwritten; public long bytesWritten; public void read( int nbytes ) { nread++; bytesRead += nbytes; } public void write( int nbytes ) { nwritten++; bytesWritten += nbytes; } /** * Write the statistics. * @param ps Where to write the statistics. */ public void writeOn( PrintStream ps ) { long avgPerRead = (nread == 0 ?0 :bytesRead/nread); long avgPerWrite = (nwritten == 0 ?0 :bytesWritten/nwritten); ps.println ( "read("+nread+","+bytesRead+","+avgPerRead+")"+ ", write("+nwritten+","+bytesWritten+","+avgPerWrite+")" ); } /** * Reset the statistics. */ public void reset() { nread = bytesRead = 0L; nwritten = bytesWritten = 0L; } } --- NEW FILE: ClassStatistics.java --- /** The Notice below must appear in each file of the Source Code of any copy you distribute of the Licensed Product. Contributors to any Modifications may add their own copyright notices to identify their own contributions. License: The contents of this file are subject to the CognitiveWeb Open Source License Version 1.0 (the License). You may not copy or use this file, in either source code or executable form, except in compliance with the License. You may obtain a copy of the License from http://www.CognitiveWeb.org/legal/license/ Software distributed under the License is distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyrights: Portions created by or assigned to CognitiveWeb, Inc. are Copyright (c) 2003-2003 CognitiveWeb, Inc. All Rights Reserved. Contact information for CognitiveWeb, Inc. is available at http://www.CognitiveWeb.org Portions Copyright (c) 2002-2003 Bryan Thompson. Acknowledgements: Special thanks to the developers of the Jabber Open Source License 1.0 (JOSL), from which this License was derived. This License contains terms that differ from JOSL. Special thanks to the CognitiveWeb Open Source Contributors for their suggestions and support of the Cognitive Web. Modifications: */ /* * Created on Nov 4, 2005 */ package org.CognitiveWeb.extser.profiler; import java.io.IOException; import java.io.PrintStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.CognitiveWeb.extser.AbstractExtensibleSerializer; import org.CognitiveWeb.extser.ISerializer; import org.CognitiveWeb.extser.NativeType; import org.CognitiveWeb.extser.Stateless; /** * Per class statistics. * * @author thompsonbry */ public class ClassStatistics extends Statistics { final public int classId; final public String classname; final public boolean stateless; // iff implements Stateless. public ClassStatistics( AbstractExtensibleSerializer serializer, int classId ) { super( serializer ); this.classId = classId; if( classId >= NativeType.NULL && classId <= NativeType.OBJECT_ARRAY ) { classname = NativeType.asString( classId ); stateless = false; } else { try { classname = getSerializer().getClassName( classId ); } catch( IOException ex ) { throw new RuntimeException( ex ); } try { Class cl = Class.forName( classname ); stateless = Stateless.class.isAssignableFrom( cl ); } catch( ClassNotFoundException ex ) { throw new RuntimeException( ex ); } } reset(); } public VersionStatistics get( short versionId, ISerializer serializer ) { Short versionId2 = new Short( versionId ); VersionStatistics stats = (VersionStatistics) versionStatistics.get( versionId2 ); if( stats == null ) { stats = new VersionStatistics( getSerializer(), versionId, serializer ); versionStatistics.put( versionId2, stats); } return stats; } /** * Members are {@link VersionStatistics}. */ public Map versionStatistics; public void reset() { super.reset(); versionStatistics = new HashMap(); } public void read( ISerializer ser, short version, int nbytes ) { super.read( nbytes ); get( version, ser ).read( nbytes ); } public void write( ISerializer ser, short version, int nbytes ) { super.write( nbytes ); get( version, ser ).write( nbytes ); } public void writeOn( PrintStream ps ) { long avgPerRead = (nread == 0 ?0 :bytesRead/nread); long avgPerWrite = (nwritten == 0 ?0 :bytesWritten/nwritten); ps.println ( "class="+classname+(stateless?"(Stateless)":"")+ ", classId="+classId+ ", read("+nread+","+bytesRead+","+avgPerRead+")"+ ", write("+nwritten+","+bytesWritten+","+avgPerWrite+")" ); Iterator itr = versionStatistics.values().iterator(); while( itr.hasNext() ) { VersionStatistics stats = (VersionStatistics) itr.next(); stats.writeOn( ps ); } } } --- NEW FILE: Profiler.java --- /** The Notice below must appear in each file of the Source Code of any copy you distribute of the Licensed Product. Contributors to any Modifications may add their own copyright notices to identify their own contributions. License: The contents of this file are subject to the CognitiveWeb Open Source License Version 1.0 (the License). You may not copy or use this file, in either source code or executable form, except in compliance with the License. You may obtain a copy of the License from http://www.CognitiveWeb.org/legal/license/ Software distributed under the License is distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyrights: Portions created by or assigned to CognitiveWeb, Inc. are Copyright (c) 2003-2003 CognitiveWeb, Inc. All Rights Reserved. Contact information for CognitiveWeb, Inc. is available at http://www.CognitiveWeb.org Portions Copyright (c) 2002-2003 Bryan Thompson. Acknowledgements: Special thanks to the developers of the Jabber Open Source License 1.0 (JOSL), from which this License was derived. This License contains terms that differ from JOSL. Special thanks to the CognitiveWeb Open Source Contributors for their suggestions and support of the Cognitive Web. Modifications: */ /* * Created on Nov 4, 2005 */ package org.CognitiveWeb.extser.profiler; import java.io.PrintStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.CognitiveWeb.extser.AbstractExtensibleSerializer; import org.CognitiveWeb.extser.ISerializer; import org.CognitiveWeb.extser.NativeType; /** * Collects statistics about serialization. You can use this to identify * objects for which serializers are not registered (they will show up as * <code>versionId == 0</code> and * <code>serializer == DefaultSerializer</code>, the versions of the * objects being read or written, etc. Objects wrapping Java primitives, * e.g., {@link Boolean},{@link Integer}, etc., as well as array types * will all have classId values less than {@link NativeType#FIRST_OBJECT_INDEX}. * <p> * * Note: Objects which correspond to compound records (comprised of other * objects) will be double-counted by the profiler. That is, the profiler * will report the serialized size of both the compound record and each * object within that compound record. * <p> * * The profiler is disabled by default. However it imposes a negligable * overhead. It may be enabled using a configuration option, in which case * the profiler will automatically report when it is finalized. Usually this * is not long after the recman is closed. * <p> * * @author thompsonbry */ public class Profiler extends Statistics { private final AbstractExtensibleSerializer serializer; private boolean enabled = false; /** * Enable or disable the profiler. It is disabled by default. * * @param val The new value. * * @return The old value. */ public boolean enable( boolean val ) { boolean tmp = enabled; enabled = val; return tmp; } /** * If the profiler is enabled and there has been at least one read or * write reported, then writes the statistics onto {@link System#err}. * While it is deprecated, you can use * {@link System#runFinalizersOnExit(boolean)} to trigger this * automatically. */ protected void finalize() throws Throwable { super.finalize(); if( enabled && (nread>0 || nwritten>0) ) { writeOn( System.err ); } } public Profiler(AbstractExtensibleSerializer serializer) { super( serializer ); reset(); this.serializer = serializer; } public boolean isEnabled() {return enabled;} /** * Members are {@link ClassStatistics}. */ private Map classStatistics; public ClassStatistics get( int classId ) { Integer classId2 = new Integer( classId ); ClassStatistics stats = (ClassStatistics) classStatistics.get( classId2 ); if( stats == null ) { stats = new ClassStatistics( getSerializer(), classId ); classStatistics.put( classId2, stats); } return stats; } synchronized public void serialized( ISerializer ser, int classId, short versionId, int nbytes ) { if( enabled ) { nread++; bytesRead += nbytes; get( classId ).read( ser, versionId, nbytes ); } } synchronized public void deserialized( ISerializer ser, int classId, short versionId, int nbytes ) { if( enabled ) { nwritten++; bytesWritten += nbytes; get( classId ).write( ser, versionId, nbytes ); } } /** * Write out the collected statistics. * * @param ps * * @todo Change to a tab delimited format so that you can analyze it in a * spreadsheet. */ public void writeOn( PrintStream ps ) { ps.println( "----- serialization statistics -----"); ps.println( "read(#read,#bytesRead,avgPerRead), write(#written,#bytesWritten,avgPerWrite)"); super.writeOn( ps ); Iterator itr = classStatistics.values().iterator(); while( itr.hasNext() ) { ClassStatistics stats = (ClassStatistics) itr.next(); stats.writeOn( ps ); } ps.println( "------------------------------------"); } public void reset() { super.reset(); classStatistics = new HashMap(); } } --- NEW FILE: VersionStatistics.java --- /** The Notice below must appear in each file of the Source Code of any copy you distribute of the Licensed Product. Contributors to any Modifications may add their own copyright notices to identify their own contributions. License: The contents of this file are subject to the CognitiveWeb Open Source License Version 1.0 (the License). You may not copy or use this file, in either source code or executable form, except in compliance with the License. You may obtain a copy of the License from http://www.CognitiveWeb.org/legal/license/ Software distributed under the License is distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyrights: Portions created by or assigned to CognitiveWeb, Inc. are Copyright (c) 2003-2003 CognitiveWeb, Inc. All Rights Reserved. Contact information for CognitiveWeb, Inc. is available at http://www.CognitiveWeb.org Portions Copyright (c) 2002-2003 Bryan Thompson. Acknowledgements: Special thanks to the developers of the Jabber Open Source License 1.0 (JOSL), from which this License was derived. This License contains terms that differ from JOSL. Special thanks to the CognitiveWeb Open Source Contributors for their suggestions and support of the Cognitive Web. Modifications: */ /* * Created on Nov 4, 2005 */ package org.CognitiveWeb.extser.profiler; import java.io.PrintStream; import org.CognitiveWeb.extser.AbstractExtensibleSerializer; import org.CognitiveWeb.extser.ISerializer; /** * Per version statistics (collected on a per class basis). * * @author thompsonbry */ public class VersionStatistics extends Statistics { final public short versionId; final public ISerializer serializer; public VersionStatistics( AbstractExtensibleSerializer serializer, short versionId, ISerializer versionSerializer ) { super( serializer ); this.versionId = versionId; this.serializer = versionSerializer; } public void writeOn( PrintStream ps ) { long avgPerRead = (nread == 0 ?0 :bytesRead/nread); long avgPerWrite = (nwritten == 0 ?0 :bytesWritten/nwritten); ps.println ( " versionId="+versionId+ ", class="+serializer.getClass().getName()+ ", read("+nread+","+bytesRead+","+avgPerRead+")"+ ", write("+nwritten+","+bytesWritten+","+avgPerWrite+")" ); } } |