#1875 - Refactor simplify MetaTimedMetric removing unrequired getStartTime() and getBeanCount()

This commit is contained in:
rob bygrave
2019-11-29 23:42:00 +13:00
parent a04594540c
commit e093675444
27 changed files with 35 additions and 150 deletions
@@ -11,11 +11,6 @@ public interface MetaTimedMetric extends MetaMetric {
*/
String getLocation();
/**
* Return the time the counters started from.
*/
long getStartTime();
/**
* Return the total count.
*/
@@ -35,11 +30,4 @@ public interface MetaTimedMetric extends MetaMetric {
* Return the mean execution time in micros.
*/
long getMean();
/**
* Return the total beans or rows processed or loaded.
* <p>
* This will be 0 if the metric isn't a query plan (like transaction execution statistics).
*/
long getBeanCount();
}
@@ -12,11 +12,6 @@ public interface TimedMetric {
*/
void add(long micros);
/**
* Add a time event with the number of loaded beans or rows.
*/
void add(long micros, long beans);
/**
* Add a time event for a batch of beans.
*/
@@ -27,11 +22,6 @@ public interface TimedMetric {
*/
void addSinceNanos(long startNanos);
/**
* Add a time event given the start nanos and bean count.
*/
void addSinceNanos(long startNanos, long beans);
/**
* Return true if there are no metrics collected since the last collection.
*/
@@ -12,21 +12,11 @@ public interface TimedMetricMap {
*/
void addSinceNanos(String key, long startNanos);
/**
* Add a time event given the start nanos and beans.
*/
void addSinceNanos(String key, long startNanos, int beans);
/**
* Add an execution for the given key.
*/
void add(String key, long exeMicros);
/**
* Add an execution for the given key including row/bean count.
*/
void add(String key, long exeMicros, int rows);
/**
* Visit the metric.
*/
@@ -31,8 +31,6 @@ public final class DtoQueryRequest<T> extends AbstractSqlQueryRequest {
private DataReader dataReader;
private int beanCount;
DtoQueryRequest(SpiEbeanServer server, DtoQueryEngine engine, SpiDtoQuery<T> query) {
super(server, query, null);
this.queryEngine = engine;
@@ -85,7 +83,7 @@ public final class DtoQueryRequest<T> extends AbstractSqlQueryRequest {
protected void requestComplete() {
if (plan != null) {
long exeMicros = (System.nanoTime() - startNano) / 1000L;
plan.collect(exeMicros, beanCount);
plan.collect(exeMicros);
}
}
@@ -103,7 +101,6 @@ public final class DtoQueryRequest<T> extends AbstractSqlQueryRequest {
@SuppressWarnings("unchecked")
public T readNextBean() throws SQLException {
beanCount++;
dataReader.resetColumnPosition();
return (T)plan.readRow(dataReader);
}
@@ -88,7 +88,7 @@ public final class PersistRequestCallableSql extends PersistRequest {
@Override
public void postExecute() {
if (startNanos > 0) {
persistExecute.collectSqlCall(label, startNanos, rowCount);
persistExecute.collectSqlCall(label, startNanos);
}
if (transaction.isLogSummary()) {
String m = "CallableSql label[" + callableSql.getLabel() + "]" + " rows[" + rowCount + "]" + " bind[" + bindLog + "]";
@@ -87,7 +87,7 @@ public final class PersistRequestOrmUpdate extends PersistRequest {
@Override
public void postExecute() {
if (startNanos > 0) {
persistExecute.collectOrmUpdate(label, startNanos, rowCount);
persistExecute.collectOrmUpdate(label, startNanos);
}
OrmUpdateType ormUpdateType = ormUpdate.getOrmUpdateType();
String tableName = ormUpdate.getBaseTable();
@@ -165,7 +165,7 @@ public final class PersistRequestUpdateSql extends PersistRequest {
@Override
public void postExecute() {
if (startNanos > 0) {
persistExecute.collectSqlUpdate(label, startNanos, rowCount);
persistExecute.collectSqlUpdate(label, startNanos);
}
if (transaction.isLogSql() && !batchThisRequest) {
transaction.logSql(Str.add(TrimLogSql.trim(updateSql.getGeneratedSql()), "; -- bind(", bindLog, ") rows(", String.valueOf(rowCount), ")"));
@@ -60,7 +60,7 @@ public interface RelationalQueryEngine {
/**
* Collect SQL query execution statistics.
*/
void collect(String label, long exeMicros, int rows);
void collect(String label, long exeMicros);
/**
* Visit the metrics.
@@ -51,7 +51,7 @@ public final class RelationalQueryRequest extends AbstractSqlQueryRequest {
String label = query.getLabel();
if (label != null) {
long exeMicros = (System.nanoTime() - startNano) / 1000L;
queryEngine.collect(label, exeMicros, rows);
queryEngine.collect(label, exeMicros);
}
}
@@ -18,7 +18,7 @@ public interface DtoQueryPlan {
/**
* Add an event to the query execution statistics.
*/
void collect(long exeMicros, int rows);
void collect(long exeMicros);
/**
* Visit the metric (if not empty).
@@ -16,8 +16,8 @@ abstract class DtoQueryPlanBase implements DtoQueryPlan {
}
@Override
public void collect(long exeTime, int rows) {
metric.add(exeTime, rows);
public void collect(long exeTime) {
metric.add(exeTime);
}
@Override
@@ -55,21 +55,18 @@ final class DefaultPersistExecute implements PersistExecute {
}
@Override
public void collectOrmUpdate(String label, long startNanos, int rowCount) {
long exeMicros = (System.nanoTime() - startNanos) / 1000L;
ormUpdateMetric.add(label, exeMicros, rowCount);
public void collectOrmUpdate(String label, long startNanos) {
ormUpdateMetric.addSinceNanos(label, startNanos);
}
@Override
public void collectSqlUpdate(String label, long startNanos, int rowCount) {
long exeMicros = (System.nanoTime() - startNanos) / 1000L;
sqlUpdateMetric.add(label, exeMicros, rowCount);
public void collectSqlUpdate(String label, long startNanos) {
sqlUpdateMetric.addSinceNanos(label, startNanos);
}
@Override
public void collectSqlCall(String label, long startNanos, int rowCount) {
long exeMicros = (System.nanoTime() - startNanos) / 1000L;
sqlCallMetric.add(label, exeMicros, rowCount);
public void collectSqlCall(String label, long startNanos) {
sqlCallMetric.addSinceNanos(label, startNanos);
}
@Override
@@ -38,17 +38,17 @@ public interface PersistExecute {
/**
* Collect execution metrics for sql update.
*/
void collectOrmUpdate(String label, long startNanos, int rowCount);
void collectOrmUpdate(String label, long startNanos);
/**
* Collect execution metrics for sql update.
*/
void collectSqlUpdate(String label, long startNanos, int rowCount);
void collectSqlUpdate(String label, long startNanos);
/**
* Collect execution metrics for sql callable.
*/
void collectSqlCall(String label, long startNanos, int rowCount);
void collectSqlCall(String label, long startNanos);
/**
* Visit the metrics.
@@ -89,11 +89,6 @@ class DQueryPlanMetric implements QueryPlanMetric {
return meta.getLocation();
}
@Override
public long getStartTime() {
return stats.getStartTime();
}
@Override
public long getCount() {
return stats.getCount();
@@ -113,10 +108,5 @@ class DQueryPlanMetric implements QueryPlanMetric {
public long getMean() {
return stats.getMean();
}
@Override
public long getBeanCount() {
return stats.getBeanCount();
}
}
}
@@ -14,26 +14,20 @@ class DTimeMetricStats implements TimedMetricStats {
private String location;
private final long startTime;
private final long count;
private final long total;
private final long max;
private final long beanCount;
DTimeMetricStats(MetricType metricType, String name, long collectionStart, long count, long total, long max, long beanCount) {
DTimeMetricStats(MetricType metricType, String name, long count, long total, long max) {
this.metricType = metricType;
this.name = name;
this.startTime = collectionStart;
this.count = count;
this.total = total;
// collection is racy so sanitize the max value if it has not been set
// this most likely would happen when count = 1 so max = mean
this.max = max != Long.MIN_VALUE ? max : (count < 1 ? 0 : Math.round(total / count));
this.beanCount = beanCount;
}
@Override
@@ -76,14 +70,6 @@ class DTimeMetricStats implements TimedMetricStats {
return location;
}
/**
* Return the time the counter started statistics collection.
*/
@Override
public long getStartTime() {
return startTime;
}
/**
* Return the count of values collected.
*/
@@ -116,8 +102,4 @@ class DTimeMetricStats implements TimedMetricStats {
return (count < 1) ? 0L : Math.round((double)(total / count));
}
@Override
public long getBeanCount() {
return beanCount;
}
}
@@ -4,7 +4,6 @@ import io.ebean.meta.MetricType;
import io.ebean.meta.MetricVisitor;
import io.ebean.metric.TimedMetric;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
@@ -20,16 +19,12 @@ class DTimedMetric implements TimedMetric {
private final String name;
private final LongAdder beanCount = new LongAdder();
private final LongAdder count = new LongAdder();
private final LongAdder total = new LongAdder();
private final LongAccumulator max = new LongAccumulator(Math::max, Long.MIN_VALUE);
private final AtomicLong startTime = new AtomicLong(System.currentTimeMillis());
DTimedMetric(MetricType metricType, String name) {
this.metricType = metricType;
this.name = name;
@@ -51,11 +46,6 @@ class DTimedMetric implements TimedMetric {
add((System.nanoTime() - startNanos) / 1000L);
}
@Override
public void addSinceNanos(long startNanos, long beans) {
add((System.nanoTime() - startNanos) / 1000L, beans);
}
/**
* Add a value. Usually the value is Time or Bytes etc.
*/
@@ -67,12 +57,6 @@ class DTimedMetric implements TimedMetric {
max.accumulate(value);
}
@Override
public void add(long micros, long beans) {
add(micros);
beanCount.add(beans);
}
@Override
public boolean isEmpty() {
return count.sum() == 0;
@@ -83,11 +67,9 @@ class DTimedMetric implements TimedMetric {
*/
@Override
public void reset() {
startTime.set(System.currentTimeMillis());
max.reset();
count.reset();
total.reset();
beanCount.reset();
}
@Override
@@ -101,14 +83,7 @@ class DTimedMetric implements TimedMetric {
@Override
public DTimeMetricStats collect(boolean reset) {
boolean empty = count.sum() == 0;
if (empty) {
if (reset) {
startTime.set(System.currentTimeMillis());
}
return null;
} else {
return getStatistics(reset);
}
return empty ? null : getStatistics(reset);
}
/**
@@ -119,15 +94,13 @@ class DTimedMetric implements TimedMetric {
if (reset) {
// Note these values are not guaranteed to be consistent wrt each other
// but should be reasonably consistent (small time between count and total)
final long beans = beanCount.sumThenReset();
final long maxVal = max.getThenReset();
final long totalVal = total.sumThenReset();
final long countVal = count.sumThenReset();
final long startTimeVal = startTime.getAndSet(System.currentTimeMillis());
return new DTimeMetricStats(metricType, name, startTimeVal, countVal, totalVal, maxVal, beans);
return new DTimeMetricStats(metricType, name, countVal, totalVal, maxVal);
} else {
return new DTimeMetricStats(metricType, name, startTime.get(), count.sum(), total.sum(), max.get(), beanCount.sum());
return new DTimeMetricStats(metricType, name, count.sum(), total.sum(), max.get());
}
}
@@ -24,21 +24,11 @@ class DTimedMetricMap implements TimedMetricMap {
add(key, (System.nanoTime() - startNanos)/1000L);
}
@Override
public void addSinceNanos(String key, long startNanos, int beans) {
add(key, (System.nanoTime() - startNanos)/1000L, beans);
}
@Override
public void add(String key, long exeMicros) {
map.computeIfAbsent(key, (k) -> new DTimedMetric(metricType, name + key)).add(exeMicros);
}
@Override
public void add(String key, long exeMicros, int rows) {
map.computeIfAbsent(key, (k) -> new DTimedMetric(metricType, name + key)).add(exeMicros, rows);
}
@Override
public void visit(MetricVisitor visitor) {
for (DTimedMetric value : map.values()) {
@@ -601,7 +601,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery, SpiProfileTran
if (autoTuneProfiling) {
profilingListener.collectQueryInfo(objectGraphNode, loadedBeanCount, executionTimeMicros);
}
if (queryPlan.executionTime(loadedBeanCount, executionTimeMicros)) {
if (queryPlan.executionTime(executionTimeMicros)) {
queryPlan.captureBindForQueryPlan(predicates, executionTimeMicros);
}
getTransaction().profileEvent(this);
@@ -117,7 +117,7 @@ class CQueryFetchSingleAttribute implements SpiProfileTransactionEvent {
executionTimeMicros = (System.nanoTime() - startNano) / 1000L;
request.slowQueryCheck(executionTimeMicros, rowCount);
if (queryPlan.executionTime(rowCount, executionTimeMicros)) {
if (queryPlan.executionTime(executionTimeMicros)) {
queryPlan.captureBindForQueryPlan(predicates, executionTimeMicros);
}
getTransaction().profileEvent(this);
@@ -312,8 +312,8 @@ public class CQueryPlan {
/**
* Register an execution time against this query plan;
*/
boolean executionTime(long loadedBeanCount, long timeMicros) {
stats.add(loadedBeanCount, timeMicros);
boolean executionTime(long timeMicros) {
stats.add(timeMicros);
return bindCapture != null && bindCapture.collectFor(timeMicros);
}
@@ -36,8 +36,8 @@ public final class CQueryPlanStats {
/**
* Add a query execution to the statistics.
*/
public void add(long loadedBeanCount, long timeMicros) {
timedMetric.add(timeMicros, loadedBeanCount);
public void add(long timeMicros) {
timedMetric.add(timeMicros);
// not safe but should be atomic
lastQueryTime = System.currentTimeMillis();
}
@@ -112,11 +112,6 @@ public final class CQueryPlanStats {
return queryPlan.getLocation();
}
@Override
public long getBeanCount() {
return metrics.getBeanCount();
}
@Override
public long getCount() {
return metrics.getCount();
@@ -137,11 +132,6 @@ public final class CQueryPlanStats {
return metrics.getMean();
}
@Override
public long getStartTime() {
return metrics.getStartTime();
}
@Override
public String getHash() {
return queryPlan.getHash();
@@ -126,7 +126,7 @@ class CQueryRowCount implements SpiProfileTransactionEvent {
executionTimeMicros = (System.nanoTime() - startNano) / 1000L;
request.slowQueryCheck(executionTimeMicros, rowCount);
if (queryPlan.executionTime(rowCount, executionTimeMicros)) {
if (queryPlan.executionTime(executionTimeMicros)) {
queryPlan.captureBindForQueryPlan(predicates, executionTimeMicros);
}
t.profileEvent(this);
@@ -93,7 +93,7 @@ class CQueryUpdate implements SpiProfileTransactionEvent {
long executionTimeMicros = (System.nanoTime() - startNano) / 1000L;
request.slowQueryCheck(executionTimeMicros, rowCount);
if (queryPlan.executionTime(rowCount, executionTimeMicros)) {
if (queryPlan.executionTime(executionTimeMicros)) {
queryPlan.captureBindForQueryPlan(predicates, executionTimeMicros);
}
t.profileEvent(this);
@@ -42,8 +42,8 @@ public class DefaultRelationalQueryEngine implements RelationalQueryEngine {
}
@Override
public void collect(String label, long exeMicros, int rows) {
timedMetricMap.add(label, exeMicros, rows);
public void collect(String label, long exeMicros) {
timedMetricMap.add(label, exeMicros);
}
@Override