KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > session > SessionStatistics


1 /*
2  * $Id: SessionStatistics.java,v 1.4 2004/12/01 07:54:27 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.session;
15
16 import java.text.DateFormat JavaDoc;
17 import java.util.Date JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:@mueller.de">armin</a>
21  * @version $Revision: 1.4 $
22  */

23 public class SessionStatistics {
24
25     private final long birthDay = System.currentTimeMillis();
26
27     private long dispatchStartTime = 0;
28     private int dispatchCounter = 0;
29     private long dispatchDuration = 0;
30
31     private long deliverStartTime = 0;
32     private int deliverCounter = 0;
33     private long deliverDuration = 0;
34
35     private long requestStartTime = 0;
36     private int requestCounter = 0;
37     private long requestDuration = 0;
38
39
40     public final long getBirthDay() {
41         return birthDay;
42     }
43
44     public final long getUptime() {
45         return System.currentTimeMillis() - birthDay;
46     }
47
48     final void startRequest() {
49         requestStartTime = System.currentTimeMillis();
50     }
51
52     final void endRequest() {
53         endDispatching();
54         endDelivering();
55         if (requestStartTime > 0) {
56             long duration = System.currentTimeMillis() - requestStartTime;
57             WingsStatistics.getStatistics().incrementRequestCount(duration);
58
59             requestCounter++;
60             requestDuration += duration;
61             requestStartTime = -1;
62         }
63     }
64
65     public final long getRequestCount() {
66         return requestCounter;
67     }
68
69     public final long getRequestDuration() {
70         return requestDuration;
71     }
72
73     final void startDispatching() {
74         dispatchStartTime = System.currentTimeMillis();
75     }
76
77     final void endDispatching() {
78         if (dispatchStartTime > 0) {
79             long duration = System.currentTimeMillis() - dispatchStartTime;
80             WingsStatistics.getStatistics().incrementDispatchCount(duration);
81
82             dispatchCounter++;
83             dispatchDuration += duration;
84             dispatchStartTime = -1;
85         }
86     }
87
88     public final int getDispatchCount() {
89         return dispatchCounter;
90     }
91
92     public final long getDispatchDuration() {
93         return dispatchDuration;
94     }
95
96
97     final void startDelivering() {
98         deliverStartTime = System.currentTimeMillis();
99     }
100
101     final void endDelivering() {
102         if (deliverStartTime > 0) {
103             long duration = System.currentTimeMillis() - deliverStartTime;
104             WingsStatistics.getStatistics().incrementDeliverCount(duration);
105
106             deliverCounter++;
107             deliverDuration += duration;
108             deliverStartTime = -1;
109         }
110     }
111
112     public final int getDeliverCount() {
113         return deliverCounter;
114     }
115
116     public final long getDeliverDuration() {
117         return deliverDuration;
118     }
119
120     public String JavaDoc toString() {
121         StringBuffer JavaDoc tResult = new StringBuffer JavaDoc();
122
123         tResult.append("birthday: ").append(DateFormat.getDateTimeInstance().format(new Date JavaDoc(birthDay))).append("\n")
124                 .append("requests: ").append(requestCounter).append(" / ").append(requestCounter == 0 ? 0 : requestDuration / requestCounter).append(" ms\n")
125                 .append("dispatch: ").append(dispatchCounter).append(" / ").append(dispatchCounter == 0 ? 0 : dispatchDuration / dispatchCounter).append(" ms\n")
126                 .append("deliver: ").append(deliverCounter).append(" / ").append(deliverCounter == 0 ? 0 : deliverDuration / deliverCounter).append(" ms\n");
127
128         return tResult.toString();
129     }
130 }
131
Popular Tags