File: multipart_bm.erl

package info (click to toggle)
yaws 2.0.8%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,084 kB
  • sloc: erlang: 42,190; sh: 2,543; javascript: 1,459; ansic: 890; makefile: 878; lisp: 79; python: 34; xml: 12; php: 1
file content (43 lines) | stat: -rw-r--r-- 1,493 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-module(multipart_bm).
-include("../../include/yaws_api.hrl").

-export([benchmarks/0]).

-export([long_msg/1]).

benchmarks() ->
    {100, [long_msg]}.

long_msg(Iter) ->
    Sep = make_rand_bin(32),
    Body = binary:copy(make_rand_bin(10240), 100),
    Msg = list_to_binary(["--", Sep, "\r\n",
                          "Content-Disposition: form-data; name=\"abc123\"; ",
                          "filename=\"abc123\"\r\n",
                          "Content-Type: text/plain\r\n",
                          "Test-Header: sampledata\r\n\r\n",
                          Body,
                          "\r\n--",Sep,"--\r\n"
                         ]),
    long_msg(Iter, mk_arg(Msg, Sep), Body).

long_msg(0, _Msg, _Body) -> ok;
long_msg(Iter, Msg, Body) ->
    {result, Res} = yaws_api:parse_multipart_post(Msg, [binary]),
    Body = proplists:get_value(body, Res),
    %%io:format("~p~n", [Res]),
    long_msg(Iter - 1, Msg, Body).

make_rand_bin(Length) ->
    State = rand:seed_s(exs64),
    make_rand_bin(Length, State, <<>>).
make_rand_bin(0, _State, Acc) -> Acc;
make_rand_bin(Length, State, Acc) ->
    {Rand, State1} = rand:uniform_s($z - $a, State),
    make_rand_bin(Length - 1, State1, <<Acc/binary, (Rand + $a - 1)>>).

mk_arg(Data, Sep) ->
    ContentType = binary_to_list(<<"multipart/form-data; boundary=", Sep/binary>>),
    Req = #http_request{method = 'POST'},
    Headers = #headers{content_type = ContentType},
    #arg{headers = Headers, req = Req, clidata = Data}.