KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > coverage > Profile


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.coverage;
6
7 import java.io.BufferedWriter JavaDoc;
8 import java.io.FileReader JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.io.LineNumberReader JavaDoc;
11
12 import org.h2.util.IOUtils;
13
14 /**
15  * The class used at runtime to measure the code usage and performance.
16  */

17 public class Profile extends Thread JavaDoc {
18     public static final boolean LIST_UNVISITED = true;
19     public static final boolean FAST = false;
20     public static final boolean TRACE = false;
21     public static final Profile main = new Profile();
22     public static int current;
23     private BufferedWriter JavaDoc trace;
24     public int[] count;
25     public int[] time;
26     boolean stop;
27     int maxIndex;
28     int lastIndex;
29     long lastTime;
30     static int top = 15;
31     
32     static {
33         try {
34             String JavaDoc s = System.getProperty("profile.top");
35             if (s != null) {
36                 top = Integer.parseInt(s);
37             }
38         } catch (Throwable JavaDoc e) {
39             // ignore SecurityExceptions
40
}
41     }
42
43     public static void visit(int i) {
44         if (FAST) {
45             current = i;
46         } else {
47             main.addVisit(i);
48         }
49     }
50
51     public void run() {
52         list();
53     }
54
55     public static void startCollecting() {
56         main.stop = false;
57         main.lastTime = System.currentTimeMillis();
58     }
59
60     public static void stopCollecting() {
61         main.stop = true;
62     }
63
64     public static void list() {
65         if (main.lastIndex == 0) {
66             // don't list anything if no statistics collected
67
return;
68         }
69         try {
70             main.listUnvisited();
71             main.listTop("MOST CALLED", main.count, top);
72             main.listTop("MOST TIME USED", main.time, top);
73         } catch (Exception JavaDoc e) {
74             e.printStackTrace();
75         }
76     }
77
78     Profile() {
79         FileReader JavaDoc reader = null;
80         try {
81             reader = new FileReader JavaDoc("profile.txt");
82             LineNumberReader JavaDoc r = new LineNumberReader JavaDoc(reader);
83             while (r.readLine() != null) {
84                 // nothing - just count lines
85
}
86             maxIndex = r.getLineNumber();
87             count = new int[maxIndex];
88             time = new int[maxIndex];
89             lastTime = System.currentTimeMillis();
90             Runtime.getRuntime().addShutdownHook(this);
91         } catch (Exception JavaDoc e) {
92             e.printStackTrace();
93             System.exit(1);
94         } finally {
95             IOUtils.closeSilently(reader);
96         }
97     }
98
99     void addVisit(int i) {
100         if (stop) {
101             return;
102         }
103         long now = System.currentTimeMillis();
104         if (TRACE && trace != null) {
105             int duration = (int) (now - lastTime);
106             try {
107                 trace.write(i + "\t" + duration + "\r\n");
108             } catch (Exception JavaDoc e) {
109                 e.printStackTrace();
110                 System.exit(1);
111             }
112         }
113         count[i]++;
114         time[lastIndex] += (int) (now - lastTime);
115         lastTime = now;
116         lastIndex = i;
117     }
118
119     void listUnvisited() throws Exception JavaDoc {
120         printLine('=');
121         print("NOT COVERED");
122         printLine('-');
123         FileReader JavaDoc reader = null;
124         FileWriter JavaDoc fwriter = null;
125         try {
126             reader = new FileReader JavaDoc("profile.txt");
127             LineNumberReader JavaDoc r = new LineNumberReader JavaDoc(reader);
128             fwriter = new FileWriter JavaDoc("notcovered.txt");
129             BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(fwriter);
130             int unvisited = 0;
131             int unvisitedthrow = 0;
132             for (int i = 0; i < maxIndex; i++) {
133                 String JavaDoc line = r.readLine();
134                 if (count[i] == 0) {
135                     if (!line.endsWith("throw")) {
136                         writer.write(line + "\r\n");
137                         if(LIST_UNVISITED) {
138                             print(line+"\r\n");
139                         }
140                         unvisited++;
141                     } else {
142                         unvisitedthrow++;
143                     }
144                 }
145             }
146             int percent = (100 * unvisited / maxIndex);
147             print("Not covered: " + percent + " % " + " (" + unvisited + " of "
148                     + maxIndex + "; throw=" + unvisitedthrow + ")");
149         } finally {
150             IOUtils.closeSilently(fwriter);
151             IOUtils.closeSilently(reader);
152         }
153     }
154
155     void listTop(String JavaDoc title, int[] list, int max) throws Exception JavaDoc {
156         printLine('-');
157         int total = 0;
158         int totallines = 0;
159         for (int j = 0; j < maxIndex; j++) {
160             int l = list[j];
161             if (l > 0) {
162                 total += list[j];
163                 totallines++;
164             }
165         }
166         if (max == 0) {
167             max = totallines;
168         }
169         print(title);
170         print("Total: " + total);
171         printLine('-');
172         String JavaDoc[] text = new String JavaDoc[max];
173         int[] index = new int[max];
174         for (int i = 0; i < max; i++) {
175             int big = list[0];
176             int bigIndex = 0;
177             for (int j = 1; j < maxIndex; j++) {
178                 int l = list[j];
179                 if (l > big) {
180                     big = l;
181                     bigIndex = j;
182                 }
183             }
184             list[bigIndex] = -(big + 1);
185             index[i] = bigIndex;
186         }
187         FileReader JavaDoc reader = null;
188         try {
189             reader = new FileReader JavaDoc("profile.txt");
190             LineNumberReader JavaDoc r = new LineNumberReader JavaDoc(reader);
191             for (int i = 0; i < maxIndex; i++) {
192                 String JavaDoc line = r.readLine();
193                 int k = list[i];
194                 if (k < 0) {
195                     k = -(k + 1);
196                     list[i] = k;
197                     for (int j = 0; j < max; j++) {
198                         if (index[j] == i) {
199                             int percent = (100 * k / total);
200                             text[j] = k + " " + percent + "%: " + line;
201                         }
202                     }
203                 }
204             }
205             for (int i = 0; i < max; i++) {
206                 print(text[i]);
207             }
208         } finally {
209             IOUtils.closeSilently(reader);
210         }
211     }
212
213     void print(String JavaDoc s) {
214         System.out.println(s);
215     }
216
217     void printLine(char c) {
218         for (int i = 0; i < 60; i++) {
219             System.out.print(c);
220         }
221         print("");
222     }
223 }
224
225
Popular Tags