ResponseStatistics.java
6.74 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.provider.ProcessingStatus
* com.scene7.is.provider.Response
* com.scene7.is.provider.ResponseData
* com.scene7.is.util.MimeTypeEnum
* com.scene7.is.util.ServerCacheUseEnum
* com.scene7.is.util.Statistics
* com.scene7.is.util.StatisticsSnapshot
* com.scene7.is.util.collections.CollectionUtil
* org.jetbrains.annotations.NotNull
* org.jetbrains.annotations.Nullable
*/
package com.scene7.is.ps.provider;
import com.scene7.is.provider.ProcessingStatus;
import com.scene7.is.provider.Response;
import com.scene7.is.provider.ResponseData;
import com.scene7.is.util.MimeTypeEnum;
import com.scene7.is.util.ServerCacheUseEnum;
import com.scene7.is.util.Statistics;
import com.scene7.is.util.StatisticsSnapshot;
import com.scene7.is.util.collections.CollectionUtil;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ResponseStatistics
implements Statistics {
private static final long STARTUP_TIME = System.currentTimeMillis();
private long idleStartTime;
private long startTime;
private int enterCount;
private long totalIdleTime;
private int totalRequestsServed;
private long maxRequestTime;
private long totalRequestTime;
private long totalBytesServed;
private long maxBytesServed;
private int maxRequestOverlap;
@NotNull
private final Map<Integer, Long> statusCounters = new HashMap<Integer, Long>();
@NotNull
private final Map<MimeTypeEnum, Long> mimeTypeCounters = CollectionUtil.enumMap(MimeTypeEnum.class);
@NotNull
private final Map<ServerCacheUseEnum, Long> cacheUseCounters = CollectionUtil.enumMap(ServerCacheUseEnum.class);
@NotNull
private final String name;
@NotNull
private final String version;
@NotNull
private final String revision;
@NotNull
private final String buildNumber;
@NotNull
private final String buildDate;
public ResponseStatistics(@NotNull String name, @NotNull String version, @NotNull String revision, @NotNull String buildNumber, @NotNull String buildDate) {
this.name = name;
this.version = version;
this.revision = revision;
this.buildNumber = buildNumber;
this.buildDate = buildDate;
this.reset();
}
public static long getStartupTime() {
return STARTUP_TIME;
}
public static long getUpTime() {
return System.currentTimeMillis() - STARTUP_TIME;
}
public synchronized void enter() {
if (this.enterCount == 0) {
this.totalIdleTime += System.currentTimeMillis() - this.idleStartTime;
}
++this.enterCount;
if (this.enterCount > this.maxRequestOverlap) {
this.maxRequestOverlap = this.enterCount;
}
}
public synchronized void leave(@Nullable Response response, @Nullable ProcessingStatus status) {
--this.enterCount;
if (this.enterCount == 0) {
this.idleStartTime = System.currentTimeMillis();
}
++this.totalRequestsServed;
if (status != null && status.getFetchedTime() != 0) {
this.countRequestTime(status.getFetchedTime() - status.getCreatedTime());
}
if (response != null) {
this.countBytesServed(response.getData().getSize());
ResponseStatistics.updateDistribution(this.statusCounters, response.getStatus());
MimeTypeEnum mimeType = response.getData().getMimeType();
if (mimeType != null) {
ResponseStatistics.updateDistribution(this.mimeTypeCounters, mimeType);
}
ResponseStatistics.updateDistribution(this.cacheUseCounters, response.getServerCacheUse());
}
}
public synchronized void leave(long bytesServed, @Nullable ProcessingStatus status) {
--this.enterCount;
if (this.enterCount == 0) {
this.idleStartTime = System.currentTimeMillis();
}
++this.totalRequestsServed;
if (status != null && status.getFetchedTime() != 0) {
this.countRequestTime(status.getFetchedTime() - status.getCreatedTime());
}
this.countBytesServed(bytesServed);
}
@NotNull
public String getName() {
return this.name;
}
@NotNull
public String getVersion() {
return this.version;
}
@NotNull
public String getRevision() {
return this.revision;
}
@NotNull
public String getBuildNumber() {
return this.buildNumber;
}
@NotNull
public String getBuildDate() {
return this.buildDate;
}
public synchronized int getRequestOverlap() {
return this.enterCount;
}
@NotNull
public StatisticsSnapshot getSnapshot(boolean resetStats) {
return this.getSnapshot(System.currentTimeMillis(), resetStats);
}
@NotNull
public synchronized StatisticsSnapshot getSnapshot(long currentTime, boolean resetStats) {
long upTime = currentTime - STARTUP_TIME;
long elapsedTime = currentTime - this.startTime;
long totalIdleTime = this.totalIdleTime;
if (this.enterCount == 0) {
totalIdleTime += currentTime - this.idleStartTime;
}
StatisticsSnapshot snapshot = new StatisticsSnapshot(upTime, elapsedTime, this.maxBytesServed, this.totalBytesServed, this.maxRequestTime, this.totalRequestTime, totalIdleTime, this.totalRequestsServed, this.maxRequestOverlap, this.statusCounters, this.mimeTypeCounters, this.cacheUseCounters);
if (resetStats) {
this.reset();
}
return snapshot;
}
private void reset() {
this.idleStartTime = this.startTime = System.currentTimeMillis();
this.totalIdleTime = 0;
this.totalRequestsServed = 0;
this.maxRequestTime = 0;
this.totalRequestTime = 0;
this.totalBytesServed = 0;
this.maxBytesServed = 0;
this.maxRequestOverlap = 0;
this.statusCounters.clear();
this.mimeTypeCounters.clear();
this.cacheUseCounters.clear();
}
private void countRequestTime(long time) {
if (time > this.maxRequestTime) {
this.maxRequestTime = time;
}
this.totalRequestTime += time;
}
private void countBytesServed(long bytesServed) {
if (this.maxBytesServed < bytesServed) {
this.maxBytesServed = bytesServed;
}
this.totalBytesServed += bytesServed;
}
private static <T> void updateDistribution(@NotNull Map<T, Long> counters, @NotNull T object) {
Long counter = counters.get(object);
counter = counter == null ? Long.valueOf(1) : Long.valueOf(counter + 1);
counters.put(object, counter);
}
}