Created
November 3, 2015 17:09
-
-
Save kudryashov-sv/63e796895903d796b4cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(test). | |
-compile(export_all). | |
-record(state, {count, ts, big_dict}). | |
-define(PRINT_PERIOD, timer:seconds(1)). | |
run(DictSize, Threshold) -> | |
Monitor = run_monitor(Threshold), | |
Looper = run_loop(self(), DictSize), | |
ok = wait_looper(Looper), | |
io:format("start ...~n"), | |
timer:sleep(?PRINT_PERIOD * 5 + 100), | |
unlink(Looper), | |
exit(Looper, kill), | |
unlink(Monitor), | |
exit(Monitor, kill), | |
okay. | |
wait_looper(Looper) -> | |
receive | |
{Looper, ready} -> | |
ok | |
end. | |
run_loop(Owner, DictSize) -> | |
proc_lib:spawn_link(fun() -> init_loop(Owner, DictSize) end). | |
init_loop(Owner, DictSize) -> | |
BigTuple = erlang:list_to_tuple(lists:seq(1, 100000)), | |
StateIn = #state{ | |
big_dict = | |
lists:foldl( | |
fun(Key, AccIn) -> | |
AccSize = dict:size(AccIn), | |
if | |
AccSize > 0 andalso AccSize rem 10000 =:= 0 -> | |
io:format("[~.2f%] init dict ~w/~w~n", [(AccSize * 100) / DictSize, AccSize, DictSize]); | |
true -> | |
ok | |
end, | |
dict:store(Key, BigTuple, AccIn) | |
end, | |
dict:new(), | |
lists:seq(1, DictSize))}, | |
timer:send_interval(?PRINT_PERIOD, self(), print_stats), | |
Owner ! {self(), ready}, | |
loop(StateIn#state{count = 0, ts = os:timestamp()}). | |
loop(State = #state{count = Count, ts = TS, big_dict = Dict}) -> | |
receive | |
print_stats -> | |
io:format("looper performance: ~.3f CPS, dict size: ~w~n", [Count / (timer:now_diff(os:timestamp(), TS) / 1000000), dict:size(Dict)]), | |
?MODULE:loop(State#state{count = 0, ts = os:timestamp()}); | |
X -> | |
io:format("x: ~p", [X]) | |
after 0 -> | |
?MODULE:loop(State#state{count = Count + 1}) | |
end. | |
run_monitor(Threshold) -> | |
proc_lib:spawn_link(fun() -> erlang:system_monitor({self(), [{long_gc, Threshold}]}), monitor_loop() end). | |
monitor_loop() -> | |
receive | |
Any -> | |
io:format("message: ~w~n", [Any]), | |
?MODULE:monitor_loop() | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment