summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/server/RequestTimer.java
blob: 6c0edec466b74e3d50325d17a3415f1eb5deb7c5 (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
/*
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.terminal.gwt.server;

import java.io.Serializable;

/**
 * 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 implements Serializable {
    private long requestStartTime = 0;

    /**
     * Starts the timing of a request. This should be called before any
     * processing of the request.
     */
    public void start() {
        requestStartTime = System.nanoTime();
    }

    /**
     * Stops the timing of a request. This should be called when all processing
     * of a request has finished.
     * 
     * @param context
     */
    public void stop(AbstractWebApplicationContext context) {
        // Measure and store the total handling time. This data can be
        // used in TestBench 3 tests.
        long time = (System.nanoTime() - requestStartTime) / 1000000;

        // The timings must be stored in the context, since a new
        // RequestTimer is created for every request.
        context.setLastRequestTime(time);
    }
}