KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > j2eedo > common > TraceTime


1 /*
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 1.0
21  *
22  * Created on 15 mars 2004
23  * @author fmillevi@yahoo.com
24  *
25  */

26 package org.objectweb.speedo.j2eedo.common;
27
28 import java.util.Vector JavaDoc;
29
30 public class TraceTime {
31     public Vector JavaDoc trace;
32     private boolean running;
33
34     /**
35      * Empty constructor
36      */

37     public TraceTime() {
38         this.trace = new Vector JavaDoc();
39         this.running = false;
40     }
41
42     public void startTask(String JavaDoc task) {
43         if (this.running) {
44             this.endTask();
45         }
46         TraceTimeElement tt = new TraceTimeElement(task);
47         this.trace.add(tt);
48         running = true;
49     }
50
51     public void endTask() {
52         try {
53             TraceTimeElement tt = (TraceTimeElement) this.trace.lastElement();
54             tt.endTask();
55         } catch (Exception JavaDoc e) {
56
57         }
58         this.running = false;
59     }
60
61     public void reset() {
62         if (this.running) {
63             this.endTask();
64         }
65         this.trace = null;
66         this.trace = new Vector JavaDoc();
67         this.running = false;
68     }
69
70     public String JavaDoc reportXMLComment() {
71         return "<!-- TraceTime Report:\n\t" + report() + "\n-->";
72         
73     }
74     public String JavaDoc report() {
75         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
76         try {
77             if (this.trace.size() > 0) {
78                 if (this.running) {
79                     this.endTask();
80                 }
81
82                 TraceTimeElement tt = null;
83                 for (int i = 0; i < this.trace.size(); i++) {
84                     tt = (TraceTimeElement) this.trace.get(i);
85                     sb.append(tt.task);
86                     sb.append(":");
87                     sb.append(tt.duration());
88                     sb.append(" ms\t");
89                     tt = null;
90                 }
91                 long duration =
92                     ((TraceTimeElement) this.trace.lastElement())
93                         .endTime
94                         .getTime()
95                         - ((TraceTimeElement) this.trace.firstElement())
96                             .beginTime
97                             .getTime();
98                 sb.append("Total:");
99                 sb.append(duration);
100                 sb.append(" ms\t");
101             } else {
102                 sb.append("Nothing\t");
103             }
104         } catch (Exception JavaDoc e) {
105         }
106
107         return sb.toString();
108     }
109
110     public static void main(String JavaDoc[] args) {
111         TraceTime tt = new TraceTime();
112         tt.startTask("sample");
113         try {
114             Thread.sleep(1000);
115
116         } catch (Exception JavaDoc e) {
117             System.err.println("Exception:" + e);
118         }
119         tt.endTask();
120         tt.startTask("other sample");
121         try {
122             Thread.sleep(300);
123
124         } catch (Exception JavaDoc e) {
125             System.err.println("Exception:" + e);
126         }
127         System.out.println(tt.report());
128     }
129 }
130
Popular Tags