Java源码示例:io.opencensus.stats.Aggregation.Distribution

示例1
@Test
public void testEquals() {
  new EqualsTester()
      .addEqualityGroup(Sum.create(), Sum.create())
      .addEqualityGroup(Count.create(), Count.create())
      .addEqualityGroup(
          Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))),
          Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))),
          Distribution.create(BucketBoundaries.create(Arrays.asList(0.0, 1.0, 5.0))))
      .addEqualityGroup(
          Distribution.create(BucketBoundaries.create(Arrays.asList(1.0, 2.0, 5.0))),
          Distribution.create(BucketBoundaries.create(Arrays.asList(1.0, 2.0, 5.0))))
      .addEqualityGroup(Mean.create(), Mean.create())
      .addEqualityGroup(LastValue.create(), LastValue.create())
      .testEquals();
}
 
示例2
@Test
public void testMatch() {
  List<Aggregation> aggregations =
      Arrays.asList(
          Sum.create(),
          Count.create(),
          Mean.create(),
          Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))),
          LastValue.create());

  List<String> actual = new ArrayList<String>();
  for (Aggregation aggregation : aggregations) {
    actual.add(
        aggregation.match(
            Functions.returnConstant("SUM"),
            Functions.returnConstant("COUNT"),
            Functions.returnConstant("DISTRIBUTION"),
            Functions.returnConstant("LASTVALUE"),
            Functions.returnConstant("UNKNOWN")));
  }

  assertThat(actual)
      .isEqualTo(Arrays.asList("SUM", "COUNT", "UNKNOWN", "DISTRIBUTION", "LASTVALUE"));
}
 
示例3
@Test
public void testGetCumulativeViewDataWithEmptyBucketBoundaries() {
  Aggregation noHistogram =
      Distribution.create(BucketBoundaries.create(Collections.<Double>emptyList()));
  View view = createCumulativeView(VIEW_NAME, MEASURE_DOUBLE, noHistogram, Arrays.asList(KEY));
  clock.setTime(Timestamp.create(1, 0));
  viewManager.registerView(view);
  statsRecorder
      .newMeasureMap()
      .put(MEASURE_DOUBLE, 1.1)
      .record(tagger.emptyBuilder().put(KEY, VALUE).build());
  clock.setTime(Timestamp.create(3, 0));
  ViewData viewData = viewManager.getView(VIEW_NAME);
  assertThat(viewData.getWindowData())
      .isEqualTo(CumulativeData.create(Timestamp.create(1, 0), Timestamp.create(3, 0)));
  StatsTestUtil.assertAggregationMapEquals(
      viewData.getAggregationMap(),
      ImmutableMap.of(
          Arrays.asList(VALUE),
          StatsTestUtil.createAggregationData(noHistogram, MEASURE_DOUBLE, 1.1)),
      EPSILON);
}
 
示例4
/**
 * Create exponential distribution for aggregating values.
 *
 * <p>Round boundaries to the nearest integer and remove duplicates.
 */
private static Distribution exponentialDistribution(double base, double expStart, double expStep,
    long steps) {
  return Distribution
      .create(BucketBoundaries.create(DoubleStream.iterate(expStart, v -> v + expStep)
          .limit(steps + 1).map(exp -> Math.round(Math.pow(base, exp))).distinct().boxed()
          .collect(Collectors.toList())));
}
 
示例5
@Test
public void createMutableAggregation() {
  BucketBoundaries bucketBoundaries = BucketBoundaries.create(Arrays.asList(-1.0, 0.0, 1.0));

  assertThat(
          RecordUtils.createMutableAggregation(Sum.create(), MEASURE_DOUBLE).toAggregationData())
      .isEqualTo(SumDataDouble.create(0));
  assertThat(RecordUtils.createMutableAggregation(Sum.create(), MEASURE_LONG).toAggregationData())
      .isEqualTo(SumDataLong.create(0));
  assertThat(
          RecordUtils.createMutableAggregation(Count.create(), MEASURE_DOUBLE)
              .toAggregationData())
      .isEqualTo(CountData.create(0));
  assertThat(
          RecordUtils.createMutableAggregation(Count.create(), MEASURE_LONG).toAggregationData())
      .isEqualTo(CountData.create(0));
  assertThat(
          RecordUtils.createMutableAggregation(Mean.create(), MEASURE_DOUBLE).toAggregationData())
      .isEqualTo(MeanData.create(0, 0));
  assertThat(
          RecordUtils.createMutableAggregation(Mean.create(), MEASURE_LONG).toAggregationData())
      .isEqualTo(MeanData.create(0, 0));
  assertThat(
          RecordUtils.createMutableAggregation(LastValue.create(), MEASURE_DOUBLE)
              .toAggregationData())
      .isEqualTo(LastValueDataDouble.create(Double.NaN));
  assertThat(
          RecordUtils.createMutableAggregation(LastValue.create(), MEASURE_LONG)
              .toAggregationData())
      .isEqualTo(LastValueDataLong.create(0));

  MutableDistribution mutableDistribution =
      (MutableDistribution)
          RecordUtils.createMutableAggregation(
              Distribution.create(bucketBoundaries), MEASURE_DOUBLE);
  assertThat(mutableDistribution.getSumOfSquaredDeviations()).isWithin(EPSILON).of(0);
  assertThat(mutableDistribution.getBucketCounts()).isEqualTo(new long[2]);
}
 
示例6
private static void registerAllViews() {
  // Defining the distribution aggregations
  Aggregation latencyDistribution =
      Distribution.create(
          BucketBoundaries.create(
              Arrays.asList(
                  // [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms,
                  // >=1s, >=2s, >=4s, >=6s]
                  0.0,
                  25.0,
                  50.0,
                  75.0,
                  100.0,
                  200.0,
                  400.0,
                  600.0,
                  800.0,
                  1000.0,
                  2000.0,
                  4000.0,
                  6000.0)));

  Aggregation lengthsDistribution =
      Distribution.create(
          BucketBoundaries.create(
              Arrays.asList(
                  // [>=0B, >=5B, >=10B, >=20B, >=40B, >=60B, >=80B, >=100B, >=200B, >=400B,
                  // >=600B,
                  // >=800B, >=1000B]
                  0.0,
                  5.0,
                  10.0,
                  20.0,
                  40.0,
                  60.0,
                  80.0,
                  100.0,
                  200.0,
                  400.0,
                  600.0,
                  800.0,
                  1000.0)));

  // Define the count aggregation
  Aggregation countAggregation = Aggregation.Count.create();

  // So tagKeys
  List<TagKey> noKeys = new ArrayList<TagKey>();

  // Define the views
  View[] views =
      new View[] {
        View.create(
            Name.create("ocjavametrics/latency"),
            "The distribution of latencies",
            M_LATENCY_MS,
            latencyDistribution,
            Collections.singletonList(KEY_METHOD)),
        View.create(
            Name.create("ocjavametrics/lines_in"),
            "The number of lines read in from standard input",
            M_LINES_IN,
            countAggregation,
            noKeys),
        View.create(
            Name.create("ocjavametrics/errors"),
            "The number of errors encountered",
            M_ERRORS,
            countAggregation,
            Collections.singletonList(KEY_METHOD)),
        View.create(
            Name.create("ocjavametrics/line_lengths"),
            "The distribution of line lengths",
            M_LINE_LENGTHS,
            lengthsDistribution,
            noKeys)
      };

  // Create the view manager
  ViewManager vmgr = Stats.getViewManager();

  // Then finally register the views
  for (View view : views) {
    vmgr.registerView(view);
  }
}
 
示例7
@Test
public void testCreateDistribution() {
  BucketBoundaries bucketBoundaries = BucketBoundaries.create(Arrays.asList(0.1, 2.2, 33.3));
  Distribution distribution = Distribution.create(bucketBoundaries);
  assertThat(distribution.getBucketBoundaries()).isEqualTo(bucketBoundaries);
}
 
示例8
@Test
public void testNullBucketBoundaries() {
  thrown.expect(NullPointerException.class);
  thrown.expectMessage("bucketBoundaries");
  Distribution.create(null);
}
 
示例9
@Override
public MutableAggregation apply(Distribution arg) {
  return MutableDistribution.create(arg.getBucketBoundaries());
}