Skip to content

Commit

Permalink
improved WebURIStat. for issue #1816
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 12, 2017
1 parent efccb6c commit 31aa93a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/alibaba/druid/support/http/stat/WebURIStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class WebURIStat {
private volatile int concurrentMax;
private volatile long requestCount;
private volatile long requestTimeNano;
private volatile long requestTimeNanoMax;
private volatile long requestTimeNanoMaxOccurTime;

final static AtomicIntegerFieldUpdater<WebURIStat> runningCountUpdater = AtomicIntegerFieldUpdater.newUpdater(WebURIStat.class,
"runningCount");
final static AtomicIntegerFieldUpdater<WebURIStat> concurrentMaxUpdater = AtomicIntegerFieldUpdater.newUpdater(WebURIStat.class,
Expand All @@ -40,6 +43,11 @@ public class WebURIStat {
"requestCount");
final static AtomicLongFieldUpdater<WebURIStat> requestTimeNanoUpdater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"requestTimeNano");
final static AtomicLongFieldUpdater<WebURIStat> requestTimeNanoMaxUpdater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"requestTimeNanoMax");

final static AtomicLongFieldUpdater<WebURIStat> requestTimeNanoMaxOccurTimeUpdater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"requestTimeNanoMaxOccurTime");

private volatile long jdbcFetchRowCount;
private volatile long jdbcFetchRowPeak; // 单次请求读取行数的峰值
Expand Down Expand Up @@ -169,6 +177,22 @@ public void afterInvoke(Throwable error, long nanos) {
runningCountUpdater.decrementAndGet(this);
requestTimeNanoUpdater.addAndGet(this, nanos);

for (;;) {
long current = requestTimeNanoUpdater.get(this);
if (current < nanos) {
if (requestTimeNanoUpdater.compareAndSet(this, current, nanos)) {
// 可能不准确,但是绝大多数情况下都会正确,性能换取一致性
requestTimeNanoMaxOccurTime = System.currentTimeMillis();

break;
} else {
continue;
}
} else {
break;
}
}

histogramRecord(nanos);

if (error != null) {
Expand Down Expand Up @@ -435,6 +459,8 @@ public WebURIStatValue getValue(boolean reset) {
val.setConcurrentMax(get(this, concurrentMaxUpdater, reset));
val.setRequestCount(get(this, requestCountUpdater, reset));
val.setRequestTimeNano(get(this, requestTimeNanoUpdater, reset));
val.setRequestTimeNanoMax(get(this, requestTimeNanoMaxUpdater, reset));
val.setRequestTimeNanoMaxOccurTime(get(this, requestTimeNanoMaxOccurTimeUpdater, reset));

val.setJdbcFetchRowCount(get(this, jdbcFetchRowCountUpdater, reset));
val.setJdbcFetchRowPeak(get(this, jdbcFetchRowPeakUpdater, reset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class WebURIStatValue {
@MField(aggregate = AggregateType.Sum)
protected long requestTimeNano;

@MField(aggregate = AggregateType.Max)
protected long requestTimeNanoMax;

@MField(aggregate = AggregateType.Last)
protected Date requestTimeNanoMaxOccurTime;

@MField(aggregate = AggregateType.Sum)
protected long jdbcFetchRowCount;

Expand Down Expand Up @@ -177,6 +183,26 @@ public void setRequestTimeNano(long requestTimeNano) {
this.requestTimeNano = requestTimeNano;
}

public long getRequestTimeNanoMax() {
return requestTimeNanoMax;
}

public void setRequestTimeNanoMax(long requestTimeNanoMax) {
this.requestTimeNanoMax = requestTimeNanoMax;
}

public Date getRequestTimeNanoMaxOccurTime() {
return requestTimeNanoMaxOccurTime;
}

public void setRequestTimeNanoMaxOccurTime(long requestTimeNanoMaxOccurTime) {
if (requestTimeNanoMaxOccurTime > 0) {
this.requestTimeNanoMaxOccurTime = new Date(requestTimeNanoMaxOccurTime);
} else {
this.requestTimeNanoMaxOccurTime = null;
}
}

public long getJdbcFetchRowCount() {
return jdbcFetchRowCount;
}
Expand Down Expand Up @@ -309,6 +335,10 @@ public long getRequestTimeMillis() {
return getRequestTimeNano() / (1000 * 1000);
}

public long getRequestTimeMillisMax() {
return getRequestTimeNanoMax() / (1000 * 1000);
}

public Date getLastAccessTime() {
return lastAccessTime;
}
Expand Down Expand Up @@ -358,6 +388,9 @@ public Map<String, Object> getStatData() {
data.put("Profiles", profileDataList);
}

data.put("RequestTimeMillisMax", this.getRequestTimeMillisMax());
data.put("RequestTimeMillisMaxOccurTime", this.getRequestTimeNanoMaxOccurTime());

return data;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/support/http/resources/weburi.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h3>
<th><a id="th-URI" class="lang" langKey="URI">URI</a></th>
<th><a id="th-RequestCount" class="lang" langKey="RequestCount">RequestCount</a></th>
<th><a id="th-RequestTimeMillis" class="lang" langKey="RequestTimeMillis">RequestTimeMillis</a></th>
<th><a id="th-RequestTimeMillisMax" class="lang" langKey="RequestTimeMillisMax">RequestTimeMillisMax</a></th>
<th><a id="th-RunningCount" class="lang" langKey="RunningCount">RunningCount</a></th>
<th><a id="th-ConcurrentMax" class="lang" langKey="ConcurrentMax">ConcurrentMax</a></th>
<th><a id="th-JdbcExecuteCount" class="lang" langKey="JdbcExecuteCount">JdbcExecuteCount</a></th>
Expand Down Expand Up @@ -124,6 +125,7 @@ <h3>
html += "<td>" + '<a target="_blank" href="weburi-detail.html?uri='+ encodeURI(stat.URI) + '">' + druid.common.subSqlString(stat.URI, 64) + '</a>' + "</td>";
html += "<td>" + replace(stat.RequestCount) + "</td>";
html += "<td>" + replace(stat.RequestTimeMillis) + "</td>";
html += "<td>" + replace(stat.RequestTimeMillisMax) + "</td>";
html += "<td>" + replace(stat.RunningCount) + "</td>";
html += "<td>" + replace(stat.ConcurrentMax) + "</td>";
html += "<td>" + replace(stat.JdbcExecuteCount) + "</td>";
Expand Down

0 comments on commit 31aa93a

Please sign in to comment.