mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-17 22:56:28 +00:00
ENH: Add ProfileLocation - Part 2 - performance metrics for transactions with label and profileLocation
This commit is contained in:
1 parent
4e33027fdf
commit
51d2e4c600
37 files changed
+779
-33
No files matched your search
@@ -19,6 +19,11 @@ class BasicProfileLocation implements ProfileLocation {
|
||||
return shortDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(long executionTime) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public String obtain() {
|
||||
return location;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebeaninternal.metric.MetricFactory;
|
||||
import io.ebeaninternal.metric.TimedMetric;
|
||||
import io.ebeaninternal.metric.TimedMetricMap;
|
||||
|
||||
/**
|
||||
* Default metric factory implementation.
|
||||
*/
|
||||
public class DMetricFactory implements MetricFactory {
|
||||
|
||||
@Override
|
||||
public TimedMetricMap createTimedMetricMap(String name) {
|
||||
return new DTimedMetricMap(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimedMetric createTimedMetric(String name) {
|
||||
return new DTimedMetric(name);
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,12 @@ class DProfileLocation implements ProfileLocation {
|
||||
private final int lineNumber;
|
||||
|
||||
DProfileLocation() {
|
||||
this.lineNumber = 0;
|
||||
this(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create with a given line number.
|
||||
*/
|
||||
DProfileLocation(int lineNumber) {
|
||||
this.lineNumber = lineNumber;
|
||||
}
|
||||
@@ -29,6 +32,11 @@ class DProfileLocation implements ProfileLocation {
|
||||
return "location: " + location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(long executionTime) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public String obtain() {
|
||||
// atomic assignment so happy with this
|
||||
if (location == null) {
|
||||
|
||||
@@ -2,6 +2,8 @@ package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.ProfileLocation;
|
||||
import io.ebean.service.SpiProfileLocationFactory;
|
||||
import io.ebeaninternal.metric.MetricFactory;
|
||||
import io.ebeaninternal.metric.TimedMetric;
|
||||
|
||||
/**
|
||||
* Default implementation of the profile location factory.
|
||||
@@ -14,12 +16,17 @@ public class DProfileLocationFactory implements SpiProfileLocationFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProfileLocation create(int lineNumber) {
|
||||
return new DProfileLocation(lineNumber);
|
||||
public ProfileLocation create(int lineNumber, String label) {
|
||||
|
||||
TimedMetric timedMetric = MetricFactory.get().createTimedMetric("txn.named." + label);
|
||||
|
||||
DTimedProfileLocation loc = new DTimedProfileLocation(lineNumber, label, timedMetric);
|
||||
TimedProfileLocationRegistry.register(loc);
|
||||
return loc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProfileLocation create(String location) {
|
||||
public ProfileLocation createAt(String location) {
|
||||
return new BasicProfileLocation(location);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebeaninternal.metric.TimedMetricStats;
|
||||
|
||||
/**
|
||||
* Snapshot of the current statistics for a Counter or TimeCounter.
|
||||
*/
|
||||
class DTimeMetricStats implements TimedMetricStats {
|
||||
|
||||
private final String name;
|
||||
|
||||
private String location;
|
||||
|
||||
private final long startTime;
|
||||
|
||||
private final long count;
|
||||
|
||||
private final long total;
|
||||
|
||||
private final long max;
|
||||
|
||||
DTimeMetricStats(String name, long collectionStart, long count, long total, long max) {
|
||||
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));
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (location != null) {
|
||||
sb.append("loc:").append(location).append(" ");
|
||||
}
|
||||
if (name != null) {
|
||||
sb.append("name:").append(name).append(" ");
|
||||
}
|
||||
sb.append("count:").append(count)
|
||||
.append(" total:").append(total)
|
||||
.append(" max:").append(max);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time the counter started statistics collection.
|
||||
*/
|
||||
@Override
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the count of values collected.
|
||||
*/
|
||||
@Override
|
||||
public long getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total of all the values.
|
||||
*/
|
||||
@Override
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Max value collected.
|
||||
*/
|
||||
@Override
|
||||
public long getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the mean value rounded up.
|
||||
*/
|
||||
@Override
|
||||
public long getMean() {
|
||||
return (count < 1) ? 0L : Math.round((double)(total / count));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebeaninternal.metric.TimedMetric;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.LongAccumulator;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
|
||||
/**
|
||||
* Used to collect timed execution statistics.
|
||||
* <p>
|
||||
* It is intended for high concurrent updates to the statistics and relatively infrequent reads.
|
||||
* </p>
|
||||
*/
|
||||
class DTimedMetric implements TimedMetric {
|
||||
|
||||
protected final String name;
|
||||
|
||||
protected final LongAdder count = new LongAdder();
|
||||
|
||||
protected final LongAdder total = new LongAdder();
|
||||
|
||||
protected final LongAccumulator max = new LongAccumulator(Math::max, Long.MIN_VALUE);
|
||||
|
||||
protected final AtomicLong startTime = new AtomicLong(System.currentTimeMillis());
|
||||
|
||||
DTimedMetric(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value. Usually the value is Time or Bytes etc.
|
||||
*/
|
||||
@Override
|
||||
public void add(long value) {
|
||||
|
||||
count.increment();
|
||||
total.add(value);
|
||||
max.accumulate(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return count.sum() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collect(boolean reset, List<MetaTimedMetric> result) {
|
||||
DTimeMetricStats metric = collect(reset);
|
||||
if (metric != null) {
|
||||
result.add(metric);
|
||||
}
|
||||
}
|
||||
|
||||
// @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 the current statistics resetting the internal values if reset is true.
|
||||
*/
|
||||
public DTimeMetricStats getStatistics(boolean reset) {
|
||||
|
||||
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 maxVal = max.getThenReset();
|
||||
final long totalVal = total.sumThenReset();
|
||||
final long countVal = count.sumThenReset();
|
||||
final long startTimeVal = startTime.getAndSet(System.currentTimeMillis());
|
||||
return new DTimeMetricStats(name, startTimeVal, countVal, totalVal, maxVal);
|
||||
|
||||
} else {
|
||||
return new DTimeMetricStats(name, startTime.get(), count.sum(), total.sum(), max.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all the internal counters and start time.
|
||||
*/
|
||||
public void reset() {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
max.reset();
|
||||
count.reset();
|
||||
total.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the start time.
|
||||
*/
|
||||
public long getStartTime() {
|
||||
return startTime.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the count of values.
|
||||
*/
|
||||
public long getCount() {
|
||||
return count.sum();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total of values.
|
||||
*/
|
||||
public long getTotal() {
|
||||
return total.sum();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the max value.
|
||||
*/
|
||||
public long getMax() {
|
||||
return max.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebeaninternal.metric.TimedMetricMap;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
class DTimedMetricMap implements TimedMetricMap {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final ConcurrentHashMap<String, DTimedMetric> map = new ConcurrentHashMap<>();
|
||||
|
||||
DTimedMetricMap(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(String key, long exeMicros) {
|
||||
map.computeIfAbsent(key, (k)-> new DTimedMetric(name + key)).add(exeMicros);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collect(boolean reset, List<MetaTimedMetric> list) {
|
||||
for (DTimedMetric value : map.values()) {
|
||||
value.collect(reset, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebeaninternal.metric.TimedMetric;
|
||||
import io.ebeaninternal.metric.TimedMetricStats;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Default profile location that uses stack trace.
|
||||
*/
|
||||
class DTimedProfileLocation extends DProfileLocation implements TimedProfileLocation {
|
||||
|
||||
private final String label;
|
||||
|
||||
private final TimedMetric timedMetric;
|
||||
|
||||
DTimedProfileLocation(int lineNumber, String label, TimedMetric timedMetric) {
|
||||
super(lineNumber);
|
||||
this.label = label;
|
||||
this.timedMetric = timedMetric;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimedMetric getMetric() {
|
||||
return timedMetric;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(long executionTime) {
|
||||
timedMetric.add(executionTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collect(boolean reset, List<MetaTimedMetric> list) {
|
||||
|
||||
TimedMetricStats collect = timedMetric.collect(reset);
|
||||
if (collect != null) {
|
||||
collect.setLocation(obtain());
|
||||
list.add(collect);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.ProfileLocation;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebeaninternal.metric.TimedMetric;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ProfileLocation that collects timing metrics.
|
||||
*/
|
||||
public interface TimedProfileLocation extends ProfileLocation {
|
||||
|
||||
/**
|
||||
* Return the label.
|
||||
*/
|
||||
String getLabel();
|
||||
|
||||
/**
|
||||
* Return the metric.
|
||||
*/
|
||||
TimedMetric getMetric();
|
||||
|
||||
/**
|
||||
* Collect the metrics adding to the given list if the metrics are non empty.
|
||||
*/
|
||||
void collect(boolean reset, List<MetaTimedMetric> list);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Global registry of the TimedProfileLocation instances created.
|
||||
*/
|
||||
public class TimedProfileLocationRegistry {
|
||||
|
||||
private static final List<TimedProfileLocation> list = Collections.synchronizedList(new ArrayList<TimedProfileLocation>());
|
||||
|
||||
/**
|
||||
* Register the timed profile location instance.
|
||||
*/
|
||||
public static void register(TimedProfileLocation location) {
|
||||
list.add(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the registered timed locations.
|
||||
*/
|
||||
public static List<TimedProfileLocation> registered() {
|
||||
return list;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user