View Source ddskerl_counters (ddskerl v0.2.1)

DDSketch implementation in Erlang.

This implements an optimised pre-allocated bounded bucket count, that is, on a degenerate case, memory consumption won't grow but quantiles might lose accuracy as values escape bounds.

In this optimisation, buckets are preallocated using counters. Values within (0,1) will be inserted into the underflow bucket; values above the expected limit will be inserted into the overflow bucket.

About rounding errors

Due to the nature of the implementation, this algorithm will round/1 floats when keeping track of the minimum and maximums, hence these values might incurr rounding of a single digit errors.

All other quantiles, as well as integers, will be tracked with the precision the sketch was configured with.

Calculating the bucket count

If you have an expected highest value M and a given error E, your ideal bucket count would be given by the formula: ceil(math:log2(M) * (1.0 / math:log2((1 + E) / (1 - E)))).

For example, if you measure microseconds and you expect no operation to take more than an hour:

1> F = fun(M, E) -> ceil(math:log2(M) * (1.0 / math:log2((1 + E) / (1 - E)))) end.
2> F(3_600_000_000, 0.01).
1101

Note than for an expected error of 1% and 2184 buckets, we can fit values all the way as big to the biggest 64bits signed integer. If we're measuring picoseconds, this would suffice to measure 107 days.

When to use

This is a good choice when it is kept in a persistent_term and it is globally shared. Note however all the perks and pitfalls of using a persistent term before choosing one.

For example:

persistent_term:put(my_summary, ddskerl_counters:new(Opts)).
...
ddskerl_counters:insert(persistent_term:get(my_summary), Value).
...
ddskerl_counters:quantile(persistent_term:get(my_summary), Q).

Summary

Types

DDSketch instance

Options for the DDSketch

Functions

Insert a value into the DDSketch

Merge two DDSketch instances

Create a new DDSketch instance

Calculate the quantile of a DDSketch

Reset the DDSketch values to zero

Get the sum of elements in the DDSketch

Get the total number of elements in the DDSketch

Types

ddsketch()

-opaque ddsketch()

DDSketch instance

opts()

-type opts() :: #{error := float(), bound := non_neg_integer()}.

Options for the DDSketch

Functions

insert/2

-spec insert(ddsketch(), Value :: number()) -> ddsketch().

Insert a value into the DDSketch

merge/2

-spec merge(ddsketch(), ddsketch()) -> ddsketch().

Merge two DDSketch instances

new/1

-spec new(opts()) -> ddsketch().

Create a new DDSketch instance

quantile/2

-spec quantile(ddsketch(), Quantile :: float()) -> Value :: float() | undefined.

Calculate the quantile of a DDSketch

reset/1

-spec reset(ddsketch()) -> ddsketch().

Reset the DDSketch values to zero

sum/1

-spec sum(ddsketch()) -> non_neg_integer().

Get the sum of elements in the DDSketch

total/1

-spec total(ddsketch()) -> non_neg_integer().

Get the total number of elements in the DDSketch