KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > debug > Profiler


1 // $Id: Profiler.java,v 1.4 2004/09/23 16:29:16 belaban Exp $
2

3 package org.jgroups.debug;
4
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Hashtable JavaDoc;
9
10 /**
11  * Allows to time execution of 'named' statements, counts number of times called and total
12  * execution time.
13  *
14  * @author Bela Ban
15  */

16 public class Profiler {
17
18     static public class Entry {
19         long num_calls=0;
20         long tot_time=0;
21         double avg=0.0;
22         long start_time=0;
23         long stop_time=0;
24
25         synchronized void compute() {
26             num_calls++;
27             tot_time+=stop_time - start_time;
28             avg=(double)tot_time / num_calls;
29         }
30     }
31
32
33     private static OutputStream JavaDoc os=null;
34     private static final Hashtable JavaDoc entries=new Hashtable JavaDoc();
35
36
37     public Profiler() {
38         try {
39             os=new FileOutputStream JavaDoc("profiler.dat");
40         }
41         catch(Exception JavaDoc e) {
42             System.err.println(e);
43         }
44     }
45
46
47     public static void setFilename(String JavaDoc filename) {
48         try {
49             if(os != null) {
50                 os.close();
51             }
52             os=new FileOutputStream JavaDoc(filename);
53         }
54         catch(Exception JavaDoc e) {
55             System.err.println(e);
56         }
57     }
58
59
60     public static void start(String JavaDoc call_name) {
61         Entry e=(Entry)entries.get(call_name);
62         if(e == null) {
63             e=new Entry();
64             entries.put(call_name, e);
65         }
66         e.start_time=System.currentTimeMillis();
67     }
68
69
70     public static void stop(String JavaDoc call_name) {
71         Entry e=(Entry)entries.get(call_name);
72         if(e == null) {
73             System.err.println("Profiler.stop(): entry for " + call_name + " not found");
74             return;
75         }
76         e.stop_time=System.currentTimeMillis();
77         e.compute();
78     }
79
80
81     public static void dump() { // dump to file
82
String JavaDoc key;
83         Entry val;
84         if(os == null) {
85             System.err.println("Profiler.dump(): output file is null");
86             return;
87         }
88         try {
89             os.write("Key: Number of calls: Total time (ms): Average time (ms):\n".getBytes());
90             os.write("-----------------------------------------------------------------\n\n".getBytes());
91         }
92         catch(Exception JavaDoc e) {
93             System.err.println(e);
94         }
95         for(Enumeration JavaDoc e=entries.keys(); e.hasMoreElements();) {
96             key=(String JavaDoc)e.nextElement();
97             val=(Entry)entries.get(key);
98             try {
99                 os.write((key + ": " + val.num_calls + ' ' +
100                           val.tot_time + ' ' + trim(val.avg) + '\n').getBytes());
101             }
102             catch(Exception JavaDoc ex) {
103                 System.err.println(ex);
104             }
105         }
106     }
107
108
109     public static double trim(double inp) {
110         double retval=0.0, rem=0.0;
111         long l1, l2;
112
113         l1=(long)inp;
114         rem=inp - l1;
115         rem=rem * 100.0;
116         l2=(long)rem;
117         rem=l2 / 100.0;
118         retval=l1 + rem;
119         return retval;
120     }
121
122
123     public static void main(String JavaDoc[] args) {
124         Profiler.setFilename("bela.out");
125
126
127         try {
128
129             Profiler.start("time1");
130             Thread.sleep(1500);
131             Profiler.stop("time1");
132
133             Profiler.start("time1");
134             Thread.sleep(1500);
135             Profiler.start("time2");
136             Thread.sleep(500);
137
138             Profiler.stop("time2");
139             Thread.sleep(1500);
140             Profiler.stop("time1");
141
142
143             Profiler.dump();
144         }
145         catch(Exception JavaDoc e) {
146             System.err.println(e);
147         }
148     }
149 }
150
Popular Tags