stephan     2002/09/03 00:46:14

  Modified:    src/java/org/apache/cocoon/generation ProfilerGenerator.java
  Log:
  Extend the profiler components to store intermediate SAX
  fragments, and also the some informations about the
  environment, thanks to Bruno Dumon.
  
  Revision  Changes    Path
  1.2       +237 -50   
xml-cocoon2/src/java/org/apache/cocoon/generation/ProfilerGenerator.java
  
  Index: ProfilerGenerator.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/generation/ProfilerGenerator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ProfilerGenerator.java    21 Aug 2002 06:15:31 -0000      1.1
  +++ ProfilerGenerator.java    3 Sep 2002 07:46:13 -0000       1.2
  @@ -52,35 +52,70 @@
   
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
  +import org.apache.avalon.framework.parameters.Parameters;
  +import org.apache.avalon.framework.parameters.ParameterException;
   
  +import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.components.profiler.EnvironmentInfo;
   import org.apache.cocoon.components.profiler.Profiler;
   import org.apache.cocoon.components.profiler.ProfilerResult;
  +import org.apache.cocoon.components.sax.XMLDeserializer;
  +import org.apache.cocoon.environment.SourceResolver;
  +import org.apache.cocoon.environment.Request;
  +import org.apache.cocoon.environment.ObjectModelHelper;
  +import org.apache.cocoon.xml.IncludeXMLConsumer;
   
   import org.xml.sax.ContentHandler;
   import org.xml.sax.SAXException;
   import org.xml.sax.helpers.AttributesImpl;
   
  +import java.io.IOException;
   import java.text.DateFormat;
   import java.util.Collection;
   import java.util.Date;
   import java.util.Iterator;
  +import java.util.Map;
  +import java.util.Set;
   
   /**
    * Generates an XML representation of the current status of Profiler.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vadim Gritsenko</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Bruno Dumon</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephan Michels</a>
    * @version CVS $Id$
    */
   public class ProfilerGenerator extends ComposerGenerator {
   
       /**
  -     * The XML namespace for the output document.
  +     * The XML PROFILER_NS for the output document.
        */
  -    protected static final String namespace = 
"http://apache.org/cocoon/profiler/1.0";;
  +    protected static final String PROFILER_NS = 
"http://apache.org/cocoon/profiler/1.0";;
  +
  +    protected static final String PROFILERINFO_ELEMENT = "profilerinfo";
  +    protected static final String RESULTS_ELEMENT = "pipeline";
  +    protected static final String RESULT_ELEMENT = "result";
  +    protected static final String AVERAGERESULT_ELEMENT = "average";
  +    protected static final String ENVIROMENTINFO_ELEMENT = "environmentinfo";
  +    protected static final String REQUESTPARAMETERS_ELEMENT = "request-parameters";
  +    protected static final String REQUESTPARAMETER_ELEMENT = "parameter";
  +    protected static final String SESSIONATTRIBUTES_ELEMENT = "session-attributes";
  +    protected static final String SESSIONATTRIBUTE_ELEMENT = "attribute";
  +    protected static final String COMPONENT_ELEMENT = "component";
  +    protected static final String FRAGMENT_ELEMENT = "fragment";
  +    
   
       private Profiler profiler;
   
  +    // the key identifying the ProfilerResult
  +    protected Long key = null;
  +
  +    // Index of the result of latest results 
  +    protected int resultIndex = -1;
  +
  +    // Index of the componen of the latest results
  +    protected int componentIndex = -1;
  +
       /**
        * Composable
        */
  @@ -90,6 +125,28 @@
           this.profiler = (Profiler)super.manager.lookup(Profiler.ROLE);
       }
   
  +    public void setup(SourceResolver resolver, Map objectModel, String soure, 
Parameters parameters) 
  +        throws ProcessingException, SAXException, IOException {
  +
  +        super.setup(resolver, objectModel, source, parameters);
  +        Request request = ObjectModelHelper.getRequest(objectModel);
  +
  +        if (request.getParameter("key")!=null)
  +            this.key = new Long(Long.parseLong(request.getParameter("key")));
  +        else
  +            this.key = null;
  +
  +        if ((request.getParameter("result")!=null) && (this.key!=null))
  +            this.resultIndex = Integer.parseInt(request.getParameter("result"));
  +        else
  +            this.resultIndex = -1;
  +
  +        if ((request.getParameter("component")!=null) && (this.resultIndex!=-1))
  +            this.componentIndex = 
Integer.parseInt(request.getParameter("component"));
  +        else
  +            this.componentIndex = -1;
  +    }
  +
       /**
        * Disposable
        */
  @@ -108,11 +165,11 @@
        */
       public void generate() throws SAXException {
   
  -        // Start the document and set the namespace.
  +        // Start the document and set the PROFILER_NS.
           this.contentHandler.startDocument();
  -        this.contentHandler.startPrefixMapping("", namespace);
  +        this.contentHandler.startPrefixMapping("", PROFILER_NS);
   
  -        generate(this.contentHandler);
  +        generateProfilerInfo();
   
           // End the document.
           this.contentHandler.endPrefixMapping("");
  @@ -120,86 +177,216 @@
       }
   
       /** Generate the main status document. */
  -    private void generate(ContentHandler ch) throws SAXException {
  +    private void generateProfilerInfo() 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);
  +        atts.addAttribute(PROFILER_NS, "date", "date", "CDATA", dateTime);
  +        this.contentHandler.startElement(PROFILER_NS, PROFILERINFO_ELEMENT, 
PROFILERINFO_ELEMENT, atts);
   
  -        Collection results = profiler.getResults();
  -        for(Iterator i = results.iterator(); i.hasNext();)
  -            generateResult(ch, (ProfilerResult)i.next());
  +        Collection resultsKeys = profiler.getResultKeys();
  +        for(Iterator i = resultsKeys.iterator(); i.hasNext();) {
  +            Long key = (Long)i.next();
  +            if ((this.key==null) || (this.key.equals(key)))
  +                generateResults(key, profiler.getResult(key));
  +        }
   
           // End root element.
  -        ch.endElement(namespace, "profilerinfo", "profilerinfo");
  +        this.contentHandler.endElement(PROFILER_NS, PROFILERINFO_ELEMENT, 
PROFILERINFO_ELEMENT);
       }
   
  -    private void generateResult(ContentHandler ch, ProfilerResult result) throws 
SAXException {
  +    private void generateResults(Long key, ProfilerResult result) throws 
SAXException {
           AttributesImpl atts = new AttributesImpl();
   
           int count = result.getCount();
           String[] roles = result.getRoles(); // Roles of the components
  -        String[] source = result.getSources(); // Source of the components
  +        String[] sources = result.getSources(); // Source of the components
  +        
  +        EnvironmentInfo[] environmentInfos = result.getLatestEnvironmentInfos();
           long[] totalTime = result.getTotalTime(); // Total time of the requests
  -        long totalTimeSum = 0; // Total time of all requests
  +        long[][] timeOfComponents = result.getLastTimes(); // Time of each component
  +        Object[][] fragments = result.getLatestSAXFragments(); // SAX Fragments of 
each component
  +
  +        // Total time of all requests
  +        long totalTimeSum = 0; 
           for(int i=0; i < count; i++)
               totalTimeSum += totalTime[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(totalTimeSum));
  -        ch.startElement(namespace, "pipeline", "pipeline", atts);
  +        atts.addAttribute(PROFILER_NS, "uri", "uri", "CDATA", result.getURI());
  +        atts.addAttribute(PROFILER_NS, "count", "count", "CDATA", 
Integer.toString(result.getCount()));
  +        atts.addAttribute(PROFILER_NS, "time", "time", "CDATA", 
Long.toString(totalTimeSum));
  +        atts.addAttribute(PROFILER_NS, "key", "key", "CDATA", key.toString());
  +        this.contentHandler.startElement(PROFILER_NS, RESULTS_ELEMENT, 
RESULTS_ELEMENT, atts);
           atts.clear();
   
  -        if(count > 0){
  -            atts.addAttribute(namespace, "time", "time", "CDATA", 
Long.toString(totalTimeSum / count));
  -            ch.startElement(namespace, "average", "average", atts);
  +        // Generate average result 
  +        if ((count > 0) && (this.resultIndex==-1)) {
  +            atts.addAttribute(PROFILER_NS, "time", "time", "CDATA", 
Long.toString(totalTimeSum / count));
  +            this.contentHandler.startElement(PROFILER_NS, AVERAGERESULT_ELEMENT, 
AVERAGERESULT_ELEMENT, atts);
               atts.clear();
   
  -            long[][] timeOfComponents = result.getLastTimes(); // Time of each 
component
  -            long[] totalTimeOfComponents = new long[roles.length]; // Time of each 
component for all requests
  +            // Total time of each component for all requests
  +            long[] totalTimeOfComponents = new long[roles.length]; 
             
  -            for(int j=0; j<count; j++){
  -                for(int i=0; i<roles.length; i++){
  +            for(int i=0; i<roles.length; i++) {
  +                totalTimeOfComponents[i] = 0;
  +                for(int j=0; j<count; j++) {
                       totalTimeOfComponents[i] += timeOfComponents[j][i];
                   }
               }
   
               for(int i=0; i<roles.length; i++){
  +                atts.addAttribute(PROFILER_NS, "offset", "offset", "CDATA", 
String.valueOf(i));
  +
                   if(roles[i] != null)
  -                    atts.addAttribute(namespace, "role", "role", "CDATA", roles[i]);
  -                if(source[i] != null)
  -                    atts.addAttribute(namespace, "source", "source", "CDATA", 
source[i]);
  -                atts.addAttribute(namespace, "time", "time", "CDATA", 
Long.toString(totalTimeOfComponents[i] / count));
  -                ch.startElement(namespace, "element", "element", atts);
  +                    atts.addAttribute(PROFILER_NS, "role", "role", "CDATA", 
roles[i]);
  +
  +                if(sources[i] != null)
  +                    atts.addAttribute(PROFILER_NS, "source", "source", "CDATA", 
sources[i]);
  +
  +                atts.addAttribute(PROFILER_NS, "time", "time", "CDATA", 
  +                                  Long.toString(totalTimeOfComponents[i] / count));
  +
  +                this.contentHandler.startElement(PROFILER_NS, COMPONENT_ELEMENT, 
COMPONENT_ELEMENT, atts);
                   atts.clear();
  -                ch.endElement(namespace, "element", "element");
  +                this.contentHandler.endElement(PROFILER_NS, COMPONENT_ELEMENT, 
COMPONENT_ELEMENT);
               }
  -            ch.endElement(namespace, "average", "average");
  +            this.contentHandler.endElement(PROFILER_NS, AVERAGERESULT_ELEMENT, 
AVERAGERESULT_ELEMENT);
  +        }
   
  -            for(int j=0; j<count; j++){
  -                atts.addAttribute(namespace, "time", "time", "CDATA", 
Long.toString(totalTime[j]));
  -                ch.startElement(namespace, "result", "result", atts);
  -                atts.clear();
  -                for(int i=0; i<roles.length; i++){
  -                    if(roles[i] != null)
  -                        atts.addAttribute(namespace, "role", "role", "CDATA", 
roles[i]);
  -                    if(source[i] != null)
  -                        atts.addAttribute(namespace, "source", "source", "CDATA", 
source[i]);
  -                    atts.addAttribute(namespace, "time", "time", "CDATA", 
Long.toString(timeOfComponents[j][i]));
  -                    ch.startElement(namespace, "element", "element", atts);
  -                    atts.clear();
  -                    ch.endElement(namespace, "element", "element");
  -                }
  -                ch.endElement(namespace, "result", "result");
  +        for(int j=0; j<count; j++) {
  +            if ((this.resultIndex==-1) || (this.resultIndex==j))
  +                generateResult(j, roles, sources, environmentInfos[j], 
  +                               totalTime[j], timeOfComponents[j], fragments[j]);
  +        }
  +
  +        this.contentHandler.endElement(PROFILER_NS, RESULTS_ELEMENT, 
RESULTS_ELEMENT);
  +    }
  +
  +    private void generateResult(int resultIndex, String[] roles, String[] sources, 
  +                                EnvironmentInfo environmentInfo,
  +                                long totaltime, long[] times, Object[] fragments) 
throws SAXException  {
  + 
  +        AttributesImpl atts = new AttributesImpl();
  +        atts.addAttribute(PROFILER_NS, "time", "time", "CDATA", 
Long.toString(totaltime));
  +        atts.addAttribute(PROFILER_NS, "index", "index", "CDATA", 
String.valueOf(resultIndex));
  +        this.contentHandler.startElement(PROFILER_NS, RESULT_ELEMENT, 
RESULT_ELEMENT, atts);
  +        atts.clear();
  +
  +        if (this.resultIndex!=-1)
  +            generateEnvironmentInfo(environmentInfo);
  +
  +        for(int i=0; i<roles.length; i++) {
  +            generateComponent(i, roles[i], sources[i], times[i], fragments[i]);
  +        }
  +        this.contentHandler.endElement(PROFILER_NS, RESULT_ELEMENT, RESULT_ELEMENT);
  +    }
  +
  +    private void generateComponent(int componentIndex, String role, String source, 
long time, Object fragment) 
  +        throws SAXException  {
  +
  +        AttributesImpl atts = new AttributesImpl();
  +
  +        atts.addAttribute(PROFILER_NS, "index", "index", "CDATA", 
String.valueOf(componentIndex));
  +
  +        if (role != null)
  +            atts.addAttribute(PROFILER_NS, "role", "role", "CDATA", role);
  +
  +        if (source != null)
  +            atts.addAttribute(PROFILER_NS, "source", "source", "CDATA", source);
  +
  +        atts.addAttribute(PROFILER_NS, "time", "time", "CDATA", 
Long.toString(time));
  +
  +        this.contentHandler.startElement(PROFILER_NS, COMPONENT_ELEMENT, 
COMPONENT_ELEMENT, atts);
  +        atts.clear();
  +
  +        if (this.componentIndex==componentIndex)
  +            generateSAXFragment(fragment);
  +
  +        this.contentHandler.endElement(PROFILER_NS, COMPONENT_ELEMENT, 
COMPONENT_ELEMENT);
  +    }
  +
  +    private void generateEnvironmentInfo(EnvironmentInfo environmentInfo) throws 
SAXException  {
  +        this.contentHandler.startElement(PROFILER_NS, ENVIROMENTINFO_ELEMENT, 
ENVIROMENTINFO_ELEMENT, 
  +                                         new AttributesImpl());
  +
  +        if (environmentInfo != null) {
  +            // Generate SAX events for the request parameters
  +            this.contentHandler.startElement(PROFILER_NS, 
REQUESTPARAMETERS_ELEMENT, REQUESTPARAMETERS_ELEMENT, 
  +                                        new AttributesImpl());
  +
  +            Map requestParameters = environmentInfo.getRequestParameters();
  +            Set requestParamEntries = requestParameters.entrySet();
  +            Iterator requestParamEntriesIt = requestParamEntries.iterator();
  +            while (requestParamEntriesIt.hasNext()) {
  +                AttributesImpl atts = new AttributesImpl();
  +                Map.Entry entry = (Map.Entry)requestParamEntriesIt.next();
  +                atts.addAttribute(PROFILER_NS, "name", "name", "CDATA", 
(String)entry.getKey());
  +                atts.addAttribute(PROFILER_NS, "value", "value", "CDATA", 
(String)entry.getValue());
  +                this.contentHandler.startElement(PROFILER_NS, 
REQUESTPARAMETER_ELEMENT, 
  +                                                 REQUESTPARAMETER_ELEMENT, atts);
  +                this.contentHandler.endElement(PROFILER_NS, 
REQUESTPARAMETER_ELEMENT, 
  +                                               REQUESTPARAMETER_ELEMENT);
  +            }
  +            this.contentHandler.endElement(PROFILER_NS, REQUESTPARAMETERS_ELEMENT, 
REQUESTPARAMETERS_ELEMENT);
  +
  +            // Generate SAX events for the session attributes
  +            this.contentHandler.startElement(PROFILER_NS, 
SESSIONATTRIBUTES_ELEMENT, SESSIONATTRIBUTES_ELEMENT, 
  +                                        new AttributesImpl());
  +
  +            Map sessionAttributes = environmentInfo.getSessionAttributes();
  +            Set sessionAttrEntries = sessionAttributes.entrySet();
  +            Iterator sessionAttrEntriesIt = sessionAttrEntries.iterator();
  +            while (sessionAttrEntriesIt.hasNext()) {
  +                AttributesImpl atts = new AttributesImpl();
  +                Map.Entry entry = (Map.Entry)sessionAttrEntriesIt.next();
  +                atts.addAttribute(PROFILER_NS, "name", "name", "CDATA", 
(String)entry.getKey());
  +                atts.addAttribute(PROFILER_NS, "value", "value", "CDATA", 
(String)entry.getValue());
  +                this.contentHandler.startElement(PROFILER_NS, 
SESSIONATTRIBUTE_ELEMENT, 
  +                                                 SESSIONATTRIBUTE_ELEMENT, atts);
  +                this.contentHandler.endElement(PROFILER_NS, 
SESSIONATTRIBUTE_ELEMENT, 
  +                                               SESSIONATTRIBUTE_ELEMENT);
               }
  +            this.contentHandler.endElement(PROFILER_NS, SESSIONATTRIBUTES_ELEMENT, 
SESSIONATTRIBUTES_ELEMENT);
  +
  +            // And the rest
  +            this.contentHandler.startElement(PROFILER_NS, "uri", "uri", new 
AttributesImpl());
  +            this.contentHandler.characters(environmentInfo.getURI().toCharArray(), 
0, 
  +                                           environmentInfo.getURI().length());
  +            this.contentHandler.endElement(PROFILER_NS, "uri", "uri");
           }
   
  -        ch.endElement(namespace, "pipeline", "pipeline");
  +        this.contentHandler.endElement(PROFILER_NS, ENVIROMENTINFO_ELEMENT, 
ENVIROMENTINFO_ELEMENT);
  +    }
  +
  +    public void generateSAXFragment(Object fragment) throws SAXException {
  +
  +        if (fragment!=null) {
  +
  +            this.contentHandler.startElement(PROFILER_NS, FRAGMENT_ELEMENT, 
FRAGMENT_ELEMENT, 
  +                                             new AttributesImpl());
  +
  +            XMLDeserializer deserializer = null;
  +            try {
  +                deserializer = 
(XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
  +                deserializer.setConsumer(new IncludeXMLConsumer(this.xmlConsumer));
  +                deserializer.deserialize(fragment);
  +            } catch (ComponentException ce) {
  +                getLogger().debug("Could not retrieve XMLDeserializer component", 
ce);
  +                throw new SAXException("Could not retrieve XMLDeserializer 
component", ce);
  +            } catch (Exception e) {
  +                getLogger().debug("Could not serialize SAX fragment", e);
  +                throw new SAXException("Could not serialize SAX fragment", e);
  +            } finally { 
  +                if (deserializer!=null)
  +                    this.manager.release(deserializer);
  +            }
  +
  +            this.contentHandler.endElement(PROFILER_NS, FRAGMENT_ELEMENT, 
FRAGMENT_ELEMENT);
  +        }
       }
   }
   
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to