Monday, November 29, 2010

Using the Lightweight Metrics System in my GWT code

Hi everyone,

We have a class for capturing events in our Java code for performance
monitoring, which I have ported to our GWT client code.
Unfortunately, getting it to play nice with the Lightweight Metrics
System has been a little frustrating.

By itself, our performance monitoring classes work as expected;
basically it consists of a ProfileEvent class which has a String
description and a long time (designed for currentTimeMillis), and a
Profiler class which creates ProfileEvents and puts them into an

By comparing the times of the various ProfileEvents, I can get a rough
idea of the time taken between any two events in the ArrayList.

Over the weekend I decided to wire up the Lightweight Metrics System
to a Profiler in my GWT module. I did this by adapting the Javascript
over at,
but instead of sending my events to a div, I send them to my GWT
Profiler class.

My JSNI looks like this:

private static native void publish()
$wnd.addPerfEvent = function (event) { // called by
window.__gwtStatsEvent instead of the div example
if (event) {
(event.moduleName,event.subSystem,event.evtGroup,event.type,'' +

My GWT code is something like:

public static void logProfilerEvent (String moduleName, String
subsystem, String eventGroup, String type, String millis) {
Double milliValue = NumberFormat.getDecimalFormat().parse(millis);
long milliTime = milliValue.longValue();

Profiler profiler = getProfiler ();
profiler.logEvent (new ProfileEvent (moduleName + "." + subsystem +
"." + type, milliTime));

To get this to work I had to cast the event.millis field, a native
Javascript number, to a String by appending an empty String. Then, on
the GWT side, I am parsing the String and converting it to the long I
would have preferred. Passing the millis as a Long worked up to a
point, but eventually it would throw a Javascript exception because
millis, despite being passed as a "Long", did not conform to the Long
format GWT was expecting (something like e[0] + e[1]; in the compiled
Javascript; one or the other would be undefined)... hence the

So now it appears to work like a charm.

My question is: Is my little hack actually necessary, or is there
some JSNI trick or GWT tip that I missed that would make this code
more elegant? Should passing millis as a Long work, perhaps there is
an issue in the compiler?

I saw recently that there have been contributions made to the
Lightweight Metrics System that look like it may play nice with the
GWT code in the future, and I look forward to seeing that day.


You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment