mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 06:12:43 +00:00
#1586 - ENH: Add TimedMetric.addSinceNanos() ... as a helper when using start nanos
This commit is contained in:
@@ -17,6 +17,11 @@ public interface TimedMetric {
|
||||
*/
|
||||
void add(long micros, long beans);
|
||||
|
||||
/**
|
||||
* Add a time event given the start nanos.
|
||||
*/
|
||||
void addSinceNanos(long startNanos);
|
||||
|
||||
/**
|
||||
* Return true if there are no metrics collected since the last collection.
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,11 @@ import io.ebean.meta.MetricVisitor;
|
||||
*/
|
||||
public interface TimedMetricMap {
|
||||
|
||||
/**
|
||||
* Add a time event given the start nanos.
|
||||
*/
|
||||
void addSinceNanos(String key, long startNanos);
|
||||
|
||||
/**
|
||||
* Add an execution for the given key.
|
||||
*/
|
||||
|
||||
@@ -35,6 +35,11 @@ class DTimedMetric implements TimedMetric {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSinceNanos(long startNanos) {
|
||||
add((System.nanoTime() - startNanos) / 1000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value. Usually the value is Time or Bytes etc.
|
||||
*/
|
||||
|
||||
@@ -19,6 +19,11 @@ class DTimedMetricMap implements TimedMetricMap {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSinceNanos(String key, long startNanos) {
|
||||
add(key, (System.nanoTime() - startNanos)/1000L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(String key, long exeMicros) {
|
||||
map.computeIfAbsent(key, (k) -> new DTimedMetric(metricType, name + key)).add(exeMicros);
|
||||
|
||||
@@ -512,8 +512,7 @@ class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEventCode
|
||||
}
|
||||
connection = null;
|
||||
active = false;
|
||||
long exeMicros = (System.nanoTime() - startNanos) / 1000L;
|
||||
manager.collectMetricReadOnly(exeMicros);
|
||||
manager.collectMetricReadOnly((System.nanoTime() - startNanos) / 1000L);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.meta.BasicMetricVisitor;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebean.meta.MetricType;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DTimedMetricMapTest {
|
||||
|
||||
@Test
|
||||
public void addSinceNanos() throws InterruptedException {
|
||||
|
||||
DTimedMetricMap metricMap = new DTimedMetricMap(MetricType.L2, "addSinceNanos");
|
||||
|
||||
long nanos = System.nanoTime();
|
||||
Thread.sleep(10);
|
||||
|
||||
metricMap.addSinceNanos("some", nanos);
|
||||
|
||||
BasicMetricVisitor visitor = new BasicMetricVisitor();
|
||||
metricMap.visit(visitor);
|
||||
|
||||
MetaTimedMetric timedMetric = visitor.getTimedMetrics().get(0);
|
||||
assertThat(timedMetric.getCount()).isEqualTo(1);
|
||||
assertThat(timedMetric.getTotal()).isGreaterThan(10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.meta.MetricType;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DTimedMetricTest {
|
||||
|
||||
@Test
|
||||
public void addSinceNanos() throws InterruptedException {
|
||||
|
||||
DTimedMetric metric = new DTimedMetric(MetricType.L2, "addSinceNanos");
|
||||
|
||||
long start = System.nanoTime();
|
||||
Thread.sleep(10);
|
||||
|
||||
metric.addSinceNanos(start);
|
||||
|
||||
DTimeMetricStats stats = metric.collect(true);
|
||||
assertThat(stats.getCount()).isEqualTo(1);
|
||||
assertThat(stats.getTotal()).isGreaterThan(10);
|
||||
assertThat(stats.getMax()).isEqualTo(stats.getTotal());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user