001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.profiler;
018:
019: import org.apache.cocoon.util.HashUtil;
020:
021: import java.util.ArrayList;
022:
023: /**
024: * Request-time profiler information.
025: *
026: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
027: * @author <a href="mailto:bruno@outerthought.org">Bruno Dumon</a>
028: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
029: * @version CVS $Id: ProfilerData.java 433543 2006-08-22 06:22:54Z crossley $
030: */
031: public class ProfilerData {
032:
033: /**
034: * Entry, which stores the role and source of a component from a pipeline.
035: */
036: public static class Entry {
037:
038: public String role;
039: public String source;
040: public long setup;
041: public long time;
042: public Object fragment;
043:
044: protected Entry(String role, String source) {
045: this .role = role;
046: this .source = source;
047: }
048: }
049:
050: // List of all entries
051: private ArrayList entries = null;
052:
053: // Environment information
054: private EnvironmentInfo environmentinfo;
055:
056: // Measured total time
057: private long totaltime = 0;
058:
059: /**
060: * Create a new profiler dataset.
061: */
062: public ProfilerData() {
063: entries = new ArrayList();
064: }
065:
066: /**
067: * Add new component from the pipeling, which should be measured.
068: *
069: * @param component Component of the pipeline.
070: * @param role Role of the component.
071: * @param source Source attribute of the component.
072: */
073: public void addComponent(Object component, String role,
074: String source) {
075: entries.add(new Entry((role != null) ? role : component
076: .getClass().getName(), source));
077: }
078:
079: /**
080: * Returns the count of components.
081: *
082: * @return Count of components.
083: */
084: public int getCount() {
085: return entries.size();
086: }
087:
088: /**
089: * Set the environment information.
090: *
091: * @param environmentinfo Environment information.
092: */
093: public void setEnvironmentInfo(EnvironmentInfo environmentinfo) {
094: this .environmentinfo = environmentinfo;
095: }
096:
097: /**
098: * Returns the environment information.
099: *
100: * @return Environment information.
101: */
102: public EnvironmentInfo getEnvironmentInfo() {
103: return this .environmentinfo;
104: }
105:
106: /**
107: * Set measured time of precessing from the pipeline.
108: *
109: * @param time Total time of all components.
110: */
111: public void setTotalTime(long time) {
112: this .totaltime = time;
113: }
114:
115: /**
116: * Return measured time of precessing from the pipeline.
117: *
118: * @return Total time of all components.
119: */
120: public long getTotalTime() {
121: return this .totaltime;
122: }
123:
124: /**
125: * Set measured setup time of the i-th component of the pipeline.
126: *
127: * @param index Index of the component.
128: * @param time Measured setup time of the component.
129: */
130: public void setSetupTime(int index, long time) {
131: ((Entry) entries.get(index)).setup = time;
132: }
133:
134: /**
135: * Get measured setup time of the i-th component of the pipeline.
136: *
137: * @param index Index of the component.
138: * @return Measured setup time of the component.
139: */
140: public long getSetupTime(int index) {
141: return ((Entry) entries.get(index)).setup;
142: }
143:
144: /**
145: * Set measured processing time of the i-th component of the pipeline.
146: *
147: * @param index Index of the component.
148: * @param time Measured processing time of the component.
149: */
150: public void setProcessingTime(int index, long time) {
151: ((Entry) entries.get(index)).time = time;
152: }
153:
154: /**
155: * Get measured processing time of the i-th component of the pipeline.
156: *
157: * @param index Index of the component.
158: * @return Measured processing time of the component.
159: */
160: public long getProcessingTime(int index) {
161: return ((Entry) entries.get(index)).time;
162: }
163:
164: /**
165: * Set the SAX fragment for the i-th component of the pipeline.
166: *
167: * @param index Index of the component.
168: * @param fragment SAX fragment of the component.
169: */
170: public void setSAXFragment(int index, Object fragment) {
171: ((Entry) entries.get(index)).fragment = fragment;
172: }
173:
174: /**
175: * Returns all measured times.
176: *
177: * @return Array of all entries.
178: */
179: public Entry[] getEntries() {
180: return (Entry[]) entries.toArray(new Entry[entries.size()]);
181: }
182:
183: /**
184: * Generate a key for a given URI for this pipeline
185: *
186: * @param uri URI
187: * @return Hash key.
188: */
189: public long getKey(String uri) {
190: StringBuffer key = new StringBuffer(uri);
191:
192: for (int i = 0; i < entries.size(); i++) {
193: Entry entry = (Entry) entries.get(i);
194:
195: key.append(':');
196: key.append(entry.role);
197: key.append(':');
198: key.append(entry.source);
199: }
200: return HashUtil.hash(key);
201: }
202: }
|