|
| 1 | +/** |
| 2 | + * Copyright 2018, OpenCensus Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import * as mocha from 'mocha'; |
| 19 | + |
| 20 | +import {Recorder} from '../src'; |
| 21 | +import {AggregationType, CountData, DistributionData, LastValueData, Measure, Measurement, MeasureType, MeasureUnit, SumData, Tags} from '../src/stats/types'; |
| 22 | + |
| 23 | +/** The order of how close values must be to be considerated almost equal */ |
| 24 | +const EPSILON = 6; |
| 25 | + |
| 26 | +interface RecorderTestCase { |
| 27 | + values: number[]; |
| 28 | + description: string; |
| 29 | +} |
| 30 | + |
| 31 | +function isAlmostEqual( |
| 32 | + actual: number, expected: number, epsilon: number): boolean { |
| 33 | + return Math.abs(actual - expected) < Math.pow(10, -epsilon); |
| 34 | +} |
| 35 | + |
| 36 | +function assertDistributionData( |
| 37 | + distributionData: DistributionData, values: number[]) { |
| 38 | + const valuesSum = values.reduce((acc, cur) => acc + cur); |
| 39 | + |
| 40 | + assert.strictEqual(distributionData.max, Math.max(...values)); |
| 41 | + assert.strictEqual(distributionData.min, Math.min(...values)); |
| 42 | + assert.strictEqual(distributionData.count, values.length); |
| 43 | + assert.strictEqual(distributionData.sum, valuesSum); |
| 44 | + |
| 45 | + for (const bucket of distributionData.buckets) { |
| 46 | + const expectedBucketCount = values |
| 47 | + .filter( |
| 48 | + value => bucket.lowBoundary <= value && |
| 49 | + value < bucket.highBoundary) |
| 50 | + .length; |
| 51 | + assert.strictEqual(bucket.count, expectedBucketCount); |
| 52 | + } |
| 53 | + |
| 54 | + const expectedMean = valuesSum / values.length; |
| 55 | + assert.ok(isAlmostEqual(distributionData.mean, expectedMean, EPSILON)); |
| 56 | + |
| 57 | + const expectedSumSquaredDeviations = |
| 58 | + values.map(value => Math.pow(value - expectedMean, 2)) |
| 59 | + .reduce((acc, curr) => acc + curr); |
| 60 | + assert.ok(isAlmostEqual( |
| 61 | + distributionData.sumSquaredDeviations, expectedSumSquaredDeviations, |
| 62 | + EPSILON)); |
| 63 | + |
| 64 | + const expectedStdDeviation = |
| 65 | + Math.sqrt(expectedSumSquaredDeviations / values.length); |
| 66 | + assert.ok(isAlmostEqual( |
| 67 | + distributionData.stdDeviation, expectedStdDeviation, EPSILON)); |
| 68 | +} |
| 69 | + |
| 70 | +describe('Recorder', () => { |
| 71 | + const measures: Measure[] = [ |
| 72 | + {name: 'Test Measure 1', type: MeasureType.DOUBLE, unit: MeasureUnit.UNIT}, |
| 73 | + {name: 'Test Measure 2', type: MeasureType.INT64, unit: MeasureUnit.UNIT} |
| 74 | + ]; |
| 75 | + const tags: Tags = {testKey: 'testValue'}; |
| 76 | + const testCases: RecorderTestCase[] = [ |
| 77 | + {values: [1.1, 2.5, 3.2, 4.7, 5.2], description: 'with positive values'}, { |
| 78 | + values: [-1.5, -2.3, -3.7, -4.3, -5.9], |
| 79 | + description: 'with negative values' |
| 80 | + }, |
| 81 | + {values: [0, 0, 0, 0], description: 'with zeros'}, |
| 82 | + {values: [1.1, -2.3, 3.2, -4.3, 5.2], description: 'with mixed values'} |
| 83 | + ]; |
| 84 | + |
| 85 | + for (const measure of measures) { |
| 86 | + describe(`for count aggregation data of ${measure.type} values`, () => { |
| 87 | + for (const testCase of testCases) { |
| 88 | + it(`should record measurements ${testCase.description} correctly`, |
| 89 | + () => { |
| 90 | + const countData: CountData = { |
| 91 | + type: AggregationType.COUNT, |
| 92 | + tags, |
| 93 | + timestamp: Date.now(), |
| 94 | + value: 0 |
| 95 | + }; |
| 96 | + let count = 0; |
| 97 | + for (const value of testCase.values) { |
| 98 | + count++; |
| 99 | + const measurement: Measurement = {measure, tags, value}; |
| 100 | + const updatedAggregationData = |
| 101 | + Recorder.addMeasurement(countData, measurement) as CountData; |
| 102 | + |
| 103 | + assert.strictEqual(updatedAggregationData.value, count); |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + describe( |
| 110 | + `for last value aggregation data of ${measure.type} values`, () => { |
| 111 | + for (const testCase of testCases) { |
| 112 | + it(`should record measurements ${testCase.description} correctly`, |
| 113 | + () => { |
| 114 | + const lastValueData: LastValueData = { |
| 115 | + type: AggregationType.LAST_VALUE, |
| 116 | + tags, |
| 117 | + timestamp: Date.now(), |
| 118 | + value: undefined |
| 119 | + }; |
| 120 | + for (const value of testCase.values) { |
| 121 | + const measurement: Measurement = {measure, tags, value}; |
| 122 | + const lastValue = measure.type === MeasureType.DOUBLE ? |
| 123 | + value : |
| 124 | + Math.trunc(value); |
| 125 | + |
| 126 | + const updatedAggregationData = |
| 127 | + Recorder.addMeasurement(lastValueData, measurement) as |
| 128 | + LastValueData; |
| 129 | + assert.strictEqual(updatedAggregationData.value, lastValue); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + describe(`for sum aggregation data of ${measure.type} values`, () => { |
| 136 | + for (const testCase of testCases) { |
| 137 | + it(`should record measurements ${testCase.description} correctly`, |
| 138 | + () => { |
| 139 | + const sumData: SumData = { |
| 140 | + type: AggregationType.SUM, |
| 141 | + tags, |
| 142 | + timestamp: Date.now(), |
| 143 | + value: 0 |
| 144 | + }; |
| 145 | + let acc = 0; |
| 146 | + for (const value of testCase.values) { |
| 147 | + acc += measure.type === MeasureType.DOUBLE ? value : |
| 148 | + Math.trunc(value); |
| 149 | + const measurement: Measurement = {measure, tags, value}; |
| 150 | + const updatedAggregationData = |
| 151 | + Recorder.addMeasurement(sumData, measurement) as SumData; |
| 152 | + |
| 153 | + assert.strictEqual(updatedAggregationData.value, acc); |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + describe( |
| 160 | + `for distribution aggregation data of ${measure.type} values`, () => { |
| 161 | + for (const testCase of testCases) { |
| 162 | + it(`should record measurements ${testCase.description} correctly`, |
| 163 | + () => { |
| 164 | + const distributionData: DistributionData = { |
| 165 | + type: AggregationType.DISTRIBUTION, |
| 166 | + tags, |
| 167 | + timestamp: Date.now(), |
| 168 | + startTime: Date.now(), |
| 169 | + count: 0, |
| 170 | + sum: 0, |
| 171 | + max: Number.MIN_SAFE_INTEGER, |
| 172 | + min: Number.MAX_SAFE_INTEGER, |
| 173 | + mean: 0, |
| 174 | + stdDeviation: 0, |
| 175 | + sumSquaredDeviations: 0, |
| 176 | + buckets: [ |
| 177 | + {highBoundary: 0, lowBoundary: -Infinity, count: 0}, |
| 178 | + {highBoundary: 2, lowBoundary: 0, count: 0}, |
| 179 | + {highBoundary: 4, lowBoundary: 2, count: 0}, |
| 180 | + {highBoundary: 6, lowBoundary: 4, count: 0}, |
| 181 | + {highBoundary: Infinity, lowBoundary: 6, count: 0} |
| 182 | + ] |
| 183 | + }; |
| 184 | + const sentValues = []; |
| 185 | + for (const value of testCase.values) { |
| 186 | + sentValues.push( |
| 187 | + measure.type === MeasureType.DOUBLE ? value : |
| 188 | + Math.trunc(value)); |
| 189 | + const measurement: Measurement = {measure, tags, value}; |
| 190 | + const updatedAggregationData = |
| 191 | + Recorder.addMeasurement(distributionData, measurement) as |
| 192 | + DistributionData; |
| 193 | + |
| 194 | + assertDistributionData(distributionData, sentValues); |
| 195 | + } |
| 196 | + }); |
| 197 | + } |
| 198 | + }); |
| 199 | + } |
| 200 | +}); |
0 commit comments