Skip to content

Commit 1fe82a1

Browse files
committed
Validate some simple generators early
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
1 parent 7575664 commit 1fe82a1

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

src/basho_bench_keygen.erl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ new({concat_binary, OneGen, TwoGen}, Id) ->
6262
fun() ->
6363
<<(Gen1())/binary, (Gen2())/binary>>
6464
end;
65-
new({sequential_int, MaxKey}, Id) ->
65+
new({sequential_int, MaxKey}, Id)
66+
when is_integer(MaxKey), MaxKey > 0 ->
6667
Ref = make_ref(),
6768
DisableProgress =
6869
basho_bench_config:get(disable_sequential_int_progress_report, false),
6970
fun() -> sequential_int_generator(Ref, MaxKey, Id, DisableProgress) end;
7071
new({partitioned_sequential_int, MaxKey}, Id) ->
7172
new({partitioned_sequential_int, 0, MaxKey}, Id);
72-
new({partitioned_sequential_int, StartKey, NumKeys}, Id) ->
73+
new({partitioned_sequential_int, StartKey, NumKeys}, Id)
74+
when is_integer(StartKey), is_integer(NumKeys), NumKeys > 0 ->
7375
Workers = basho_bench_config:get(concurrent),
7476
Range = NumKeys div Workers,
7577
MinValue = StartKey + Range * (Id - 1),
@@ -81,18 +83,22 @@ new({partitioned_sequential_int, StartKey, NumKeys}, Id) ->
8183
basho_bench_config:get(disable_sequential_int_progress_report, false),
8284
?DEBUG("ID ~p generating range ~p to ~p\n", [Id, MinValue, MaxValue]),
8385
fun() -> sequential_int_generator(Ref, MaxValue - MinValue, Id, DisableProgress) + MinValue end;
84-
new({uniform_int, MaxKey}, _Id) ->
86+
new({uniform_int, MaxKey}, _Id)
87+
when is_integer(MaxKey), MaxKey > 0 ->
8588
fun() -> random:uniform(MaxKey) end;
86-
new({uniform_int, StartKey, NumKeys}, _Id) ->
89+
new({uniform_int, StartKey, NumKeys}, _Id)
90+
when is_integer(StartKey), is_integer(NumKeys), NumKeys > 0 ->
8791
fun() -> random:uniform(NumKeys) + StartKey - 1 end;
88-
new({pareto_int, MaxKey}, _Id) ->
92+
new({pareto_int, MaxKey}, _Id)
93+
when is_integer(MaxKey), MaxKey > 0 ->
8994
pareto(trunc(MaxKey * 0.2), ?PARETO_SHAPE);
9095
new({truncated_pareto_int, MaxKey}, Id) ->
9196
Pareto = new({pareto_int, MaxKey}, Id),
9297
fun() -> erlang:min(MaxKey, Pareto()) end;
9398
new(uuid_v4, _Id) ->
9499
fun() -> uuid:v4() end;
95-
new({function, Module, Function, Args}, Id) ->
100+
new({function, Module, Function, Args}, Id)
101+
when is_atom(Module), is_atom(Function), is_list(Args) ->
96102
case code:ensure_loaded(Module) of
97103
{module, Module} ->
98104
erlang:apply(Module, Function, [Id] ++ Args);
@@ -106,7 +112,7 @@ new({valgen, ValGen}, Id) ->
106112
new(Bin, _Id) when is_binary(Bin) ->
107113
fun() -> Bin end;
108114
new(Other, _Id) ->
109-
?FAIL_MSG("Unsupported key generator requested: ~p\n", [Other]).
115+
?FAIL_MSG("Invalid key generator requested: ~p\n", [Other]).
110116

111117
dimension({int_to_str, InputGen}) ->
112118
dimension(InputGen);

src/basho_bench_valgen.erl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,42 @@
3030
%% API
3131
%% ====================================================================
3232

33-
new({fixed_bin, Size}, Id) ->
33+
new({fixed_bin, Size}, Id)
34+
when is_integer(Size), Size >= 0 ->
3435
Source = init_source(Id),
3536
fun() -> data_block(Source, Size) end;
36-
new({fixed_bin, Size, Val}, _Id) ->
37+
new({fixed_bin, Size, Val}, _Id)
38+
when is_integer(Size), Size >= 0, is_integer(Val), Val >= 0, Val =< 255 ->
3739
Data = list_to_binary(lists:duplicate(Size, Val)),
3840
fun() -> Data end;
39-
new({fixed_char, Size}, _Id) ->
41+
new({fixed_char, Size}, _Id)
42+
when is_integer(Size), Size >= 0 ->
4043
fun() -> list_to_binary(lists:map(fun (_) -> random:uniform(95)+31 end, lists:seq(1,Size))) end;
41-
new({exponential_bin, MinSize, Mean}, Id) ->
44+
new({exponential_bin, MinSize, Mean}, Id)
45+
when is_integer(MinSize), MinSize >= 0, is_number(Mean), Mean > 0 ->
4246
Source = init_source(Id),
4347
fun() -> data_block(Source, MinSize + trunc(basho_bench_stats:exponential(1 / Mean))) end;
44-
new({uniform_bin, MinSize, MaxSize}, Id) ->
48+
new({uniform_bin, MinSize, MaxSize}, Id)
49+
when is_integer(MinSize), is_integer(MaxSize), MinSize < MaxSize ->
4550
Source = init_source(Id),
4651
Diff = MaxSize - MinSize,
4752
fun() -> data_block(Source, MinSize + random:uniform(Diff)) end;
48-
new({function, Module, Function, Args}, Id) ->
53+
new({function, Module, Function, Args}, Id)
54+
when is_atom(Module), is_atom(Function), is_list(Args) ->
4955
case code:ensure_loaded(Module) of
5056
{module, Module} ->
5157
erlang:apply(Module, Function, [Id] ++ Args);
5258
_Error ->
5359
?FAIL_MSG("Could not find valgen function: ~p:~p\n", [Module, Function])
5460
end;
55-
new({uniform_int, MaxVal}, _Id) ->
61+
new({uniform_int, MaxVal}, _Id)
62+
when is_integer(MaxVal), MaxVal >= 1 ->
5663
fun() -> random:uniform(MaxVal) end;
57-
new({uniform_int, MinVal, MaxVal}, _Id) ->
64+
new({uniform_int, MinVal, MaxVal}, _Id)
65+
when is_integer(MinVal), is_integer(MaxVal), MaxVal > MinVal ->
5866
fun() -> random:uniform(MinVal, MaxVal) end;
5967
new(Other, _Id) ->
60-
?FAIL_MSG("Unsupported value generator requested: ~p\n", [Other]).
68+
?FAIL_MSG("Invalid value generator requested: ~p\n", [Other]).
6169

6270
dimension({fixed_bin, Size}, KeyDimension) ->
6371
Size * KeyDimension;

0 commit comments

Comments
 (0)