下面列出了org.openjdk.jmh.annotations.Threads#com.netflix.spectator.api.Statistic 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static MeasurementNode createStageNode(String stage,
double count,
double totalTime,
double max) {
Id id = registry.createId("id").withTag(Statistic.count);
Measurement countMeasurement = new Measurement(id.withTag(Statistic.count), 0, count);
Measurement totalTimeMeasurement = new Measurement(id.withTag(Statistic.totalTime), 0, totalTime);
Measurement maxMeasurement = new Measurement(id.withTag(Statistic.max), 0, max);
MeasurementNode stageNode = new MeasurementNode(stage, null);
stageNode.addChild(Statistic.count.name(), countMeasurement);
stageNode.addChild(Statistic.totalTime.name(), totalTimeMeasurement);
stageNode.addChild(Statistic.max.name(), maxMeasurement);
return stageNode;
}
/**
* Create a new instance.
*/
DoubleDistributionSummary(Clock clock, Id id, long resetFreq) {
this.clock = clock;
this.id = id;
this.resetFreq = resetFreq;
lastResetTime = new AtomicLong(clock.wallTime());
lastUpdateTime = new AtomicLong(clock.wallTime());
count = new AtomicLong(0L);
totalAmount = new AtomicLong(ZERO);
totalOfSquares = new AtomicLong(ZERO);
max = new AtomicLong(ZERO);
countId = id.withTag(Statistic.count);
totalAmountId = id.withTag(Statistic.totalAmount);
totalOfSquaresId = id.withTag(Statistic.totalOfSquares);
maxId = id.withTag(Statistic.max);
}
@Test
public void aggregateCounters() {
for (int i = 0; i < 10; ++i) {
registry.counter("test", "i", "" + i).increment();
}
clock.setWallTime(5000);
List<Measurement> input = registry.measurements().collect(Collectors.toList());
List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
Assertions.assertEquals(1, aggr.size());
Measurement m = aggr.get(0);
Id id = registry.createId("test")
.withTag("atlas.dstype", "rate")
.withTag(Statistic.count);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(10.0 / 5.0, m.value(), 1e-12);
}
@Test
public void aggregateGauges() {
for (int i = 0; i < 10; ++i) {
registry.gauge("test", "i", "" + i).set(2.0);
}
clock.setWallTime(5000);
List<Measurement> input = registry.measurements().collect(Collectors.toList());
List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
Assertions.assertEquals(1, aggr.size());
Measurement m = aggr.get(0);
Id id = registry.createId("test")
.withTag("atlas.dstype", "gauge")
.withTag(Statistic.gauge);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(2.0, m.value(), 1e-12);
}
@Test
public void aggregateGaugesWithNaN() {
for (int i = 0; i < 10; ++i) {
double v = (i % 2 == 0) ? i : Double.NaN;
registry.gauge("test", "i", "" + i).set(v);
}
clock.setWallTime(5000);
List<Measurement> input = registry.measurements().collect(Collectors.toList());
List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
Assertions.assertEquals(1, aggr.size());
Measurement m = aggr.get(0);
Id id = registry.createId("test")
.withTag("atlas.dstype", "gauge")
.withTag(Statistic.gauge);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(8.0, m.value(), 1e-12);
}
@Test
public void avgRandom() {
Id id = Id.create("test");
Id measurementId = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
ManualClock clock = new ManualClock();
Counter primary = registry(clock, PRIMARY_STEP).counter(id);
Counter consolidated = registry(clock, CONSOLIDATED_STEP).counter(id);
Consolidator consolidator = new Consolidator.Avg(CONSOLIDATED_STEP, MULTIPLE);
consolidateRandomData(
measurementId,
clock,
consolidator,
primary::add,
consolidated::add,
primary::measure,
consolidated::measure);
}
@Test
public void maxRandom() {
Id id = Id.create("test");
Id measurementId = id.withTag("atlas.dstype", "gauge").withTag(Statistic.max);
ManualClock clock = new ManualClock();
Gauge primary = registry(clock, PRIMARY_STEP).maxGauge(id);
Gauge consolidated = registry(clock, CONSOLIDATED_STEP).maxGauge(id);
Consolidator consolidator = new Consolidator.Max(CONSOLIDATED_STEP, MULTIPLE);
consolidateRandomData(
measurementId,
clock,
consolidator,
primary::set,
consolidated::set,
primary::measure,
consolidated::measure);
}
@Test
public void noneRandom() {
Id id = Id.create("test");
Id measurementId = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
ManualClock clock = new ManualClock();
Counter primary = registry(clock, CONSOLIDATED_STEP).counter(id);
Counter consolidated = registry(clock, CONSOLIDATED_STEP).counter(id);
Consolidator consolidator = new Consolidator.None();
consolidateRandomData(
measurementId,
clock,
consolidator,
primary::add,
consolidated::add,
primary::measure,
consolidated::measure);
}
@Test
public void createFromStatistic() {
EnumSet<Statistic> counters = EnumSet.of(
Statistic.count,
Statistic.totalAmount,
Statistic.totalTime,
Statistic.totalOfSquares,
Statistic.percentile);
for (Statistic statistic : Statistic.values()) {
Consolidator consolidator = Consolidator.create(statistic, CONSOLIDATED_STEP, MULTIPLE);
if (counters.contains(statistic)) {
Assertions.assertTrue(consolidator instanceof Consolidator.Avg, statistic.name());
} else {
Assertions.assertTrue(consolidator instanceof Consolidator.Max, statistic.name());
}
}
}
/**
* Creates a timer for tracking long running tasks.
*
* @param registry
* Registry to use.
* @param id
* Identifier for the metric being registered.
* @return
* Timer instance.
*/
public static LongTaskTimer get(Registry registry, Id id) {
ConcurrentMap<Id, Object> state = registry.state();
Object obj = Utils.computeIfAbsent(state, id, i -> {
LongTaskTimer timer = new LongTaskTimer(registry, id);
PolledMeter.using(registry)
.withId(id)
.withTag(Statistic.activeTasks)
.monitorValue(timer, LongTaskTimer::activeTasks);
PolledMeter.using(registry)
.withId(id)
.withTag(Statistic.duration)
.monitorValue(timer, t -> t.duration() / NANOS_PER_SECOND);
return timer;
});
if (!(obj instanceof LongTaskTimer)) {
Utils.propagateTypeError(registry, id, LongTaskTimer.class, obj.getClass());
obj = new LongTaskTimer(new NoopRegistry(), id);
}
return (LongTaskTimer) obj;
}
@Test
public void testMeasure() {
Timer timer = factory.timer(factory.createId("testMeasure"));
timer.record(42, TimeUnit.MILLISECONDS);
clock.setWallTime(3712345L);
for (Measurement m : timer.measure()) {
Assertions.assertEquals(m.timestamp(), 3712345L);
if (m.id().equals(timer.id().withTag(Statistic.count))) {
Assertions.assertEquals(1.0, m.value(), 0.1e-12);
} else if (m.id().equals(timer.id().withTag(Statistic.totalTime))) {
Assertions.assertEquals(42e6, m.value(), 0.1e-12);
} else {
Assertions.fail("unexpected id: " + m.id());
}
}
}
/** Create a new instance. */
ServoTimer(ServoRegistry r, Id id) {
super(r.clock());
this.id = id;
count = new AtomicLong(0L);
totalTime = new AtomicLong(0L);
ServoClock sc = new ServoClock(clock);
servoCount = new StepCounter(r.toMonitorConfig(id.withTag(Statistic.count), null), sc);
servoTotal = new StepCounter(r.toMonitorConfig(id.withTag(Statistic.totalTime), null), sc);
servoTotalOfSquares = new DoubleCounter(
r.toMonitorConfig(id.withTag(Statistic.totalOfSquares), null), sc);
// Constructor that takes a clock param is not public
servoMax = new MaxGauge(r.toMonitorConfig(id.withTag(Statistic.max), null));
lastUpdated = new AtomicLong(clock.wallTime());
}
@Test
public void totalOfSquaresOverflow() {
final long seconds = 10;
final long nanos = TimeUnit.SECONDS.toNanos(seconds);
final BigInteger s = new BigInteger("" + nanos);
final BigInteger s2 = s.multiply(s);
Timer t = newTimer("foo");
t.record(seconds, TimeUnit.SECONDS);
clock.setWallTime(61000L);
final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();
final double factor = 1e9 * 1e9;
final BigInteger perSec = s2.divide(BigInteger.valueOf(60));
Assertions.assertEquals(perSec.doubleValue() / factor, v, 1e-12);
}
@Test
public void totalOfSquaresManySmallValues() {
Timer t = newTimer("foo");
BigInteger sumOfSq = new BigInteger("0");
for (int i = 0; i < 100000; ++i) {
final long nanos = i;
final BigInteger s = new BigInteger("" + nanos);
final BigInteger s2 = s.multiply(s);
sumOfSq = sumOfSq.add(s2);
t.record(i, TimeUnit.NANOSECONDS);
}
clock.setWallTime(61000L);
final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();
final double factor = 1e9 * 1e9;
sumOfSq = sumOfSq.divide(BigInteger.valueOf(60));
Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 1e-12);
}
@Test
public void totalOfSquaresManyBigValues() {
Timer t = newTimer("foo");
BigInteger sumOfSq = new BigInteger("0");
for (int i = 0; i < 100000; ++i) {
final long nanos = TimeUnit.SECONDS.toNanos(i);
final BigInteger s = new BigInteger("" + nanos);
final BigInteger s2 = s.multiply(s);
sumOfSq = sumOfSq.add(s2);
t.record(i, TimeUnit.SECONDS);
}
clock.setWallTime(61000L);
final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();
// Expected :3.3332833335E14
// Actual :3.3332833334999825E14
final double factor = 1e9 * 1e9;
sumOfSq = sumOfSq.divide(BigInteger.valueOf(60));
Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 2.0);
}
@Override
public double max(TimeUnit unit) {
for (Measurement measurement : timer.measure()) {
if (stream(measurement.id().tags().spliterator(), false)
.anyMatch(tag -> tag.key().equals("statistic") && tag.value().equals(Statistic.max.toString()))) {
return TimeUtils.secondsToUnit(measurement.value(), unit);
}
}
return Double.NaN;
}
@Override
public double max() {
for (Measurement measurement : summary.measure()) {
if (stream(measurement.id().tags().spliterator(), false)
.anyMatch(tag -> tag.key().equals("statistic") && tag.value().equals(Statistic.max.toString()))) {
return measurement.value();
}
}
return Double.NaN;
}
public static PerfInfo createPerfInfo(MeasurementNode stageNode) {
PerfInfo perfInfo = new PerfInfo();
perfInfo.setTps(stageNode.findChild(Statistic.count.name()).summary());
perfInfo.setMsTotalTime(stageNode.findChild(Statistic.totalTime.name()).summary() * 1000);
// when UT with DefaultRegistry, there is no max value
MeasurementNode maxNode = stageNode.findChild(Statistic.max.name());
if (maxNode != null) {
perfInfo.setMsMaxLatency(maxNode.summary() * 1000);
}
return perfInfo;
}
@Test
public void from() {
timer.record(10, TimeUnit.NANOSECONDS);
timer.record(2, TimeUnit.NANOSECONDS);
MeasurementGroupConfig config = new MeasurementGroupConfig("id", "g1", "g2", Statistic.count.key());
tree.from(registry.iterator(), config);
Assert.assertEquals(2, tree.getChildren().size());
MeasurementNode node = tree.findChild("id", "g1v", "g2v");
Assert.assertEquals(2d, node.findChild(Statistic.count.value()).getMeasurements().get(0).value(), 0);
Assert.assertEquals(12d, node.findChild(Statistic.totalTime.value()).getMeasurements().get(0).value(), 0);
Assert.assertEquals(0d, tree.findChild("id_notCare").summary(), 0);
}
/** Create a new instance. */
StatelessDistributionSummary(Id id, Clock clock, long ttl) {
super(id, clock, ttl);
count = new AtomicLong(0);
totalAmount = new AtomicLong(0);
totalOfSquares = new AtomicDouble(0.0);
max = new AtomicLong(0);
stats = new Id[] {
id.withTags(Statistic.count),
id.withTags(Statistic.totalAmount),
id.withTags(Statistic.totalOfSquares),
id.withTags(Statistic.max)
};
}
/** Create a new instance. */
StatelessTimer(Id id, Clock clock, long ttl) {
super(id, clock, ttl);
count = new AtomicLong(0);
totalTime = new AtomicDouble(0);
totalOfSquares = new AtomicDouble(0.0);
max = new AtomicDouble(0);
stats = new Id[] {
id.withTags(Statistic.count),
id.withTags(Statistic.totalTime),
id.withTags(Statistic.totalOfSquares),
id.withTags(Statistic.max)
};
}
@Test
public void patternUsingState() {
LongTaskTimer t = LongTaskTimer.get(registry, registry.createId("foo"));
long tid = t.start();
clock.addSeconds(60);
PolledMeter.update(registry);
Gauge g = registry.gauge(registry.createId("foo").withTag(Statistic.duration));
Assertions.assertEquals(60.0, g.value(), 1e-12);
t.stop(tid);
PolledMeter.update(registry);
Assertions.assertEquals(0.0, g.value(), 1e-12);
}
/** Create a new instance. */
AtlasDistributionSummary(Id id, Clock clock, long ttl, long step) {
super(id, clock, ttl);
this.count = new StepLong(0L, clock, step);
this.total = new StepLong(0L, clock, step);
this.totalOfSquares = new StepDouble(0.0, clock, step);
this.max = new StepLong(0L, clock, step);
this.stats = new Id[] {
id.withTags(DsType.rate, Statistic.count),
id.withTags(DsType.rate, Statistic.totalAmount),
id.withTags(DsType.rate, Statistic.totalOfSquares),
id.withTags(DsType.gauge, Statistic.max)
};
}
/** Create a new instance. */
AtlasMaxGauge(Registry registry, Id id, Clock clock, long ttl, long step) {
super(id, clock, ttl);
this.value = new StepDouble(0.0, clock, step);
// Add the statistic for typing. Re-adding the tags from the id is to retain
// the statistic from the id if it was already set
this.stat = registry.createId(id.name())
.withTag(Statistic.max)
.withTag(DsType.gauge)
.withTags(id.tags());
}
/** Create a new instance. */
AtlasGauge(Registry registry, Id id, Clock clock, long ttl) {
super(id, clock, ttl);
this.value = new AtomicDouble(0.0);
// Add the statistic for typing. Re-adding the tags from the id is to retain
// the statistic from the id if it was already set
this.stat = registry.createId(id.name())
.withTag(Statistic.gauge)
.withTag(DsType.gauge)
.withTags(id.tags());
}
/** Create a new instance. */
AtlasCounter(Registry registry, Id id, Clock clock, long ttl, long step) {
super(id, clock, ttl);
this.value = new StepDouble(0.0, clock, step);
// Add the statistic for typing. Re-adding the tags from the id is to retain
// the statistic from the id if it was already set
this.stat = registry.createId(id.name())
.withTag(Statistic.count)
.withTag(DsType.rate)
.withTags(id.tags());
}
/** Create a new instance. */
AtlasTimer(Id id, Clock clock, long ttl, long step) {
super(id, clock, ttl);
this.count = new StepLong(0L, clock, step);
this.total = new StepDouble(0.0, clock, step);
this.totalOfSquares = new StepDouble(0.0, clock, step);
this.max = new StepLong(0L, clock, step);
this.stats = new Id[] {
id.withTags(DsType.rate, Statistic.count),
id.withTags(DsType.rate, Statistic.totalTime),
id.withTags(DsType.rate, Statistic.totalOfSquares),
id.withTags(DsType.gauge, Statistic.max)
};
}
@Test
public void aggregateTimers() {
for (int i = 0; i < 10; ++i) {
registry.timer("test", "i", "" + i).record(i, TimeUnit.SECONDS);
}
clock.setWallTime(5000);
List<Measurement> input = registry.measurements().collect(Collectors.toList());
List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
Assertions.assertEquals(4, aggr.size());
for (Measurement m : aggr) {
Id id = registry.createId("test");
switch (Utils.getTagValue(m.id(), "statistic")) {
case "count":
id = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(10.0 / 5.0, m.value(), 1e-12);
break;
case "totalTime":
id = id.withTag("atlas.dstype", "rate").withTag(Statistic.totalTime);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(45.0 / 5.0, m.value(), 1e-12);
break;
case "totalOfSquares":
id = id.withTag("atlas.dstype", "rate").withTag(Statistic.totalOfSquares);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(285.0 / 5.0, m.value(), 1e-12);
break;
case "max":
id = id.withTag("atlas.dstype", "gauge").withTag(Statistic.max);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(9.0, m.value(), 1e-12);
break;
default:
Assertions.fail("unexpected id: " + m.id());
break;
}
}
}
@Test
public void aggregateDistributionSummaries() {
for (int i = 0; i < 10; ++i) {
registry.distributionSummary("test", "i", "" + i).record(i);
}
clock.setWallTime(5000);
List<Measurement> input = registry.measurements().collect(Collectors.toList());
List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
Assertions.assertEquals(4, aggr.size());
for (Measurement m : aggr) {
Id id = registry.createId("test");
switch (Utils.getTagValue(m.id(), "statistic")) {
case "count":
id = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(10.0 / 5.0, m.value(), 1e-12);
break;
case "totalAmount":
id = id.withTag("atlas.dstype", "rate").withTag(Statistic.totalAmount);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(45.0 / 5.0, m.value(), 1e-12);
break;
case "totalOfSquares":
id = id.withTag("atlas.dstype", "rate").withTag(Statistic.totalOfSquares);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(285.0 / 5.0, m.value(), 1e-12);
break;
case "max":
id = id.withTag("atlas.dstype", "gauge").withTag(Statistic.max);
Assertions.assertEquals(id, m.id());
Assertions.assertEquals(9.0, m.value(), 1e-12);
break;
default:
Assertions.fail("unexpected id: " + m.id());
break;
}
}
}
private void checkValue(long expected) {
int count = 0;
for (Measurement m : gauge.measure()) {
Assertions.assertEquals(gauge.id().withTags(Statistic.max, DsType.gauge), m.id());
Assertions.assertEquals(expected, m.value(), 1e-12);
++count;
}
Assertions.assertEquals(1, count);
}