001: // $Id: Profiler.java,v 1.6 2005/05/30 16:14:35 belaban Exp $
002:
003: package org.jgroups.debug;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007:
008: import java.io.FileOutputStream;
009: import java.io.OutputStream;
010: import java.util.Enumeration;
011: import java.util.Hashtable;
012:
013: /**
014: * Allows to time execution of 'named' statements, counts number of times called and total
015: * execution time.
016: *
017: * @author Bela Ban
018: */
019: public class Profiler {
020:
021: static public class Entry {
022: long num_calls = 0;
023: long tot_time = 0;
024: double avg = 0.0;
025: long start_time = 0;
026: long stop_time = 0;
027:
028: synchronized void compute() {
029: num_calls++;
030: tot_time += stop_time - start_time;
031: avg = (double) tot_time / num_calls;
032: }
033: }
034:
035: private static OutputStream os = null;
036: private static final Hashtable entries = new Hashtable();
037: private static Log log = LogFactory.getLog(Profiler.class);
038:
039: public Profiler() {
040: try {
041: os = new FileOutputStream("profiler.dat");
042: } catch (Exception e) {
043: log.error(e);
044: }
045: }
046:
047: public static void setFilename(String filename) {
048: try {
049: if (os != null) {
050: os.close();
051: }
052: os = new FileOutputStream(filename);
053: } catch (Exception e) {
054: log.error(e);
055: }
056: }
057:
058: public static void start(String call_name) {
059: Entry e = (Entry) entries.get(call_name);
060: if (e == null) {
061: e = new Entry();
062: entries.put(call_name, e);
063: }
064: e.start_time = System.currentTimeMillis();
065: }
066:
067: public static void stop(String call_name) {
068: Entry e = (Entry) entries.get(call_name);
069: if (e == null) {
070: log.error("Profiler.stop(): entry for " + call_name
071: + " not found");
072: return;
073: }
074: e.stop_time = System.currentTimeMillis();
075: e.compute();
076: }
077:
078: public static void dump() { // dump to file
079: String key;
080: Entry val;
081: if (os == null) {
082: log.error("Profiler.dump(): output file is null");
083: return;
084: }
085: try {
086: os
087: .write("Key: Number of calls: Total time (ms): Average time (ms):\n"
088: .getBytes());
089: os
090: .write("-----------------------------------------------------------------\n\n"
091: .getBytes());
092: } catch (Exception e) {
093: log.error(e);
094: }
095: for (Enumeration e = entries.keys(); e.hasMoreElements();) {
096: key = (String) e.nextElement();
097: val = (Entry) entries.get(key);
098: try {
099: os.write((key + ": " + val.num_calls + ' '
100: + val.tot_time + ' ' + trim(val.avg) + '\n')
101: .getBytes());
102: } catch (Exception ex) {
103: log.error(ex);
104: }
105: }
106: }
107:
108: public static double trim(double inp) {
109: double retval = 0.0, rem = 0.0;
110: long l1, l2;
111:
112: l1 = (long) inp;
113: rem = inp - l1;
114: rem = rem * 100.0;
115: l2 = (long) rem;
116: rem = l2 / 100.0;
117: retval = l1 + rem;
118: return retval;
119: }
120:
121: public static void main(String[] args) {
122: Profiler.setFilename("bela.out");
123:
124: try {
125:
126: Profiler.start("time1");
127: Thread.sleep(1500);
128: Profiler.stop("time1");
129:
130: Profiler.start("time1");
131: Thread.sleep(1500);
132: Profiler.start("time2");
133: Thread.sleep(500);
134:
135: Profiler.stop("time2");
136: Thread.sleep(1500);
137: Profiler.stop("time1");
138:
139: Profiler.dump();
140: } catch (Exception e) {
141: log.error(e);
142: }
143: }
144: }
|