自己写的测试例子备用

 

 

  1 %%通过 gen_server 发送消息来实现 insert

  2 
  3 -module(test_ets_gen).
  4 -behaviour(gen_server).
  5 -export([start_link/0,test_call/1,test_cast/1]).
  6 -export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]).
  7 -record(state,{}).
  8 
  9 start_link()->
 10         gen_server:start_link({local,?MODULE},?MODULE,[],[]).
 11 
 12 test_call(Num)->
 13         Start = timestamp_in_millinsec(),
 14         [gen_server:call(?MODULE,{new,1}) || _X<- lists:seq(1,Num)],
 15         End = timestamp_in_millinsec(),
 16         (End-Start)/1000.
 17 test_cast(Num)->
 18         Start = timestamp_in_millinsec(),
 19         [gen_server:cast(?MODULE,{new,1}) || _X<- lists:seq(1,Num)],
 20         End = timestamp_in_millinsec(),
 21         (End-Start)/1000.
 22 
 23 init([])->
 24         State=ets:new(test,[protected,set,named_table]),
 25         ets:insert(State,{info,0}),
 26         ets:insert(State,{starttime,timestamp_in_millinsec()}),
 27         {ok,State}.
 28 handle_call({new,Num},_From,State)->
 29         Conns = ets:lookup_element(State,info,2),
 30         Sum = Conns + Num,
 31         ets:insert(State,{info,Sum}),
 32         {reply,ok,State}.
 33 handle_cast({new,Num},State)->
 34         Conns = ets:lookup_element(State,info,2),
 35         Sum = Conns + Num,
 36         ets:insert(State,{info,Sum}),
 37         {noreply,State}.
 38 handle_info(_info,State)->
 39         {noreply,State}.
 40         
 41 terminate(_Reaseon,_State)->
 42         ok.     
 43 code_change(_OldVsn,State,_Exra)->
 44         {ok,State}.
 45         
 46 timestamp_in_millinsec()->
 47         {MegaSec, Sec, MicoSec} = erlang:now(),
 48         MegaSec * 1000000000 + Sec*1000 +MicoSec div 1000.


%%通过 ! 发送消息来实现 insert

-module(test_ets1).
-export([start/2,insert_ets_p/3]).
-compile(export_all).
start(Num,Process)->
        Tab=new_ets(),
        Be=timestamp_in_millinsec(),
        [begin spawn(?MODULE,insert_ets_p,[Num,self(),Tab]),recv(Num,Process) end || _X <- lists:seq(1,Process)],
%%      recv(Num,Process),
        En=timestamp_in_millinsec(),
        Re=En -Be,
        Re.
recv(Num,_Process)->
        receive
                R ->
                        insert_ets(Num,R),
                        ets:insert(test,{endt,timestamp_in_millinsec()}),
                        io:format("recv---~p~n",[R])
        after 100 ->
                        io:format("timeout~n")
        end.

insert_ets_p(Num,From,Tab)->
        io:format("------~p----~n",[From]),
        [From ! Tab || _X<- lists:seq(1,Num)].
new_ets()->
        Tab = ets:new(test,[protected,set,named_table]),
        ets:insert(Tab,{info,0,self()}),
        ets:insert(Tab,{start,timestamp_in_millinsec()}),
        Tab.
insert_ets(Num,Tab)->
        Conns = ets:lookup_element(test,info,2),
        Sum = Conns+Num,
        io:format("fffffff====~p====~p==",[Sum,self()]),
        %%ets:insert(test,{info,0}).
        ets:insert(Tab,{info,Sum,self()}).

timestamp_in_millinsec()->
        {MegaSec, Sec, MicoSec} = erlang:now(),
        MegaSec * 1000000000 + Sec*1000 +MicoSec div 1000.
~                                                          


多个线程共享ets导致出现脏数据
-module(test_ets).
-export([start/2,insert_ets_p/3]).
-compile(export_all).
start(Num,Process)->
        Tab=new_ets(),
        Be=timestamp_in_millinsec(),
        [spawn(?MODULE,insert_ets_p,[Num,self(),Tab]) || _X <- lists:seq(1,Process)],
        recv(Num,Process),
        En=timestamp_in_millinsec(),
        Re=En -Be,
        Re.
recv(_Num,_Process)->
        receive
                R -> io:format("recv---~p~n",[R])
        after 100 ->
                io:format("timeout~n")
        end.

insert_ets_p(Num,From,Tab)->
        io:format("-----~p--------~n-",[From]),
        [insert_ets(1,Tab) || _X<- lists:seq(1,Num)],
        From ! test.
new_ets()->
        Tab = ets:new(test,[public,set,named_table]),
        ets:insert(Tab,{info,0}),
        Tab.
insert_ets(Num,Tab)->
        Conns = ets:lookup_element(test,info,2),
        Sum = Conns+Num,
        io:format("fffffff====~p=====~p======~n",[Sum,self()]),
        %%ets:insert(test,{info,0}).
        ets:insert(Tab,{info,Sum}).

timestamp_in_millinsec()->
        {MegaSec, Sec, MicoSec} = erlang:now(),
        MegaSec * 1000000000 + Sec*1000 +MicoSec div 1000.
~                                                                          

你可能感兴趣的:(erlang)