aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/test/java/io/opencensus/implcore/stats/MutableAggregationTest.java
blob: 5508588a49925f3e0838cba811d6b284239b9c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.implcore.stats;

import static com.google.common.truth.Truth.assertThat;

import io.opencensus.common.Function;
import io.opencensus.common.Functions;
import io.opencensus.implcore.stats.MutableAggregation.MutableCount;
import io.opencensus.implcore.stats.MutableAggregation.MutableDistribution;
import io.opencensus.implcore.stats.MutableAggregation.MutableLastValue;
import io.opencensus.implcore.stats.MutableAggregation.MutableMean;
import io.opencensus.implcore.stats.MutableAggregation.MutableSum;
import io.opencensus.stats.BucketBoundaries;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link io.opencensus.implcore.stats.MutableAggregation}. */
@RunWith(JUnit4.class)
public class MutableAggregationTest {

  @Rule public ExpectedException thrown = ExpectedException.none();

  private static final double TOLERANCE = 1e-6;
  private static final BucketBoundaries BUCKET_BOUNDARIES =
      BucketBoundaries.create(Arrays.asList(-10.0, 0.0, 10.0));

  @Test
  public void testCreateEmpty() {
    assertThat(MutableSum.create().getSum()).isWithin(TOLERANCE).of(0);
    assertThat(MutableCount.create().getCount()).isEqualTo(0);
    assertThat(MutableMean.create().getMean()).isWithin(TOLERANCE).of(0);
    assertThat(MutableLastValue.create().getLastValue()).isNaN();

    BucketBoundaries bucketBoundaries = BucketBoundaries.create(Arrays.asList(0.1, 2.2, 33.3));
    MutableDistribution mutableDistribution = MutableDistribution.create(bucketBoundaries);
    assertThat(mutableDistribution.getMean()).isWithin(TOLERANCE).of(0);
    assertThat(mutableDistribution.getCount()).isEqualTo(0);
    assertThat(mutableDistribution.getMin()).isPositiveInfinity();
    assertThat(mutableDistribution.getMax()).isNegativeInfinity();
    assertThat(mutableDistribution.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(0);
    assertThat(mutableDistribution.getBucketCounts()).isEqualTo(new long[4]);
  }

  @Test
  public void testNullBucketBoundaries() {
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("bucketBoundaries should not be null.");
    MutableDistribution.create(null);
  }

  @Test
  public void testNoBoundaries() {
    List<Double> buckets = Arrays.asList();
    MutableDistribution noBoundaries = MutableDistribution.create(BucketBoundaries.create(buckets));
    assertThat(noBoundaries.getBucketCounts().length).isEqualTo(1);
    assertThat(noBoundaries.getBucketCounts()[0]).isEqualTo(0);
  }

  @Test
  public void testAdd() {
    List<MutableAggregation> aggregations =
        Arrays.asList(
            MutableSum.create(),
            MutableCount.create(),
            MutableMean.create(),
            MutableDistribution.create(BUCKET_BOUNDARIES),
            MutableLastValue.create());

    List<Double> values = Arrays.asList(-1.0, 1.0, -5.0, 20.0, 5.0);

    for (double value : values) {
      for (MutableAggregation aggregation : aggregations) {
        aggregation.add(value);
      }
    }

    for (MutableAggregation aggregation : aggregations) {
      aggregation.match(
          new Function<MutableSum, Void>() {
            @Override
            public Void apply(MutableSum arg) {
              assertThat(arg.getSum()).isWithin(TOLERANCE).of(20.0);
              return null;
            }
          },
          new Function<MutableCount, Void>() {
            @Override
            public Void apply(MutableCount arg) {
              assertThat(arg.getCount()).isEqualTo(5);
              return null;
            }
          },
          new Function<MutableMean, Void>() {
            @Override
            public Void apply(MutableMean arg) {
              assertThat(arg.getMean()).isWithin(TOLERANCE).of(4.0);
              return null;
            }
          },
          new Function<MutableDistribution, Void>() {
            @Override
            public Void apply(MutableDistribution arg) {
              assertThat(arg.getBucketCounts()).isEqualTo(new long[] {0, 2, 2, 1});
              return null;
            }
          },
          new Function<MutableLastValue, Void>() {
            @Override
            public Void apply(MutableLastValue arg) {
              assertThat(arg.getLastValue()).isWithin(TOLERANCE).of(5.0);
              return null;
            }
          });
    }
  }

  @Test
  public void testMatch() {
    List<MutableAggregation> aggregations =
        Arrays.asList(
            MutableSum.create(),
            MutableCount.create(),
            MutableMean.create(),
            MutableDistribution.create(BUCKET_BOUNDARIES),
            MutableLastValue.create());

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

    assertThat(actual)
        .isEqualTo(Arrays.asList("SUM", "COUNT", "MEAN", "DISTRIBUTION", "LASTVALUE"));
  }

  @Test
  public void testCombine_SumCountMean() {
    // combine() for Mutable Sum, Count and Mean will pick up fractional stats
    List<MutableAggregation> aggregations1 =
        Arrays.asList(MutableSum.create(), MutableCount.create(), MutableMean.create());
    List<MutableAggregation> aggregations2 =
        Arrays.asList(MutableSum.create(), MutableCount.create(), MutableMean.create());

    for (double val : Arrays.asList(-1.0, -5.0)) {
      for (MutableAggregation aggregation : aggregations1) {
        aggregation.add(val);
      }
    }
    for (double val : Arrays.asList(10.0, 50.0)) {
      for (MutableAggregation aggregation : aggregations2) {
        aggregation.add(val);
      }
    }

    List<MutableAggregation> combined =
        Arrays.asList(MutableSum.create(), MutableCount.create(), MutableMean.create());
    double fraction1 = 1.0;
    double fraction2 = 0.6;
    for (int i = 0; i < combined.size(); i++) {
      combined.get(i).combine(aggregations1.get(i), fraction1);
      combined.get(i).combine(aggregations2.get(i), fraction2);
    }

    assertThat(((MutableSum) combined.get(0)).getSum()).isWithin(TOLERANCE).of(30);
    assertThat(((MutableCount) combined.get(1)).getCount()).isEqualTo(3);
    assertThat(((MutableMean) combined.get(2)).getMean()).isWithin(TOLERANCE).of(10);
  }

  @Test
  public void testCombine_Distribution() {
    // combine() for Mutable Distribution will ignore fractional stats
    MutableDistribution distribution1 = MutableDistribution.create(BUCKET_BOUNDARIES);
    MutableDistribution distribution2 = MutableDistribution.create(BUCKET_BOUNDARIES);
    MutableDistribution distribution3 = MutableDistribution.create(BUCKET_BOUNDARIES);

    for (double val : Arrays.asList(5.0, -5.0)) {
      distribution1.add(val);
    }
    for (double val : Arrays.asList(10.0, 20.0)) {
      distribution2.add(val);
    }
    for (double val : Arrays.asList(-10.0, 15.0, -15.0, -20.0)) {
      distribution3.add(val);
    }

    MutableDistribution combined = MutableDistribution.create(BUCKET_BOUNDARIES);
    combined.combine(distribution1, 1.0); // distribution1 will be combined
    combined.combine(distribution2, 0.6); // distribution2 will be ignored
    verifyMutableDistribution(combined, 0, 2, -5, 5, 50.0, new long[] {0, 1, 1, 0}, TOLERANCE);

    combined.combine(distribution2, 1.0); // distribution2 will be combined
    verifyMutableDistribution(combined, 7.5, 4, -5, 20, 325.0, new long[] {0, 1, 1, 2}, TOLERANCE);

    combined.combine(distribution3, 1.0); // distribution3 will be combined
    verifyMutableDistribution(combined, 0, 8, -20, 20, 1500.0, new long[] {2, 2, 1, 3}, TOLERANCE);
  }

  private static void verifyMutableDistribution(
      MutableDistribution mutableDistribution,
      double mean,
      long count,
      double min,
      double max,
      double sumOfSquaredDeviations,
      long[] bucketCounts,
      double tolerance) {
    assertThat(mutableDistribution.getMean()).isWithin(tolerance).of(mean);
    assertThat(mutableDistribution.getCount()).isEqualTo(count);
    assertThat(mutableDistribution.getMin()).isWithin(tolerance).of(min);
    assertThat(mutableDistribution.getMax()).isWithin(tolerance).of(max);
    assertThat(mutableDistribution.getSumOfSquaredDeviations())
        .isWithin(tolerance)
        .of(sumOfSquaredDeviations);
    assertThat(mutableDistribution.getBucketCounts()).isEqualTo(bucketCounts);
  }
}