Skip to content

Commit

Permalink
#168 Added avg request time
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkubiak committed May 31, 2016
1 parent a937121 commit 633cec0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ private Response metrics() {
.andContent(METRICS, metrics.getMetrics())
.andContent("totalRequests", totalRequests)
.andContent("minRequestTime", metrics.getMinRequestTime())
.andContent("avgRequestTime", metrics.getAvgRequestTime())
.andContent("maxRequestTime", metrics.getMaxRequestTime())
.andContent("errorRate", errorRate)
.andTemplate(Template.DEFAULT.metricsPath());
Expand Down
43 changes: 34 additions & 9 deletions mangooio-core/src/main/java/io/mangoo/models/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.LongAdder;

import com.google.inject.Singleton;
Expand All @@ -17,30 +18,50 @@
public class Metrics {
private final AtomicIntegerFieldUpdater<Metrics> maxRequestTimeUpdater = AtomicIntegerFieldUpdater.newUpdater(Metrics.class, "maxRequestTime");
private final AtomicIntegerFieldUpdater<Metrics> minRequestTimeUpdater = AtomicIntegerFieldUpdater.newUpdater(Metrics.class, "minRequestTime");
private final AtomicLongFieldUpdater<Metrics> totalRequestTimeUpdater = AtomicLongFieldUpdater.newUpdater(Metrics.class, "totalRequestTime");
private final AtomicLongFieldUpdater<Metrics> totalRequestsUpdater = AtomicLongFieldUpdater.newUpdater(Metrics.class, "totalRequests");
private final Map<Integer, LongAdder> metricsCount = new ConcurrentHashMap<>(16, 0.9f, 1);
private volatile int maxRequestTime;
private volatile int avgRequestTime;
private volatile long totalRequestTime;
private volatile long totalRequests;
private volatile int minRequestTime = -1;

public void inc(int responseCode) {
this.metricsCount.computeIfAbsent(responseCode, t -> new LongAdder()).increment();
}


public Metrics(){
}

public Metrics(Metrics copy) {
this.totalRequestTime = copy.totalRequestTime;
this.maxRequestTime = copy.maxRequestTime;
this.minRequestTime = copy.minRequestTime;
this.totalRequests = copy.totalRequests;
}

public void update(final int requestTime) {
int tempMaxRequestTime;
this.totalRequestTimeUpdater.addAndGet(this, requestTime);

int maxRequestTime;
do {
tempMaxRequestTime = this.maxRequestTime;
if (requestTime < tempMaxRequestTime) {
maxRequestTime = this.maxRequestTime;
if (requestTime < maxRequestTime) {
break;
}
} while (!this.maxRequestTimeUpdater.compareAndSet(this, tempMaxRequestTime, requestTime));
} while (!this.maxRequestTimeUpdater.compareAndSet(this, maxRequestTime, requestTime));

int tempMinRequestTime;
int minRequestTime;
do {
tempMinRequestTime = this.minRequestTime;
if (requestTime > tempMinRequestTime && tempMinRequestTime != -1) {
minRequestTime = this.minRequestTime;
if (requestTime > minRequestTime && minRequestTime != -1) {
break;
}
} while (!this.minRequestTimeUpdater.compareAndSet(this, tempMinRequestTime, requestTime));
} while (!this.minRequestTimeUpdater.compareAndSet(this, minRequestTime, requestTime));

this.totalRequestsUpdater.incrementAndGet(this);
this.avgRequestTime = (int) (this.totalRequestTime / this.totalRequests);
}

public Map<Integer, LongAdder> getMetrics() {
Expand All @@ -54,4 +75,8 @@ public int getMaxRequestTime() {
public int getMinRequestTime() {
return minRequestTime;
}

public int getAvgRequestTime() {
return avgRequestTime;
}
}
8 changes: 8 additions & 0 deletions mangooio-core/src/main/resources/templates/admin/metrics.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
</div>
</div>
</div>
<div class="col-lg-3 col-xs-6">
<div class="small-box bg-aqua">
<div class="inner">
<h3>${avgRequestTime} ms</h3>
<p>Avg request time</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.mangoo.managers.ExecutionManager;
import io.mangoo.managers.ServerEventManager;
import io.mangoo.managers.WebSocketManager;
import io.mangoo.models.Metrics;
import io.mangoo.routing.Response;
import io.mangoo.routing.bindings.Authentication;
import io.mangoo.routing.bindings.Flash;
Expand Down Expand Up @@ -48,6 +49,7 @@ public void testInjection() {
WebSocketManager webSocketManager = Application.getInstance(WebSocketManager.class);
ServerEventManager serverEventManager = Application.getInstance(ServerEventManager.class);
MetricsListener metricsListener = Application.getInstance(MetricsListener.class);
Metrics metrics = Application.getInstance(Metrics.class);

//then
assertThat(form, not(nullValue()));
Expand All @@ -65,5 +67,6 @@ public void testInjection() {
assertThat(webSocketManager, not(nullValue()));
assertThat(serverEventManager, not(nullValue()));
assertThat(metricsListener, not(nullValue()));
assertThat(metrics, not(nullValue()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.mangoo.models;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;
Expand Down Expand Up @@ -29,4 +31,19 @@ public void testIncrement() {
assertThat(metrics.getMetrics().get(418).intValue(), equalTo(2));
assertThat(metrics.getMetrics().get(420).intValue(), equalTo(1));
}

@Test
public void testUpdate() {
//given
Metrics metrics = Application.getInstance(Metrics.class);

//when
metrics.update(2000);
metrics.update(1000);

//then
assertThat(metrics.getMaxRequestTime(), equalTo(2000));
assertThat(metrics.getMinRequestTime(), lessThan(1000));
assertThat(metrics.getAvgRequestTime(), lessThan(1000));
}
}

0 comments on commit 633cec0

Please sign in to comment.