Skip to content

Commit 7d00267

Browse files
author
drewkerrigan
committed
adding post and delete and separating regexes into separate functions
1 parent a67ae32 commit 7d00267

File tree

2 files changed

+116
-32
lines changed

2 files changed

+116
-32
lines changed

examples/http.config

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66

77
{driver, basho_bench_driver_http}.
88

9-
%% Example syntax (mykeygen_seq is not defined)
10-
%% {key_generator, {function, test, mykeygen_seq, [10000, 10, 10, 100]}}.
11-
129
{key_generator, {int_to_str, {uniform_int, 50000}}}.
1310

11+
{value_generator, {fixed_bin, 10}}.
12+
1413
{operations, [
15-
{{get, {"localhost", 4567, "/"}}, 1},
16-
{{put, {"localhost", 4567, "/",
17-
"{\"this\":\"is_json_%%V\"}"}}, 1}
14+
%% Get without a key
15+
{{get, {"localhost", 4567, "/"}, []}, 1},
16+
%% Get with a key and headers
17+
{{get_re, {"localhost", 4567, "/%%K"}, [{'Content-Type', 'application/json'}]}, 1},
18+
%% Put with a json object and value
19+
{{put_re, {"localhost", 4567, "/",
20+
"{\"this\":\"is_json_%%V\"}"}, [{'Content-Type', 'application/json'}]}, 1},
21+
%% Post with an xml object and value
22+
{{post_re, {"localhost", 4567, "/%%K",
23+
"<?xml version=\"1.0\"?><catalog><book><author>%%V</author></book></catalog>"},
24+
[{'Content-Type', 'application/xml'}]}, 1},
25+
%% Delete with a key
26+
{{delete_re, {"localhost", 4567, "/%%K"}, []}, 1}
1827
]}.

src/basho_bench_driver_http.erl

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,60 @@ new(Id) ->
6464

6565
{ok, #state {path_params = Params}}.
6666

67-
run({get, {Host, Port, Path}}, KeyGen, _ValueGen, _State) ->
67+
run({get_re, {Host, Port, Path}, Headers}, KeyGen, _ValueGen, _State) ->
68+
Path1 = re:replace(Path, "%%K", KeyGen(), [global, {return, list}]),
69+
run({get, {Host, Port, Path1}, Headers}, KeyGen, _ValueGen, _State);
6870

69-
Path1 = re:replace(Path, "%%V", KeyGen(), [global, {return, list}]),
71+
run({get, {Host, Port, Path}, Headers}, _KeyGen, _ValueGen, _State) ->
72+
PUrl = #url{host=Host, port=Port, path=Path},
7073

71-
PUrl = #url{host=Host, port=Port, path=Path1},
72-
73-
case do_get(PUrl) of
74+
case do_get(PUrl, Headers) of
7475
{not_found, _Url} ->
7576
{ok, 1};
76-
{ok, _Url, _Headers} ->
77+
{ok, _Url, _Header} ->
78+
{ok, 1};
79+
{error, Reason} ->
80+
{error, Reason, 1}
81+
end;
82+
83+
run({put_re, {Host, Port, Path, Data}, Headers}, KeyGen, ValueGen, _State) ->
84+
Path1 = re:replace(Path, "%%K", KeyGen(), [global, {return, list}]),
85+
Value = re:replace(Data, "%%V", ValueGen(), [global, {return, list}]),
86+
run({put, {Host, Port, Path1, Value}, Headers}, KeyGen, ValueGen, _State);
87+
88+
run({put, {Host, Port, Path, Data}, Headers}, _KeyGen, _ValueGen, _State) ->
89+
PUrl = #url{host=Host, port=Port, path=Path},
90+
91+
case do_put(PUrl, Headers, Data) of
92+
ok ->
93+
{ok, 1};
94+
{error, Reason} ->
95+
{error, Reason, 1}
96+
end;
97+
98+
run({post_re, {Host, Port, Path, Data}, Headers}, KeyGen, ValueGen, _State) ->
99+
Path1 = re:replace(Path, "%%K", KeyGen(), [global, {return, list}]),
100+
Value = re:replace(Data, "%%V", ValueGen(), [global, {return, list}]),
101+
run({post, {Host, Port, Path1, Value}, Headers}, KeyGen, ValueGen, _State);
102+
103+
run({post, {Host, Port, Path, Data}, Headers}, _KeyGen, _ValueGen, _State) ->
104+
PUrl = #url{host=Host, port=Port, path=Path},
105+
106+
case do_post(PUrl, Headers, Data) of
107+
ok ->
77108
{ok, 1};
78109
{error, Reason} ->
79110
{error, Reason, 1}
80111
end;
81-
run({put, {Host, Port, Path, Data}}, KeyGen, _ValueGen, _State) ->
82-
Path1 = re:replace(Path, "%%V", KeyGen(), [global, {return, list}]),
83112

84-
PUrl = #url{host=Host, port=Port, path=Path1},
113+
run({delete_re, {Host, Port, Path}, Headers}, KeyGen, _ValueGen, _State) ->
114+
Path1 = re:replace(Path, "%%K", KeyGen(), [global, {return, list}]),
115+
run({delete, {Host, Port, Path1}, Headers}, KeyGen, _ValueGen, _State);
85116

86-
Value = re:replace(Data, "%%V", KeyGen(), [global, {return, list}]),
117+
run({delete, {Host, Port, Path}, Headers}, _KeyGen, _ValueGen, _State) ->
118+
PUrl = #url{host=Host, port=Port, path=Path},
87119

88-
case do_put(PUrl, [], Value) of
120+
case do_delete(PUrl, Headers) of
89121
ok ->
90122
{ok, 1};
91123
{error, Reason} ->
@@ -96,21 +128,16 @@ run({put, {Host, Port, Path, Data}}, KeyGen, _ValueGen, _State) ->
96128
%% Internal functions
97129
%% ====================================================================
98130

99-
do_get(Url) ->
100-
do_get(Url, []).
101-
102-
do_get(Url, Opts) ->
103-
case send_request(Url, [], get, [], [{response_format, binary}]) of
104-
{ok, "404", _Headers, _Body} ->
131+
do_get(Url, Headers) ->
132+
%%case send_request(Url, [], get, [], [{response_format, binary}]) of
133+
case send_request(Url, Headers, get, [], [{response_format, binary}]) of
134+
{ok, "404", _Header, _Body} ->
105135
{not_found, Url};
106-
{ok, "300", Headers, _Body} ->
107-
{ok, Url, Headers};
108-
{ok, "200", Headers, Body} ->
109-
case proplists:get_bool(body_on_success, Opts) of
110-
true -> {ok, Url, Headers, Body};
111-
false -> {ok, Url, Headers}
112-
end;
113-
{ok, Code, _Headers, _Body} ->
136+
{ok, "300", Header, _Body} ->
137+
{ok, Url, Header};
138+
{ok, "200", Header, _Body} ->
139+
{ok, Url, Header};
140+
{ok, Code, _Header, _Body} ->
114141
{error, {http_error, Code}};
115142
{error, Reason} ->
116143
{error, Reason}
@@ -122,8 +149,14 @@ do_put(Url, Headers, ValueGen) ->
122149
true ->
123150
ValueGen
124151
end,
125-
case send_request(Url, Headers ++ [{'Content-Type', 'application/octet-stream'}],
152+
case send_request(Url, Headers,
126153
put, Val, [{response_format, binary}]) of
154+
{ok, "200", _Header, _Body} ->
155+
ok;
156+
{ok, "201", _Header, _Body} ->
157+
ok;
158+
{ok, "202", _Header, _Body} ->
159+
ok;
127160
{ok, "204", _Header, _Body} ->
128161
ok;
129162
{ok, Code, _Header, _Body} ->
@@ -132,6 +165,48 @@ do_put(Url, Headers, ValueGen) ->
132165
{error, Reason}
133166
end.
134167

168+
do_post(Url, Headers, ValueGen) ->
169+
Val = if is_function(ValueGen) ->
170+
ValueGen();
171+
true ->
172+
ValueGen
173+
end,
174+
case send_request(Url, Headers,
175+
post, Val, [{response_format, binary}]) of
176+
{ok, "200", _Header, _Body} ->
177+
ok;
178+
{ok, "201", _Header, _Body} ->
179+
ok;
180+
{ok, "202", _Header, _Body} ->
181+
ok;
182+
{ok, "204", _Header, _Body} ->
183+
ok;
184+
{ok, Code, _Header, _Body} ->
185+
{error, {http_error, Code}};
186+
{error, Reason} ->
187+
{error, Reason}
188+
end.
189+
190+
do_delete(Url, Headers) ->
191+
case send_request(Url, Headers, delete, [], []) of
192+
{ok, "200", _Header, _Body} ->
193+
ok;
194+
{ok, "201", _Header, _Body} ->
195+
ok;
196+
{ok, "202", _Header, _Body} ->
197+
ok;
198+
{ok, "204", _Header, _Body} ->
199+
ok;
200+
{ok, "404", _Header, _Body} ->
201+
ok;
202+
{ok, "410", _Header, _Body} ->
203+
ok;
204+
{ok, Code, _Header, _Body} ->
205+
{error, {http_error, Code}};
206+
{error, Reason} ->
207+
{error, Reason}
208+
end.
209+
135210
connect(Url) ->
136211
case erlang:get({ibrowse_pid, Url#url.host}) of
137212
undefined ->

0 commit comments

Comments
 (0)