Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 31370 invoked by uid 500); 5 Jul 2001 15:03:29 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 31358 invoked by uid 500); 5 Jul 2001 15:03:29 -0000 Delivered-To: apmail-xml-cocoon2-cvs@apache.org Date: 5 Jul 2001 15:03:28 -0000 Message-ID: <20010705150328.31354.qmail@apache.org> From: vgritsenko@apache.org To: xml-cocoon2-cvs@apache.org Subject: cvs commit: xml-cocoon2/src/org/apache/cocoon/components/profiler Profiler.java ProfilerData.java ProfilerGenerator.java ProfilerResult.java ProfilingCachingEventPipeline.java ProfilingNonCachingEventPipeline.java ProfilingSAXConnector.java SimpleProfiler.java vgritsenko 01/07/05 08:03:28 Added: src/org/apache/cocoon/components/profiler Profiler.java ProfilerData.java ProfilerGenerator.java ProfilerResult.java ProfilingCachingEventPipeline.java ProfilingNonCachingEventPipeline.java ProfilingSAXConnector.java SimpleProfiler.java Log: First cut to implement profiler. A lot more must be done... Revision Changes Path 1.1 xml-cocoon2/src/org/apache/cocoon/components/profiler/Profiler.java Index: Profiler.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.profiler; import java.util.Collection; import org.xml.sax.ContentHandler; import org.apache.avalon.framework.component.Component; import org.apache.cocoon.environment.Request; /** * Profiler component interface. * * @author Vadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public interface Profiler extends Component { public void clearResults(); public void clearResult(Object key); public void addResult(String uri, ProfilerData data); public Collection getResultKeys(); public Collection getResults(); public ProfilerResult getResult(Object key); } 1.1 xml-cocoon2/src/org/apache/cocoon/components/profiler/ProfilerData.java Index: ProfilerData.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.profiler; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.Environment; import org.apache.cocoon.util.HashUtil; /** * Request-time profiler information. * * @author Vadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class ProfilerData { class Entry { Object component; String role; String source; long time; Entry(Object c, String r, String s) { component = c; role = r; source = s; } } private ArrayList a = null; public ProfilerData() { a = new ArrayList(); a.add(null); } public void setGenerator(Object generator, String role, String source) { a.set(0, new Entry(generator, role, source)); } public void addComponent(Object component, String role, String source) { a.add(new Entry(component, role, source)); } public int indexOf(Object component) { for(int i=0; iVadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class ProfilerGenerator extends ComposerGenerator implements Recyclable, Composable, Disposable { /** * The XML namespace for the output document. */ protected static final String namespace = "http://apache.org/cocoon/profiler/1.0"; private Profiler profiler; public void compose(ComponentManager manager) throws ComponentException { super.compose(manager); try{ this.profiler = (Profiler)super.manager.lookup("org.apache.cocoon.components.profiler.Profiler"); }catch(Exception e){ e.printStackTrace(); } } public void dispose() { if(this.profiler != null){ super.manager.release(this.profiler); this.profiler = null; } } /** * Generate the status information in XML format. * @throws SAXException * when there is a problem creating the output SAX events. */ public void generate() throws SAXException { // Start the document and set the namespace. this.contentHandler.startDocument(); this.contentHandler.startPrefixMapping("", namespace); generate(this.contentHandler); // End the document. this.contentHandler.endPrefixMapping(""); this.contentHandler.endDocument(); } /** Generate the main status document. */ private void generate(ContentHandler ch) throws SAXException { // Root element. // The current date and time. String dateTime = DateFormat.getDateTimeInstance().format(new Date()); AttributesImpl atts = new AttributesImpl(); atts.addAttribute(namespace, "date", "date", "CDATA", dateTime); ch.startElement(namespace, "profilerinfo", "profilerinfo", atts); Collection results = profiler.getResults(); for(Iterator i = results.iterator(); i.hasNext();) generateResult(ch, (ProfilerResult)i.next()); // End root element. ch.endElement(namespace, "profilerinfo", "profilerinfo"); } private void generateResult(ContentHandler ch, ProfilerResult result) throws SAXException { AttributesImpl atts = new AttributesImpl(); int count = result.getCount(); String[] roles = result.getRoles(); String[] source = result.getSources(); long[] avgTime = result.getTotalTime(); long avgTotal = 0; for(int i=0; i < avgTime.length; i++) avgTotal += avgTime[i]; atts.addAttribute(namespace, "uri", "uri", "CDATA", result.getURI()); atts.addAttribute(namespace, "count", "count", "CDATA", Integer.toString(result.getCount())); atts.addAttribute(namespace, "time", "time", "CDATA", Long.toString(avgTotal)); ch.startElement(namespace, "pipeline", "pipeline", atts); atts.clear(); if(count > 0){ atts.addAttribute(namespace, "time", "time", "CDATA", Long.toString(avgTotal / count)); ch.startElement(namespace, "average", "average", atts); atts.clear(); for(int i=0; iVadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class ProfilerResult { private String uri; private String[] roles; private String[] sources; private int count; private long[] totalTime; private long[][] latestTimes; private int latestCurrent; public ProfilerResult(String uri, int latestResultsCount) { this.uri = uri; if(latestResultsCount > 0) this.latestTimes = new long[latestResultsCount][]; } public void addData(ProfilerData data) { ProfilerData.Entry[] entries = data.getEntries(); synchronized(this){ if(totalTime == null || totalTime.length != entries.length){ // Reinitialize arrays totalTime = new long[entries.length]; roles = new String[entries.length]; sources = new String[entries.length]; for(int i=0; i= latestTimes.length) latestCurrent = 0; } count++; } } public String getURI() { return uri; } public String[] getRoles() { return roles; } public String[] getSources() { return sources; } public int getCount() { return count; } public long[] getTotalTime() { return totalTime; } public long[][] getLastTimes() { return latestTimes; } /* public void report() { System.err.println("-------------------------------------------------------------------------------"); System.err.println("PROFILER TOTALS FOR: " + uri); if(totalTime != null){ long time = 0; for(int i=0; iVadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class ProfilingCachingEventPipeline extends CachingEventPipeline implements Recyclable, Composable, Disposable { private ComponentManager manager; private Profiler profiler; private ProfilerData data = null; public void compose(ComponentManager manager) throws ComponentException { super.compose(manager); try{ this.manager = manager; this.profiler = (Profiler)manager.lookup("org.apache.cocoon.components.profiler.Profiler"); }catch(Exception e){ e.printStackTrace(); } } public void dispose() { super.dispose(); if(this.profiler != null){ this.manager.release(this.profiler); this.profiler = null; } this.manager = null; } public void setGenerator (String role, String source, Parameters param, Exception e) throws Exception { super.setGenerator(role, source, param, e); if(this.data == null) this.data = new ProfilerData(); this.data.setGenerator(super.generator, role, source); } public void setGenerator (String role, String source, Parameters param) throws Exception { super.setGenerator(role, source, param); if(this.data == null) this.data = new ProfilerData(); this.data.setGenerator(super.generator, role, source); } public void addTransformer (String role, String source, Parameters param) throws Exception { super.addTransformer(role, source, param); if(this.data == null) this.data = new ProfilerData(); this.data.addComponent(super.transformers.get(super.transformers.size()-1), role, source); } public boolean process(Environment environment) throws Exception { this.data.addComponent(super.xmlConsumer, null, null); environment.getObjectModel().put("profiler", data); // Execute pipeline long time = System.currentTimeMillis(); boolean result = super.process(environment); this.data.setTime(0, System.currentTimeMillis() - time); // Report profiler.addResult(environment.getURI(), data); return result; } public void recycle() { this.data = null; super.recycle(); } } 1.1 xml-cocoon2/src/org/apache/cocoon/components/profiler/ProfilingNonCachingEventPipeline.java Index: ProfilingNonCachingEventPipeline.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.profiler; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import org.apache.avalon.framework.component.Component; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.excalibur.pool.Recyclable; import org.apache.cocoon.environment.Environment; import org.apache.cocoon.generation.Generator; import org.apache.cocoon.sitemap.ErrorNotifier; import org.apache.cocoon.transformation.Transformer; import org.apache.cocoon.xml.AbstractXMLProducer; import org.apache.cocoon.xml.XMLConsumer; import org.apache.cocoon.xml.XMLProducer; import org.apache.cocoon.components.pipeline.NonCachingEventPipeline; import org.xml.sax.SAXException; /** * @author Vadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class ProfilingNonCachingEventPipeline extends NonCachingEventPipeline implements Recyclable, Composable, Disposable { private ComponentManager manager; private Profiler profiler; private ProfilerData data = null; public void compose(ComponentManager manager) throws ComponentException { super.compose(manager); try{ this.manager = manager; this.profiler = (Profiler)manager.lookup("org.apache.cocoon.components.profiler.Profiler"); }catch(Exception e){ e.printStackTrace(); } } public void dispose() { super.dispose(); if(this.profiler != null){ this.manager.release(this.profiler); this.profiler = null; } this.manager = null; } public void setGenerator (String role, String source, Parameters param, Exception e) throws Exception { super.setGenerator(role, source, param, e); if(this.data == null) this.data = new ProfilerData(); this.data.setGenerator(super.generator, role, source); } public void setGenerator (String role, String source, Parameters param) throws Exception { super.setGenerator(role, source, param); if(this.data == null) this.data = new ProfilerData(); this.data.setGenerator(super.generator, role, source); } public void addTransformer (String role, String source, Parameters param) throws Exception { super.addTransformer(role, source, param); if(this.data == null) this.data = new ProfilerData(); this.data.addComponent(super.transformers.get(super.transformers.size()-1), role, source); } public boolean process(Environment environment) throws Exception { this.data.addComponent(super.xmlConsumer, null, null); environment.getObjectModel().put("profiler", data); // Execute pipeline long time = System.currentTimeMillis(); boolean result = super.process(environment); this.data.setTime(0, System.currentTimeMillis() - time); // Report profiler.addResult(environment.getURI(), data); return result; } public void recycle() { this.data = null; super.recycle(); } } 1.1 xml-cocoon2/src/org/apache/cocoon/components/profiler/ProfilingSAXConnector.java Index: ProfilingSAXConnector.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.profiler; import java.io.IOException; import java.util.Map; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.excalibur.pool.Recyclable; import org.apache.cocoon.Constants; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.environment.Request; import org.apache.cocoon.sitemap.Sitemap; import org.apache.cocoon.xml.AbstractXMLPipe; import org.apache.cocoon.xml.XMLConsumer; import org.apache.cocoon.serialization.Serializer; import org.apache.cocoon.components.saxconnector.SAXConnector; /** * This SAX connector measures time taken by the following SAX handler. * @author Vadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class ProfilingSAXConnector extends AbstractXMLPipe implements Recyclable, SAXConnector { private ProfilerData data; private int index; private long time; private long total; /** * Setup this SAXConnector. */ public void setup(SourceResolver resolver, Map objectModel, String src, Parameters params) throws ProcessingException, SAXException, IOException { this.data = (ProfilerData)objectModel.get("profiler"); } /** * Recycle the SAXConnector */ public void recycle() { this.data = null; this.time = this.total = 0; this.index = -1; super.recycle(); } public void setConsumer(XMLConsumer consumer) { super.setConsumer(consumer); if(this.data != null) this.index = this.data.indexOf(consumer); else this.index = -1; } public void startDocument() throws SAXException { time = System.currentTimeMillis(); super.startDocument(); total += System.currentTimeMillis() - time; } public void endDocument() throws SAXException { time = System.currentTimeMillis(); super.endDocument(); total += System.currentTimeMillis() - time; if(index != -1) data.setTime(index, total); } public void startPrefixMapping(String prefix, String uri) throws SAXException { time = System.currentTimeMillis(); super.startPrefixMapping(prefix, uri); total += System.currentTimeMillis() - time; } public void endPrefixMapping(String prefix) throws SAXException { time = System.currentTimeMillis(); super.endPrefixMapping(prefix); total += System.currentTimeMillis() - time; } public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { time = System.currentTimeMillis(); super.startElement(uri, loc, raw, a); total += System.currentTimeMillis() - time; } public void endElement(String uri, String loc, String raw) throws SAXException { time = System.currentTimeMillis(); super.endElement(uri, loc, raw); total += System.currentTimeMillis() - time; } public void characters(char c[], int start, int len) throws SAXException { time = System.currentTimeMillis(); super.characters(c, start, len); total += System.currentTimeMillis() - time; } public void ignorableWhitespace(char c[], int start, int len) throws SAXException { time = System.currentTimeMillis(); super.ignorableWhitespace(c, start, len); total += System.currentTimeMillis() - time; } public void processingInstruction(String target, String data) throws SAXException { time = System.currentTimeMillis(); super.processingInstruction(target, data); total += System.currentTimeMillis() - time; } public void skippedEntity(String name) throws SAXException { time = System.currentTimeMillis(); super.skippedEntity(name); total += System.currentTimeMillis() - time; } public void startDTD(String name, String publicId, String systemId) throws SAXException { time = System.currentTimeMillis(); super.startDTD(name, publicId, systemId); total += System.currentTimeMillis() - time; } public void endDTD() throws SAXException { time = System.currentTimeMillis(); super.endDTD(); total += System.currentTimeMillis() - time; } public void startEntity(String name) throws SAXException { time = System.currentTimeMillis(); super.startEntity(name); total += System.currentTimeMillis() - time; } public void endEntity(String name) throws SAXException { time = System.currentTimeMillis(); super.endEntity(name); total += System.currentTimeMillis() - time; } public void startCDATA() throws SAXException { time = System.currentTimeMillis(); super.startCDATA(); total += System.currentTimeMillis() - time; } public void endCDATA() throws SAXException { time = System.currentTimeMillis(); super.endCDATA(); total += System.currentTimeMillis() - time; } public void comment(char ch[], int start, int len) throws SAXException { time = System.currentTimeMillis(); super.comment(ch, start, len); total += System.currentTimeMillis() - time; } } 1.1 xml-cocoon2/src/org/apache/cocoon/components/profiler/SimpleProfiler.java Index: SimpleProfiler.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.profiler; import java.util.Collection; import java.util.Map; import java.util.HashMap; import org.xml.sax.ContentHandler; import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.cocoon.environment.Request; import org.apache.cocoon.serialization.Serializer; /** * Profiler component implementation. Stores profiler data for * all pipelines. * * @author Vadim Gritsenko * @version CVS $Revision: 1.1 $ $Date: 2001/07/05 15:03:28 $ */ public class SimpleProfiler extends AbstractLoggable implements Profiler, ThreadSafe //, Composable, Configurable, Disposable { private static final int RESULTS_COUNT = 10; private Map results; public SimpleProfiler() { results = new HashMap(); } public void clearResults() { results.clear(); } public void clearResult(Object key) { results.remove(key); } public Collection getResultKeys() { return results.keySet(); } public Collection getResults() { return results.values(); } public ProfilerResult getResult(Object key) { return (ProfilerResult)results.get(key); } public void addResult(String uri, ProfilerData data) { Long key = new Long(data.getKey(uri)); ProfilerResult result = (ProfilerResult)results.get(key); if(result == null){ synchronized(results){ if((result = (ProfilerResult)results.get(key)) == null) results.put(key, result = new ProfilerResult(uri, RESULTS_COUNT)); } } result.addData(data); // result.report(); } } ---------------------------------------------------------------------- In case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org For additional commands, e-mail: cocoon-cvs-help@xml.apache.org