riak / apps / erlang_js / src / js_memory.erl

-module(js_memory).

-define(COUNT, 1000000).

-export([stress/1]).

stress(new) ->
    Start = erlang:memory(total),
    do(new, ?COUNT),
    display(end_test() - Start);

stress(error) ->
    Start = erlang:memory(total),
    do(error, ?COUNT),
    display(end_test() - Start).

%% Internal functions
do(error, 0) ->
    ok;
do(error, Count) ->
    show_count(Count),
    {ok, P} = js_driver:new(),
    {error, _Error} = js:define(P, <<"function foo(;">>),
    js_driver:destroy(P),
    do(error, Count - 1);

do(new, 0) ->
    ok;
do(new, Count) ->
    show_count(Count),
    {ok, P} = js_driver:new(),
    js_driver:destroy(P),
    do(new, Count - 1).

end_test() ->
    [erlang:garbage_collect(P) || P <- erlang:processes()],
    erlang:memory(total).

display(Memory) ->
    io:format("Used ~p bytes during test.~n", [Memory]).

show_count(Count) ->
    if
        (?COUNT - Count) rem 1000 == 0 ->
            io:format("~p~n", [Count]);
        true ->
            ok
    end.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.