blob: d47f444bef1b8fafb7dc14b9a54b38edcec631fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.server;
import com.vaadin.terminal.WrappedRequest;
/**
* Times the handling of requests and stores the information as an attribute in
* the request. The timing info is later passed on to the client in the UIDL and
* the client provides JavaScript API for accessing this data from e.g.
* TestBench.
*
* @author Jonatan Kronqvist / Vaadin Ltd
*/
public class RequestTimer {
public static final String SESSION_ATTR_ID = "REQUESTTIMER";
private long requestStartTime = 0;
private long totalSessionTime = 0;
private long lastRequestTime = -1;
/**
* Starts the timing of a request. This should be called before any
* processing of the request.
*
* @param request
* the request.
*/
public void start(WrappedRequest request) {
requestStartTime = System.nanoTime();
request.setAttribute("TOTAL", totalSessionTime);
request.setAttribute("LASTREQUEST", lastRequestTime);
}
/**
* Stops the timing of a request. This should be called when all processing
* of a request has finished.
*/
public void stop() {
// Measure and store the total handling time. This data can be
// used in TestBench 3 tests.
long time = (System.nanoTime() - requestStartTime) / 1000000;
lastRequestTime = time;
totalSessionTime += time;
}
/**
* Returns a valid request timer for the specified request. Timers are
* session bound.
*
* @param request
* the request for which to get a valid timer.
* @return a valid timer.
*/
public static RequestTimer get(WrappedRequest request) {
RequestTimer timer = (RequestTimer) request
.getSessionAttribute(SESSION_ATTR_ID);
if (timer == null) {
timer = new RequestTimer();
}
return timer;
}
/**
* Associates the specified request timer with the specified request. Since
* {@link #get(RequestWrapper)} will, at one point or another, return a new
* instance, this method should be called to keep the request <-> timer
* association updated.
*
* @param request
* the request for which to set the timer.
* @param requestTimer
* the timer.
*/
public static void set(WrappedRequest request, RequestTimer requestTimer) {
request.setSessionAttribute(RequestTimer.SESSION_ATTR_ID, requestTimer);
}
}
|