Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Exponential Histogram #3498

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: implement exponential histogram merge
  • Loading branch information
mwear committed Dec 21, 2022
commit 9e855c8a438bc4be890a1b98cbe4aa4b31cba0c6
86 changes: 84 additions & 2 deletions packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export class ExponentialHistogramAccumulation implements Accumulation {
return this._count;
}

zeroCount(): number {
return this._zeroCount;
}

scale(): number {
if (this._count === this._zeroCount) {
// all zeros! scale doesn't matter, use zero
Expand Down Expand Up @@ -213,9 +217,65 @@ export class ExponentialHistogramAccumulation implements Accumulation {

/**
* Merge combines data from other into self
* @param other
* @param {ExponentialHistogramAccumulation} other
*/
mergeFrom(_other: ExponentialHistogramAccumulation) {}
merge(other: ExponentialHistogramAccumulation) {
if(this._count === 0) {
this._min = other.min();
this._max = other.max();
} else if ( other.count() !== 0 ) {
if(other.min() < this.min()) {
this._min = other.min();
}
if(other.max() > this.max()) {
this._max = other.max();
}
}

this._sum += other.sum();
this._count += other.count();
this._zeroCount += other.zeroCount();

let minScale = Math.min(this.scale(), other.scale());

const ourHighLowPos = this.highLowAtScale(this.positive(), minScale);
const otherHighLowPos = other.highLowAtScale(other.positive(), minScale);
const highLowPos = {
low: Math.min(ourHighLowPos.low, otherHighLowPos.low),
high: Math.max(ourHighLowPos.high, otherHighLowPos.high),
};

const ourHighLowNeg = this.highLowAtScale(this.negative(), minScale);
const otherHighLowNeg = other.highLowAtScale(other.negative(), minScale);
const highLowNeg = {
low: Math.min(ourHighLowNeg.low, otherHighLowNeg.low),
high: Math.max(ourHighLowNeg.high, otherHighLowNeg.high),
};

minScale = Math.min(
minScale - this._changeScale(highLowPos, this._maxSize),
minScale - this._changeScale(highLowNeg, this._maxSize),
);

this._downscale(this.scale() - minScale);

this._mergeBuckets(this.positive(), other, other.positive(), minScale);
this._mergeBuckets(this.negative(), other, other.negative(), minScale);
}

highLowAtScale(buckets: Buckets, scale: number): HighLow {
if(buckets.length() === 0) {
return {
low: 0,
high: -1,
};
}
const shift = this.scale() - scale;
return {
low: util.rightShift(buckets.indexStart, shift),
high: util.rightShift(buckets.indexEnd, shift),
};
}

// todo: rename?
private _update(buckets: Buckets, value: number, increment: number) {
Expand Down Expand Up @@ -330,6 +390,28 @@ export class ExponentialHistogramAccumulation implements Accumulation {
}
}

private _mergeBuckets(
ours: Buckets,
other: ExponentialHistogramAccumulation,
theirs: Buckets,
scale: number
) {
const theirOffset = theirs.offset();
const theirChange = other.scale() - scale;

for(let i=0; i < theirs.length(); i++) {
const result = this._incrementIndexby(
ours,
util.rightShift(theirOffset + i, theirChange),
theirs.at(i),
);
if( result.success ) {
// this should not happen
// todo: log
}
}
}

// todo: delete, for debugging
printBuckets() {
console.log('positive:');
Expand Down
154 changes: 154 additions & 0 deletions packages/sdk-metrics/test/aggregator/ExponentialHistogram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Mapping } from '../../src/aggregator/exponential-histogram//mapping/typ
import { ExponentMapping } from '../../src/aggregator/exponential-histogram//mapping/ExponentMapping';
import { LogarithmMapping } from '../../src/aggregator/exponential-histogram/mapping/LogarithmMapping';
import * as assert from 'assert';
import { assertInEpsilon, assertInDelta } from './exponential-histogram/helpers';

describe('ExponentialHistogramAccumulation', () => {
describe('record', () => {
Expand Down Expand Up @@ -202,6 +203,122 @@ describe('ExponentialHistogramAccumulation', () => {
});
});
});
describe('merge', () => {
it('handles simple (even) case', () => {
const acc0 = new ExponentialHistogramAccumulation([0, 0], 4);
const acc1 = new ExponentialHistogramAccumulation([0, 0], 4);
const acc2 = new ExponentialHistogramAccumulation([0, 0], 4);

for(let i = 0; i < 4; i++) {
const v1 = 2 << i;
const v2 = 1 / (1 << i);

acc0.record(v1);
acc1.record(v2);
acc2.record(v1);
acc2.record(v2);
}

assert.strictEqual(acc0.scale(), 0);
assert.strictEqual(acc1.scale(), 0);
assert.strictEqual(acc2.scale(), -1);

assert.strictEqual(acc0.positive().offset(), 0);
assert.strictEqual(acc1.positive().offset(), -4);
assert.strictEqual(acc2.positive().offset(), -2);

assert.deepStrictEqual(getCounts(acc0.positive()), [1, 1, 1, 1]);
assert.deepStrictEqual(getCounts(acc1.positive()), [1, 1, 1, 1]);
assert.deepStrictEqual(getCounts(acc2.positive()), [2, 2, 2, 2]);

acc0.merge(acc1);

assert.strictEqual(acc0.scale(), -1);
assert.strictEqual(acc2.scale(), -1);

assertHistogramsEqual(acc0, acc2);
});

it('handles simple (odd) case', () => {
const acc0 = new ExponentialHistogramAccumulation([0, 0], 4);
const acc1 = new ExponentialHistogramAccumulation([0, 0], 4);
const acc2 = new ExponentialHistogramAccumulation([0, 0], 4);

for(let i = 0; i < 4; i++) {
const v1 = 2 << i;
const v2 = 2 / (1 << i);

acc0.record(v1);
acc1.record(v2);
acc2.record(v1);
acc2.record(v2);
}

assert.strictEqual(acc0.count(), 4);
assert.strictEqual(acc1.count(), 4);
assert.strictEqual(acc2.count(), 8);

assert.strictEqual(acc0.scale(), 0);
assert.strictEqual(acc1.scale(), 0);
assert.strictEqual(acc2.scale(), -1);

assert.strictEqual(acc0.positive().offset(), 0);
assert.strictEqual(acc1.positive().offset(), -3);
assert.strictEqual(acc2.positive().offset(), -2);

assert.deepStrictEqual(getCounts(acc0.positive()), [1, 1, 1, 1]);
assert.deepStrictEqual(getCounts(acc1.positive()), [1, 1, 1, 1]);
assert.deepStrictEqual(getCounts(acc2.positive()), [1, 2, 3, 2]);

acc0.merge(acc1);

assert.strictEqual(acc0.scale(), -1);
assert.strictEqual(acc2.scale(), -1);

assertHistogramsEqual(acc0, acc2);
});

it('handles exhaustive test case', () => {
const testMergeExhaustive = (a: number[], b: number[], size: number, incr: number) => {
const aHist = new ExponentialHistogramAccumulation([0, 0], size);
const bHist = new ExponentialHistogramAccumulation([0, 0], size);
const cHist = new ExponentialHistogramAccumulation([0, 0], size);

a.forEach(av => {
aHist.updateByIncrement(av, incr);
cHist.updateByIncrement(av, incr);
});
b.forEach(bv => {
bHist.updateByIncrement(bv, incr);
cHist.updateByIncrement(bv, incr);
});

aHist.merge(bHist);

assertHistogramsEqual(aHist, cHist);
}

const factor = 1024.0;
const count = 16;
const means = [0, factor];
const stddevs = [1, factor];
const sizes = [2, 6, 8, 9, 16];
const increments = [1, 0x100, 0x10000, 0x100000000];

for(let mean of means) {
for(let stddev of stddevs) {
const values = Array.from({length: count}, () => mean + Math.random() * stddev);
for(let part = 1; part < count; part++) {
for(let size of sizes) {
for (let incr of increments) {
testMergeExhaustive(values.slice(0, part), values.slice(part, count), size, incr);
}
}
}
}
}
});
});
});

function getCounts(buckets: Buckets): Array<number> {
Expand All @@ -225,3 +342,40 @@ function getMapping(scale: number): Mapping {
return LogarithmMapping.get(scale);
}
}

function assertHistogramsEqual(
actual: ExponentialHistogramAccumulation,
expected: ExponentialHistogramAccumulation,
) {
const actualSum = actual.sum();
const expectedSum = expected.sum();

if(actualSum === 0 || expectedSum === 0) {
assertInDelta(actualSum, expectedSum, 1e-6);
} else {
assertInEpsilon(actualSum, expectedSum, 1e-6);
}

assert.strictEqual(actual.count(), expected.count());
assert.strictEqual(actual.zeroCount(), expected.zeroCount());
assert.strictEqual(actual.scale(), expected.scale());

assert.strictEqual(
bucketsToString(actual.positive()),
bucketsToString(expected.positive())
);

assert.strictEqual(
bucketsToString(actual.negative()),
bucketsToString(expected.negative())
);
}

function bucketsToString(buckets: Buckets): string {
let str = `[@${buckets.offset()}`;
for(let i = 0; i < buckets.length(); i++) {
str += buckets.at(i).toString();
}
str += ']\n';
return str;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ export function assertInEpsilon(
`expected relative error: ${relErr} to be < ${epsilon}`
);
}

export function assertInDelta(
actual: number,
expected: number,
delta: number) {
const actualDelta = Math.abs(expected - actual);
assert.ok(
actualDelta < delta,
`expected delta: ${delta} to be < ${actualDelta}`
);
}