簡體   English   中英

產生許多進程

[英]Spawn many processes erlang

我想通過測量隨着進程數量增加而花費時間來衡量數據庫的性能。 目的是在繪制性能與流程數量的關系圖之后,誰知道怎么做? 我是埃爾朗的初學者,請喂

假設您的數據庫是mnesia ,這應該不難。 一種方法是具有寫入功能和讀取功能。 with mnesia. 但是,請注意,有多個帶有“記憶”的“ , you should NOT use the context of transaction because it returns immediately to the calling process, even before a disc write has occured. 為了測試 ,您不應該使用transaction上下文,因為即使在光盤寫入之前, transaction上下文也會立即返回到調用過程。 但是,對於光盤寫入而言,重要的是您要查看稱為sync_transaction的上下文。 這是一個例子:

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

僅當mnesia表的所有活動副本已將記錄提交到數據磁盤文件上時,以上函數才會返回。 ,a , the and finally a . 因此,為了測試隨着進程增加的速度,您需要有一個 以及最后一個 which returns the exact time it took to execute (completely) a given function. 對於計時,我們有一個內置函數: ,該函數返回(完全)執行給定功能所花費的確切時間。 簡而言之,這就是我的做法:

-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從此處下載Gproc並將其構建到您的erlang lib路徑中。

要運行它,只需調用: stress_test:start(). write_time將幫助您繪制進程數與寫入時間的關系圖。 隨着進程數從0增加到上限( ?LIMIT ),我們記下了在給定瞬間寫入給定記錄所花費的時間,並且還記下了當時的進程數。


更新
\n f(S)-> f(S,[])。\n f(S,Args)-> list:flatten(io_lib:format(S,Args))。\n
那是缺少的功能。 道歉....
記住要使用應用程序tv研究表write_time ,打開一個窗口,您可以在其中檢查mnesia表。 使用此表可以查看隨着過程數量的不斷增加而增加的寫入時間/或降低的性能。

我遺漏的一個元素是使用time()來記錄寫操作的實際時間,這可能是重要的參數。 您可以將其添加到write_time表的表定義中。

您可能會看到tsung http://tsung.erlang-projects.org/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM