View Source ddskerl_ets (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 ets tables. Values within (0,1) will be inserted into the underflow bucket; values above the expected limit will be inserted into the overflow bucket.

Calculating the bucket size

If you have an expected highest value M and a given error E, your ideal bucket size 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(3600000000, 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 shared ets tables are a more efficient way to ensure dynamicity and potentially code upgrades.

For example:

ddskerl_ets:new(#{ets_table => Table, name => Name, ...}).
...
ddskerl_ets:insert(Table, Name, Value).
...
ddskerl_ets:quantile(Table, Name, Q).

Summary

Types

DDSketch instance.

DDSketch tuple.

Options for the DDSketch.

Functions

Insert a value into the DDSketch.

Insert a value into the DDSketch.

Merge two DDSketch instances.

Merge two DDSketch instances.

Create a new DDSketch instance.

Create a new DDSketch instance.

Calculate the quantile of a DDSketch.

Calculate the quantile of a DDSketch.

Reset the DDSketch values to zero

Reset the DDSketch values to zero

Get the sum of elements in the DDSketch.

Get the sum of elements in the DDSketch.

Get the total number of elements in the DDSketch.

Get the total number of elements in the DDSketch.

Types

ddsketch()

-opaque ddsketch()

DDSketch instance.

object()

-opaque object()

DDSketch tuple.

opts()

-type opts() :: #{ets_table := ets:tab(), name := term(), error := float(), bound := non_neg_integer()}.

Options for the DDSketch.

ets_table refers to the table weher the sketch is stored, name is the key to use when storing the sketch in the ets table.

Functions

insert/2

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

Insert a value into the DDSketch.

insert/3

-spec insert(ets:tab(), term(), number()) -> any().

Insert a value into the DDSketch.

merge/2

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

Merge two DDSketch instances.

merge(Ref1, Name1, Ref2, Name2)

-spec merge(ets:tab(), term(), ets:tab(), term()) -> any().

Merge two DDSketch instances.

new/1

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

Create a new DDSketch instance.

new(Ref, Name, Err, Bound)

-spec new(ets:table(), term(), float(), non_neg_integer()) -> boolean().

Create a new DDSketch instance.

quantile/2

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

Calculate the quantile of a DDSketch.

quantile/3

-spec quantile(ets:tab(), term(), float()) -> float() | undefined.

Calculate the quantile of a DDSketch.

reset/1

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

Reset the DDSketch values to zero

reset(Ref, Name)

-spec reset(ets:tab(), term()) -> boolean().

Reset the DDSketch values to zero

sum/1

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

Get the sum of elements in the DDSketch.

sum(Ref, Name)

-spec sum(ets:table(), term()) -> 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.

total(Ref, Name)

-spec total(ets:table(), term()) -> non_neg_integer().

Get the total number of elements in the DDSketch.