erlang 进程消息及ets性能测试

进程消息发送与接收

1> test:test_proc_msg(10000000, {"test"}).
{3145000,ok}
2> test:test_proc_msg(10000000, {"test","test"}).
{3731000,ok}

3> test:test_proc_msg(10000000, {"testtesttesttest"}).
{4428000,ok}
4> test:test_proc_msg(10000000, {"testtest"}).
{3607000,ok}
5> test:test_proc_msg(10000000, {"test"}).
{3237000,ok}

1000W条简单消息发送接收时间3~4秒,每秒可以处理200W的消息,消息的大小对速度影响比较大


ets查询性能

3> test:test_ets(10000000, "test").
{{insert,{17341000,ok}},{lookup,{1717000,ok}}}

1000W万次ets写入花了17.3S,平均57W/S

1000W万次ets查询花了1.7S,平均580W/S,约为写入时间的十分之一


-module(test).


-compile(export_all).


-define(TAB, tab_name).
%%测试ets查询性能
test_ets(Num, Msg) ->
    ets:new(?TAB, [named_table,public]),
    
    InsertCostTime = timer:tc(?MODULE,insert, [Num, Msg]),
    LookupCostTime = timer:tc(?MODULE,lookup, [Num]),
    ets:delete(?TAB),
    {{insert, InsertCostTime},{lookup, LookupCostTime}}.


insert(Num, _Msg) when Num =< 0 ->    
    ok;
insert(Num, Msg) ->
    ets:insert(?TAB, {Num, Msg}),
    insert(Num - 1, Msg).


lookup(Num) when Num =< 0 ->
    ok;
lookup(Num) ->
    ets:lookup(?TAB, 50),
    lookup(Num -1).
    
%%测试进程消息收发
test_proc_msg(Num, Msg) ->
    SendPid = self(),
    RecvPid = spawn(fun() -> proc_recv(SendPid, Num) end),
    timer:tc(?MODULE, test_proc_msg_1, [RecvPid, Msg, Num]).


test_proc_msg_1(RecvPid,Msg, Num) ->
    proc_send(RecvPid, Msg, Num),
    receive
        send_over ->
            ok
    end.


proc_send(_RecvPid, _Msg, Num) when Num =< 0  ->
    ok;
proc_send(RecvPid, Msg, Num)  ->
    RecvPid ! Msg,
    proc_send(RecvPid, Msg, Num -1).


proc_recv(ParentPid, Num) when Num =< 0 ->
    ParentPid ! send_over;
proc_recv(ParentPid, Num)  ->
    receive
        _Msg ->
            proc_recv(ParentPid, Num-1)
    end.

你可能感兴趣的:(erlang开发)