产生许多进程erlang

时间:2012-03-21 23:09:30

标签: erlang process mnesia spawn

我想通过测量随着进程数量增加而做某事的时间来衡量我的数据库的性能。目的是绘制一个性能与过程数量的图表,任何人都知道如何?我是elrlang的初学者,请你好

3 个答案:

答案 0 :(得分:5)

假设您的数据库是mnesia,这应该不难。一种方法是具有写入功能和读取功能。但请注意,有几个 Activity access contexts 与mnesia。要测试write times,您不应使用transaction的上下文,因为它会立即返回到调用进程,即使在发生光盘写入之前也是如此。但是,对于光盘写入,重要的是要查看名为sync_transaction的上下文。这是一个例子:

write(Record)->
    Fun = fun(R)-> mnesia:write(R) end,
    mnesia:activity(sync_transaction,Fun,[Record],mnesia_frag).

只有当mnesia表的所有活动副本都将记录提交到数据光盘文件时,才会返回上述函数。因此,为了在流程增加时测试速度,您需要 record generator a process spawner write function 最后是 timing mechanism 。对于计时,我们有一个内置函数,名为: timer:tc/1, timer:tc/2 and timer:tc/3 ,它返回执行(完全)给定函数所花费的确切时间。简而言之,我就是这样做的:

-module(stress_test).
-compile(export_all).
-define(LIMIT,10000).
-record(book,{ isbn, title, price, version}).
%% ensure this table is {type,bag}
-record(write_time,{ isbn, num_of_processes, write_time }).
%% Assuming table (book) already exists %% Assuming mnesia running already
start()-> ensure_gproc(), tv:start(), spawn_many(?LIMIT).
spawn_many(0)-> ok; spawn_many(N)-> spawn(?MODULE,process,[]), spawn_many(N - 1).
process()-> gproc:reg({n, l,guid()},ignored), timer:apply_interval(timer:seconds(2),?MODULE,write,[]), receive <<"stop">> -> exit(normal) end.
total_processes()-> proplists:get_value(size,ets:info(gproc)) div 3.
ensure_gproc()-> case lists:keymember(gproc,1,application:which_applications()) of true -> ok; false -> application:start(gproc) end.
guid()-> random:seed(now()), MD5 = erlang:md5(term_to_binary([random:uniform(152629977),{node(), now(), make_ref()}])), MD5List = lists:nthtail(3, binary_to_list(MD5)), F = fun(N) -> f("~2.16.0B", [N]) end, L = [F(N) || N <- MD5List], lists:flatten(L).
generate_record()-> #book{isbn = guid(),title = guid(),price = guid()}.
write()-> Record = generate_record(), Fun = fun(R)-> ok = mnesia:write(R),ok end, %% Here is now the actual write we measure {Time,ok} = timer:tc(mnesia,activity,[sync_transaction,Fun,[Record],mnesia_frag]), %% The we save that time, the number of processes %% at that instant NoteTime = #write_time{ isbn = Record#book.isbn, num_of_processes = total_processes(), write_time = Time }, mnesia:activity(transaction,Fun,[NoteTime],mnesia_frag).

现在这里有依赖项,特别是: gproc 从这里 Download Gproc 下载并将其构建到你的erlang lib路径中。

要运行此功能,只需调用:stress_test:start().write_time将帮助您绘制进程数和写入时间的图表。随着进程数从0增加到上限(?LIMIT),我们注意到在给定时刻写入给定记录所花费的时间,并且我们还记录了当时进程的数量。


更新
f(S)-> f(S,[]).
f(S,Args) -> lists:flatten(io_lib:format(S, Args)).
这是缺失的功能。道歉....
记得研究表write_time,使用应用程序tv,打开一个窗口,您可以在其中检查mnesia表。使用此表可以查看增加的写入时间/或降低性能,因为进程数会不时增加。

我遗漏的一个要素是使用time()来记录写入动作的实际时间,write_time可能是重要的参数。您可以将其添加到{{1}}表的表定义中。

答案 1 :(得分:2)

答案 2 :(得分:1)

你可以看看tsung http://tsung.erlang-projects.org/