Skip to content

Commit

Permalink
Add a fixed size binary set generator
Browse files Browse the repository at this point in the history
For testing CRDT sets we often want to generate a set of elements of
elements of a certain size. In the past we used int generators, but
sets of 32bit elements aren't realistic enough. We want to know how
many elements of size X before performance degrades. To that end this
commit adds a generator `{fixed_bin_set, ElemSize, Cardinality}` where
`ElemSize` is the size of a random binary element and `Cardinality` is
the size of the set. It makes use of the existing binary block
generation code in valgen.

It would be nice if we could generate variable size element sets in
future. Maybe just multiple sets from which we pick at random?
  • Loading branch information
russelldb committed Nov 26, 2015
1 parent 286f701 commit 3471aaf
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/basho_bench_valgen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ new({fixed_bin, Size, Val}, _Id)
when is_integer(Size), Size >= 0, is_integer(Val), Val >= 0, Val =< 255 ->
Data = list_to_binary(lists:duplicate(Size, Val)),
fun() -> Data end;
%% Create a set of binaries with elements of Size and a cardinality of Card
new({fixed_bin_set, Size, Card}, Id) when is_integer(Size), Size >= 0 ->
%% This might be a bit hacky
basho_bench_config:set(?VAL_GEN_SRC_SIZE, Size*Card),
Source = init_source(Id),
fun() -> data_block(fun aligned_offset/2, Source, Size) end;
new({fixed_char, Size}, _Id)
when is_integer(Size), Size >= 0 ->
fun() -> list_to_binary(lists:map(fun (_) -> random:uniform(95)+31 end, lists:seq(1,Size))) end;
new({exponential_bin, MinSize, Mean}, Id)
when is_integer(MinSize), MinSize >= 0, is_number(Mean), Mean > 0 ->
Source = init_source(Id),
fun() -> data_block(Source, MinSize + trunc(basho_bench_stats:exponential(1 / Mean))) end;
new({uniform_bin, MinSize, MaxSize}, Id)
new({uniform_bin, MinSize, MaxSize}, Id)
when is_integer(MinSize), is_integer(MaxSize), MinSize < MaxSize ->
Source = init_source(Id),
Diff = MaxSize - MinSize,
Expand Down Expand Up @@ -107,14 +113,23 @@ init_source(Id, Path) ->
end,
{?VAL_GEN_BLOB_CFG, size(Bin), Bin}.

data_block({SourceCfg, SourceSz, Source}, BlockSize) ->
data_block(Source, BlockSize) ->
data_block(fun random_offset/2, Source, BlockSize).

data_block(OffsetFun, {SourceCfg, SourceSz, Source}, BlockSize) ->
case SourceSz - BlockSize > 0 of
true ->
Offset = random:uniform(SourceSz - BlockSize),
Offset = OffsetFun(SourceSz, BlockSize),
<<_:Offset/bytes, Slice:BlockSize/bytes, _Rest/binary>> = Source,
Slice;
false ->
?WARN("~p is too small ~p < ~p\n",
[SourceCfg, SourceSz, BlockSize]),
Source
end.

random_offset(SourceSz, BlockSize) ->
random:uniform(SourceSz - BlockSize).

aligned_offset(SourceSz, BlockSize) ->
(random:uniform(SourceSz - BlockSize) div BlockSize) * BlockSize.

0 comments on commit 3471aaf

Please sign in to comment.