KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > junit > viewer > client > ReportViewer


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.junit.viewer.client;
17
18 import com.google.gwt.core.client.EntryPoint;
19 import com.google.gwt.core.client.GWT;
20 import com.google.gwt.http.client.URL;
21 import com.google.gwt.user.client.rpc.AsyncCallback;
22 import com.google.gwt.user.client.rpc.ServiceDefTarget;
23 import com.google.gwt.user.client.ui.Button;
24 import com.google.gwt.user.client.ui.CellPanel;
25 import com.google.gwt.user.client.ui.ClickListener;
26 import com.google.gwt.user.client.ui.FlexTable;
27 import com.google.gwt.user.client.ui.HTML;
28 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
29 import com.google.gwt.user.client.ui.HasVerticalAlignment;
30 import com.google.gwt.user.client.ui.HorizontalPanel;
31 import com.google.gwt.user.client.ui.Image;
32 import com.google.gwt.user.client.ui.Label;
33 import com.google.gwt.user.client.ui.RootPanel;
34 import com.google.gwt.user.client.ui.VerticalPanel;
35 import com.google.gwt.user.client.ui.Widget;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.Comparator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * The application for viewing benchmark reports. In order for the ReportViewer
45  * to operate correctly, you must have both the {@link ReportServer} RPC and
46  * {@link com.google.gwt.junit.viewer.server.ReportImageServer} servlets up and
47  * running within a servlet container.
48  *
49  * <code>ReportViewer's</code> GWT XML module is configured to start these
50  * servlets by default. Just start <code>ReportViewer</code> in hosted mode,
51  * and GWT will run them within its own embedded servlet engine. For example,
52  *
53  * <pre>java -cp &lt;classpath&gt; com.google.gwt.dev.GWTShell -out
54  * ReportViewerShell/www
55  * com.google.gwt.junit.viewer.ReportViewer/ReportViewer.html</pre>
56  *
57  * You can configure the location where ReportServer reads the benchmark reports
58  * from by setting the system property named in
59  * {@link com.google.gwt.junit.client.Benchmark#REPORT_PATH}.
60  */

61 public class ReportViewer implements EntryPoint {
62
63   private static class MutableBool {
64
65     boolean value;
66
67     MutableBool(boolean value) {
68       this.value = value;
69     }
70   }
71
72   private static final String JavaDoc baseUrl = GWT.getModuleBaseURL();
73
74   private static final String JavaDoc imageServer = baseUrl + "test_images/";
75
76   HTML detailsLabel;
77
78   Report report;
79
80   VerticalPanel reportPanel;
81
82   ReportServerAsync reportServer;
83
84   FlexTable reportTable;
85
86   HTML statusLabel;
87
88   List JavaDoc/* <ReportSummary> */summaries;
89
90   VerticalPanel summariesPanel;
91
92   FlexTable summariesTable;
93
94   CellPanel topPanel;
95
96   public void onModuleLoad() {
97
98     init();
99
100     // Asynchronously load the summaries
101
ServiceDefTarget target = (ServiceDefTarget) GWT.create(ReportServer.class);
102     target.setServiceEntryPoint(GWT.getModuleBaseURL() + "test_reports");
103     reportServer = (ReportServerAsync) target;
104
105     reportServer.getReportSummaries(new AsyncCallback() {
106       public void onFailure(Throwable JavaDoc caught) {
107         String JavaDoc msg = "<p>" + caught.toString() + "</p>"
108             + "<p>Is your path to the reports correct?</p>";
109         statusLabel.setHTML(msg);
110       }
111
112       public void onSuccess(Object JavaDoc result) {
113         summaries = (List JavaDoc/* <ReportSummary> */) result;
114         if (summaries != null) {
115           if (summaries.size() == 0) {
116             statusLabel.setText("There are no benchmark reports available in this folder.");
117           }
118           Collections.sort(summaries, new Comparator JavaDoc() {
119             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
120               ReportSummary r1 = (ReportSummary) o1;
121               ReportSummary r2 = (ReportSummary) o2;
122               return r2.getDate().compareTo(r1.getDate()); // most recent first
123
}
124           });
125           displaySummaries();
126         }
127       }
128     });
129   }
130
131   private FlexTable createReportTable() {
132
133     FlexTable topTable = new FlexTable();
134
135     FlexTable tempReportTable = new FlexTable();
136     tempReportTable.setBorderWidth(1);
137     tempReportTable.setCellPadding(5);
138     tempReportTable.setWidget(0, 0, new Label("Date Created"));
139     tempReportTable.setWidget(0, 1, new Label("GWT Version"));
140
141     if (report == null) {
142       tempReportTable.setWidget(1, 0,
143           new Label("No currently selected report."));
144       tempReportTable.getFlexCellFormatter().setColSpan(1, 0, 3);
145       return tempReportTable;
146     }
147
148     detailsLabel.setHTML("<h3>" + report.getId() + " details </h3>");
149     tempReportTable.setWidget(1, 0, new Label(report.getDateString()));
150     tempReportTable.setWidget(1, 1, new Label(report.getGwtVersion()));
151
152     // topTable.setWidget( 0, 0, tempReportTable );
153
int currentRow = 1;
154
155     Collections.sort(report.getCategories(), new Comparator JavaDoc() {
156       public int compare(Object JavaDoc o1, Object JavaDoc o2) {
157         Category c1 = (Category) o1;
158         Category c2 = (Category) o2;
159         return c1.getName().compareTo(c2.getName());
160       }
161     }); // Should be done once in the RPC
162

163     for (int i = 0; i < report.getCategories().size(); ++i) {
164       Category c = (Category) report.getCategories().get(i);
165
166       if (!c.getName().equals("")) {
167         FlexTable categoryTable = new FlexTable();
168         categoryTable.setBorderWidth(0);
169         categoryTable.setCellPadding(5);
170         categoryTable.setText(0, 0, c.getName());
171         categoryTable.getFlexCellFormatter().setStyleName(0, 0,
172             "benchmark-category");
173
174         categoryTable.setWidget(0, 1, new Label("Description"));
175         categoryTable.setWidget(1, 0, new Label(c.getName()));
176         categoryTable.setWidget(1, 1, new Label(c.getDescription()));
177
178         topTable.setWidget(currentRow++, 0, categoryTable);
179       }
180
181       Collections.sort(c.getBenchmarks(), new Comparator JavaDoc() {
182         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
183           Benchmark b1 = (Benchmark) o1;
184           Benchmark b2 = (Benchmark) o2;
185           return b1.getName().compareTo(b2.getName());
186         }
187       }); // Should be done once in the RPC
188

189       for (int j = 0; j < c.getBenchmarks().size(); ++j) {
190         Benchmark benchmark = (Benchmark) c.getBenchmarks().get(j);
191
192         FlexTable benchmarkTable = new FlexTable();
193         benchmarkTable.setBorderWidth(0);
194         benchmarkTable.setCellPadding(5);
195         benchmarkTable.setText(0, 0, benchmark.getName());
196         // benchmarkTable.setText( 0, 1, benchmark.getDescription());
197
benchmarkTable.setWidget(1, 0, new HTML("<pre>"
198             + benchmark.getSourceCode() + "</pre>"));
199         benchmarkTable.getFlexCellFormatter().setStyleName(0, 0,
200             "benchmark-name");
201         // benchmarkTable.getFlexCellFormatter().setStyleName( 0, 1,
202
// "benchmark-description" );
203
benchmarkTable.getFlexCellFormatter().setStyleName(1, 0,
204             "benchmark-code");
205
206         // TODO(tobyr) Provide detailed benchmark information.
207
// Following commented code is a step in that direction.
208
/*
209          * benchmarkTable.setWidget( 0, 1, new Label( "Description"));
210          * benchmarkTable.setWidget( 0, 2, new Label( "Class Name"));
211          * benchmarkTable.setWidget( 0, 3, new Label( "Source Code"));
212          * benchmarkTable.setWidget( 1, 0, new Label( benchmark.getName()));
213          * benchmarkTable.setWidget( 1, 1, new Label(
214          * benchmark.getDescription())); benchmarkTable.setWidget( 1, 2, new
215          * Label( benchmark.getClassName())); benchmarkTable.setWidget( 1, 3,
216          * new HTML( "<pre>" + benchmark.getSourceCode() + "</pre>"));
217          */

218         topTable.setWidget(currentRow++, 0, benchmarkTable);
219
220         FlexTable resultsTable = new FlexTable();
221         resultsTable.setBorderWidth(0);
222         resultsTable.setCellPadding(5);
223         FlexTable.FlexCellFormatter resultsFormatter = resultsTable.getFlexCellFormatter();
224         topTable.setWidget(currentRow++, 0, resultsTable);
225
226         Collections.sort(benchmark.getResults(), new Comparator JavaDoc() {
227           public int compare(Object JavaDoc o1, Object JavaDoc o2) {
228             Result r1 = (Result) o1;
229             Result r2 = (Result) o2;
230             return r1.getAgent().compareTo(r2.getAgent());
231           }
232         }); // Should be done once in the RPC
233

234         final List JavaDoc trialsTables = new ArrayList JavaDoc();
235         final MutableBool isVisible = new MutableBool(false);
236
237         Button visibilityButton = new Button("Show Data", new ClickListener() {
238           public void onClick(Widget sender) {
239             isVisible.value = !isVisible.value;
240             for (int i = 0; i < trialsTables.size(); ++i) {
241               Widget w = (Widget) trialsTables.get(i);
242               w.setVisible(isVisible.value);
243             }
244             String JavaDoc name = isVisible.value ? "Hide Data" : "Show Data";
245             ((Button) sender).setText(name);
246           }
247         });
248
249         for (int k = 0; k < benchmark.getResults().size(); ++k) {
250           Result result = (Result) benchmark.getResults().get(k);
251
252           /*
253            * resultsTable.setWidget( 0, 0, new Label( "Result Agent"));
254            * resultsTable.setWidget( 0, 1, new Label( "Host"));
255            * resultsTable.setWidget( 0, 2, new Label( "Graph"));
256            * resultsTable.setWidget( 1, 0, new Label( result.getAgent()));
257            * resultsTable.setWidget( 1, 1, new Label( result.getHost()));
258            */

259
260           resultsTable.setWidget(0, k, new Image(getImageUrl(report.getId(),
261               c.getName(), benchmark.getClassName(), benchmark.getName(),
262               result.getAgent())));
263
264           /*
265            * FlexTable allTrialsTable = new FlexTable();
266            * allTrialsTable.setBorderWidth(1); allTrialsTable.setCellPadding(5);
267            * FlexTable.CellFormatter allTrialsFormatter = allTrialsTable
268            * .getFlexCellFormatter(); topTable.setWidget(currentRow++, 0,
269            * allTrialsTable); allTrialsTable.setWidget(0, k, trialsTable);
270            * allTrialsFormatter .setAlignment(0, k,
271            * HasHorizontalAlignment.ALIGN_CENTER,
272            * HasVerticalAlignment.ALIGN_TOP);
273            */

274
275           resultsFormatter.setAlignment(2, k,
276               HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
277
278           // A table of straight data for all trials for an agent
279
FlexTable trialsTable = new FlexTable();
280           trialsTable.setVisible(false);
281           trialsTables.add(trialsTable);
282           trialsTable.setBorderWidth(1);
283           trialsTable.setCellPadding(5);
284
285           if (k == 0) {
286             resultsTable.setWidget(1, k, visibilityButton);
287             resultsFormatter.setColSpan(1, k, benchmark.getResults().size());
288             resultsFormatter.setAlignment(1, k,
289                 HasHorizontalAlignment.ALIGN_LEFT,
290                 HasVerticalAlignment.ALIGN_MIDDLE);
291           }
292
293           resultsTable.setWidget(2, k, trialsTable);
294
295           List JavaDoc trials = result.getTrials();
296           int numTrials = trials.size();
297           int numVariables = ((Trial) trials.get(0)).getVariables().size();
298
299           Trial sampleTrial = (Trial) trials.get(0);
300           Map JavaDoc variables = sampleTrial.getVariables();
301           List JavaDoc variableNames = new ArrayList JavaDoc(variables.keySet());
302           Collections.sort(variableNames);
303
304           // Write out the variable column headers
305
for (int varIndex = 0; varIndex < numVariables; ++varIndex) {
306             String JavaDoc varName = (String JavaDoc) variableNames.get(varIndex);
307             trialsTable.setWidget(0, varIndex, new HTML(varName));
308           }
309
310           // Timing header
311
trialsTable.setWidget(0, numVariables, new HTML("Timing (ms)"));
312
313           // Write out all the trial data
314
for (int l = 0; l < numTrials; ++l) {
315             Trial trial = (Trial) trials.get(l);
316
317             // Write the variable values
318
for (int varIndex = 0; varIndex < numVariables; ++varIndex) {
319               String JavaDoc varName = (String JavaDoc) variableNames.get(varIndex);
320               String JavaDoc varValue = (String JavaDoc) trial.getVariables().get(varName);
321               trialsTable.setWidget(l + 1, varIndex, new HTML(varValue));
322             }
323
324             // Write out the timing data
325
String JavaDoc data = null;
326             if (trial.getException() != null) {
327               data = trial.getException();
328             } else {
329               data = trial.getRunTimeMillis() + "";
330             }
331             trialsTable.setWidget(l + 1, numVariables, new HTML(data));
332           }
333         }
334       }
335     }
336
337     return topTable;
338   }
339
340   private FlexTable createSummariesTable() {
341
342     FlexTable tempSummariesTable = new FlexTable();
343     tempSummariesTable.setCellPadding(5);
344     tempSummariesTable.setBorderWidth(1);
345     tempSummariesTable.setWidget(0, 0, new Label("Id"));
346     tempSummariesTable.setWidget(0, 1, new Label("Date Created"));
347     tempSummariesTable.setWidget(0, 2, new Label("Tests"));
348     // tempSummariesTable.setWidget( 0, 3, new Label( "Succeeded"));
349

350     if (summaries == null) {
351       tempSummariesTable.setWidget(1, 0, new Label("Fetching reports..."));
352       tempSummariesTable.getFlexCellFormatter().setColSpan(1, 0, 4);
353       return tempSummariesTable;
354     }
355
356     for (int i = 0; i < summaries.size(); ++i) {
357       ReportSummary summary = (ReportSummary) summaries.get(i);
358       int index = i + 1;
359       Label idLabel = new Label(summary.getId());
360       idLabel.addClickListener(new ClickListener() {
361         public void onClick(Widget w) {
362           getReportDetails(((Label) w).getText());
363         }
364       });
365       tempSummariesTable.setWidget(index, 0, idLabel);
366       tempSummariesTable.setWidget(index, 1, new Label(summary.getDateString()));
367       tempSummariesTable.setWidget(index, 2, new Label(summary.getNumTests()
368           + ""));
369       // tempSummariesTable.setWidget( index, 3, new
370
// Label(summary.allTestsSucceeded() + ""));
371
}
372
373     return tempSummariesTable;
374   }
375
376   private void displayReport() {
377     FlexTable table = createReportTable();
378     reportPanel.remove(reportTable);
379     reportTable = table;
380     reportPanel.insert(reportTable, 1);
381   }
382
383   private void displaySummaries() {
384     FlexTable table = createSummariesTable();
385     summariesPanel.remove(summariesTable);
386     summariesTable = table;
387     summariesPanel.insert(summariesTable, 1);
388   }
389
390   private String JavaDoc encode(String JavaDoc str) {
391     if (str.equals("")) {
392       return str;
393     }
394     return URL.encodeComponent(str);
395   }
396
397   private String JavaDoc getImageUrl(String JavaDoc report, String JavaDoc category, String JavaDoc testClass,
398       String JavaDoc testMethod, String JavaDoc agent) {
399     return imageServer + encode(report) + "/" + encode(category) + "/"
400         + encode(testClass) + "/" + encode(testMethod) + "/" + encode(agent);
401   }
402
403   /**
404    * Loads report details asynchronously for a given report.
405    *
406    * @param id the non-null id of the report
407    */

408   private void getReportDetails(String JavaDoc id) {
409     statusLabel.setText("Retrieving the report...");
410     reportServer.getReport(id, new AsyncCallback() {
411       public void onFailure(Throwable JavaDoc caught) {
412         statusLabel.setText(caught.toString());
413       }
414
415       public void onSuccess(Object JavaDoc result) {
416         report = (Report) result;
417         statusLabel.setText("Finished fetching report details.");
418         displayReport();
419       }
420     });
421   }
422
423   private void init() {
424     topPanel = new VerticalPanel();
425
426     summariesPanel = new VerticalPanel();
427     summariesPanel.add(new HTML("<h3>Benchmark Reports</h3>"));
428     summariesTable = createSummariesTable();
429     summariesPanel.add(summariesTable);
430
431     reportPanel = new VerticalPanel();
432     detailsLabel = new HTML("<h3>Report Details</h3>");
433     reportPanel.add(detailsLabel);
434     reportTable = createReportTable();
435     // reportPanel.add( reportTable );
436

437     topPanel.add(summariesPanel);
438     CellPanel spacerPanel = new HorizontalPanel();
439     spacerPanel.setSpacing(10);
440     spacerPanel.add(new Label());
441     topPanel.add(spacerPanel);
442     topPanel.add(reportPanel);
443     final RootPanel root = RootPanel.get();
444
445     root.add(topPanel);
446
447     statusLabel = new HTML("Select a report.");
448     root.add(statusLabel);
449   }
450 }
451
Popular Tags