Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[Full changelog](https://github.com/mozilla/glean.js/compare/v1.2.0...main)

* [#1514](https://github.com/mozilla/glean.js/pull/1514): Implement the Memory Distribution metric type.
* [#1475](https://github.com/mozilla/glean.js/pull/1475): Implement the Timing Distribution metric type.

# v1.2.0 (2022-09-21)
Expand Down
1 change: 1 addition & 0 deletions automation/size/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const METRIC_TYPES = [
"datetime",
"event",
"labeled",
"memory_distribution",
"quantity",
"string",
"text",
Expand Down
64 changes: 64 additions & 0 deletions glean/src/core/metrics/distributions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import type { Histogram } from "../../histogram/histogram";
import type { JSONValue } from "../utils";

export interface DistributionData {
// A map containing the bucket index mapped to the accumulated count.
//
// This can contain buckets with a count of `0`.
values: Record<number, number>;

// The accumulated sum of all the samples in the distribution.
sum: number;

// The number of entries in the histogram.
count: number;
}

/**
* Create a snapshot of the histogram with a time unit.
*
* Utility function for testing.
*
* @param hist Histogram to get the snapshot of.
* @returns Snapshot of the current histogram.
*/
export function snapshot(hist: Histogram): DistributionData {
const snapshotValues = hist.snapshotValues();

const utilizedValues: Record<number, number> = {};
Object.entries(snapshotValues).forEach(([key, value]) => {
const numericKey = Number(key);
if (value > 0 && !isNaN(numericKey)) {
utilizedValues[numericKey] = value;
}
});

return {
count: hist.count,
values: utilizedValues,
sum: hist.sum,
};
}

/**
* Takes the previous values and casts as a `number[]` or creates a new empty `number[]`. We store
* previous values as an array so that we can always reconstruct our histogram. We
* are unable to store complex objects in Glean as they must be JSON parse-able objects.
*
* @param jsonValue Will always be either undefined or a `number[]`.
* @returns An array of previous values or an empty array if nothing was previously stored.
*/
export function extractAccumulatedValuesFromJsonValue(jsonValue?: JSONValue): number[] {
let values: number[];
if (jsonValue) {
values = jsonValue as number[];
} else {
values = [];
}

return values;
}
34 changes: 34 additions & 0 deletions glean/src/core/metrics/memory_unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

export enum MemoryUnit {
// 1 byte
Byte = "byte",
// 2^10 bytes
Kilobyte = "kilobyte",
// 2^20 bytes
Megabyte = "megabyte",
// 2^30 bytes
Gigabyte = "gigabyte",
}

/**
* Converts a value in a given unit to bytes.
*
* @param value The value to convert.
* @param memoryUnit The `MemoryUnit` to convert from.
* @returns The integer representation of the byte value.
*/
export function convertMemoryUnitToBytes(value: number, memoryUnit: MemoryUnit): number {
switch (memoryUnit) {
case MemoryUnit.Byte:
return value;
case MemoryUnit.Kilobyte:
return value * (2 ** 10);
case MemoryUnit.Megabyte:
return value * (2 ** 20);
case MemoryUnit.Gigabyte:
return value * (2 ** 30);
}
}
26 changes: 26 additions & 0 deletions glean/src/core/metrics/time_unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,30 @@ enum TimeUnit {
Day = "day",
}

/**
* Converts a number from any `TimeUnit` to nanoseconds.
*
* @param duration Difference between start and stop time stamps.
* @param timeUnit Time unit for the duration.
* @returns Duration converted to nanoseconds.
*/
export function convertTimeUnitToNanos(duration: number, timeUnit: TimeUnit): number {
switch (timeUnit) {
case TimeUnit.Nanosecond:
return duration;
case TimeUnit.Microsecond:
return duration * 10 ** 3;
case TimeUnit.Millisecond:
return duration * 10 ** 6;
case TimeUnit.Second:
return duration * 10 ** 9;
case TimeUnit.Minute:
return duration * 10 ** 9 * 60;
case TimeUnit.Hour:
return duration * 10 ** 9 * 60 * 60;
case TimeUnit.Day:
return duration * 10 ** 9 * 60 * 60 * 24;
}
}

export default TimeUnit;
Loading