Skip to content

Commit

Permalink
Validate some simple generators early
Browse files Browse the repository at this point in the history
This makes the crashing easier to diagnose, as it will indicated an
invalid generator was passed in. It's not 100% ideal, but most
generators are complex and simple static validation is not enough. We
can do a lot better but would probably require quite a bit more work
  • Loading branch information
engelsanchez committed Jan 13, 2014
1 parent 7575664 commit 1fe82a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
20 changes: 13 additions & 7 deletions src/basho_bench_keygen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ new({concat_binary, OneGen, TwoGen}, Id) ->
fun() ->
<<(Gen1())/binary, (Gen2())/binary>>
end;
new({sequential_int, MaxKey}, Id) ->
new({sequential_int, MaxKey}, Id)
when is_integer(MaxKey), MaxKey > 0 ->
Ref = make_ref(),
DisableProgress =
basho_bench_config:get(disable_sequential_int_progress_report, false),
fun() -> sequential_int_generator(Ref, MaxKey, Id, DisableProgress) end;
new({partitioned_sequential_int, MaxKey}, Id) ->
new({partitioned_sequential_int, 0, MaxKey}, Id);
new({partitioned_sequential_int, StartKey, NumKeys}, Id) ->
new({partitioned_sequential_int, StartKey, NumKeys}, Id)
when is_integer(StartKey), is_integer(NumKeys), NumKeys > 0 ->
Workers = basho_bench_config:get(concurrent),
Range = NumKeys div Workers,
MinValue = StartKey + Range * (Id - 1),
Expand All @@ -81,18 +83,22 @@ new({partitioned_sequential_int, StartKey, NumKeys}, Id) ->
basho_bench_config:get(disable_sequential_int_progress_report, false),
?DEBUG("ID ~p generating range ~p to ~p\n", [Id, MinValue, MaxValue]),
fun() -> sequential_int_generator(Ref, MaxValue - MinValue, Id, DisableProgress) + MinValue end;
new({uniform_int, MaxKey}, _Id) ->
new({uniform_int, MaxKey}, _Id)
when is_integer(MaxKey), MaxKey > 0 ->
fun() -> random:uniform(MaxKey) end;
new({uniform_int, StartKey, NumKeys}, _Id) ->
new({uniform_int, StartKey, NumKeys}, _Id)
when is_integer(StartKey), is_integer(NumKeys), NumKeys > 0 ->
fun() -> random:uniform(NumKeys) + StartKey - 1 end;
new({pareto_int, MaxKey}, _Id) ->
new({pareto_int, MaxKey}, _Id)
when is_integer(MaxKey), MaxKey > 0 ->
pareto(trunc(MaxKey * 0.2), ?PARETO_SHAPE);
new({truncated_pareto_int, MaxKey}, Id) ->
Pareto = new({pareto_int, MaxKey}, Id),
fun() -> erlang:min(MaxKey, Pareto()) end;
new(uuid_v4, _Id) ->
fun() -> uuid:v4() end;
new({function, Module, Function, Args}, Id) ->
new({function, Module, Function, Args}, Id)
when is_atom(Module), is_atom(Function), is_list(Args) ->
case code:ensure_loaded(Module) of
{module, Module} ->
erlang:apply(Module, Function, [Id] ++ Args);
Expand All @@ -106,7 +112,7 @@ new({valgen, ValGen}, Id) ->
new(Bin, _Id) when is_binary(Bin) ->
fun() -> Bin end;
new(Other, _Id) ->
?FAIL_MSG("Unsupported key generator requested: ~p\n", [Other]).
?FAIL_MSG("Invalid key generator requested: ~p\n", [Other]).

dimension({int_to_str, InputGen}) ->
dimension(InputGen);
Expand Down
26 changes: 17 additions & 9 deletions src/basho_bench_valgen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,42 @@
%% API
%% ====================================================================

new({fixed_bin, Size}, Id) ->
new({fixed_bin, Size}, Id)
when is_integer(Size), Size >= 0 ->
Source = init_source(Id),
fun() -> data_block(Source, Size) end;
new({fixed_bin, Size, Val}, _Id) ->
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;
new({fixed_char, Size}, _Id) ->
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) ->
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,
fun() -> data_block(Source, MinSize + random:uniform(Diff)) end;
new({function, Module, Function, Args}, Id) ->
new({function, Module, Function, Args}, Id)
when is_atom(Module), is_atom(Function), is_list(Args) ->
case code:ensure_loaded(Module) of
{module, Module} ->
erlang:apply(Module, Function, [Id] ++ Args);
_Error ->
?FAIL_MSG("Could not find valgen function: ~p:~p\n", [Module, Function])
end;
new({uniform_int, MaxVal}, _Id) ->
new({uniform_int, MaxVal}, _Id)
when is_integer(MaxVal), MaxVal >= 1 ->
fun() -> random:uniform(MaxVal) end;
new({uniform_int, MinVal, MaxVal}, _Id) ->
new({uniform_int, MinVal, MaxVal}, _Id)
when is_integer(MinVal), is_integer(MaxVal), MaxVal > MinVal ->
fun() -> random:uniform(MinVal, MaxVal) end;
new(Other, _Id) ->
?FAIL_MSG("Unsupported value generator requested: ~p\n", [Other]).
?FAIL_MSG("Invalid value generator requested: ~p\n", [Other]).

dimension({fixed_bin, Size}, KeyDimension) ->
Size * KeyDimension;
Expand Down

0 comments on commit 1fe82a1

Please sign in to comment.