001: /*
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Release: 1.0
021: *
022: * Created on 15 mars 2004
023: * @author fmillevi@yahoo.com
024: *
025: */
026: package org.objectweb.speedo.j2eedo.common;
027:
028: import java.util.Vector;
029:
030: public class TraceTime {
031: public Vector trace;
032: private boolean running;
033:
034: /**
035: * Empty constructor
036: */
037: public TraceTime() {
038: this .trace = new Vector();
039: this .running = false;
040: }
041:
042: public void startTask(String task) {
043: if (this .running) {
044: this .endTask();
045: }
046: TraceTimeElement tt = new TraceTimeElement(task);
047: this .trace.add(tt);
048: running = true;
049: }
050:
051: public void endTask() {
052: try {
053: TraceTimeElement tt = (TraceTimeElement) this .trace
054: .lastElement();
055: tt.endTask();
056: } catch (Exception e) {
057:
058: }
059: this .running = false;
060: }
061:
062: public void reset() {
063: if (this .running) {
064: this .endTask();
065: }
066: this .trace = null;
067: this .trace = new Vector();
068: this .running = false;
069: }
070:
071: public String reportXMLComment() {
072: return "<!-- TraceTime Report:\n\t" + report() + "\n-->";
073:
074: }
075:
076: public String report() {
077: StringBuffer sb = new StringBuffer();
078: try {
079: if (this .trace.size() > 0) {
080: if (this .running) {
081: this .endTask();
082: }
083:
084: TraceTimeElement tt = null;
085: for (int i = 0; i < this .trace.size(); i++) {
086: tt = (TraceTimeElement) this .trace.get(i);
087: sb.append(tt.task);
088: sb.append(":");
089: sb.append(tt.duration());
090: sb.append(" ms\t");
091: tt = null;
092: }
093: long duration = ((TraceTimeElement) this .trace
094: .lastElement()).endTime.getTime()
095: - ((TraceTimeElement) this .trace.firstElement()).beginTime
096: .getTime();
097: sb.append("Total:");
098: sb.append(duration);
099: sb.append(" ms\t");
100: } else {
101: sb.append("Nothing\t");
102: }
103: } catch (Exception e) {
104: }
105:
106: return sb.toString();
107: }
108:
109: public static void main(String[] args) {
110: TraceTime tt = new TraceTime();
111: tt.startTask("sample");
112: try {
113: Thread.sleep(1000);
114:
115: } catch (Exception e) {
116: System.err.println("Exception:" + e);
117: }
118: tt.endTask();
119: tt.startTask("other sample");
120: try {
121: Thread.sleep(300);
122:
123: } catch (Exception e) {
124: System.err.println("Exception:" + e);
125: }
126: System.out.println(tt.report());
127: }
128: }
|