erlang操作MySQL和ets内存的性能测试

ERLANG操作mysql数据库性能测试

时间/毫秒

1W/1

10W/1

50W/1

100W/1

INSERT

4306

35958

189681

285401

UPDATE

5273

57502

276449

576845

SELECT

9797

96892

465241

883871

DELETE

5414

55614

295825

592211

 

第一次的测试数据,不准确,但流程就是这样

mysql中插入(insert)一条数据平均消耗了399微秒,

更新(update) 一条记录平均消耗558.5微秒、

---------查询最耗时,select一条记录平均要937.5微秒、

删除一条记录平均消耗了571.2微秒。

====加入了io:format()....

 

352,392,332,330,(insert,update,select,elete)mysql数据库的平均消耗时间

========删掉打印消息代码后的数据

 

ETS效率测试

 

Set

Ordered_set

Bag

Duplicate_bag

New()

2.81

3.71

2.96

2.66

Insert()

0.16

0.31

0.16

1.4

Lookup()

0.15

0.16

0.15

0.16

 

Ets四种类型的表在创建一个新表时时间都差不多,在2~3微秒之间,

异键表插入消耗时间是恒定的,插入数据消耗0.2微秒左右,有序异键表的消耗时间和该表的数据条目有关,需要对异键表排序。

-------同键表插入一条数据0.2微秒,duplicate_bag要1.5微秒左右(书上说的是副本同键表耗时比同键表少,因为在使用同键表时会与所有相同的键的元素去做比较,但大量的元组都有相同的键时,消耗的时间更大)

四种表查询的时间也都差不多,在半微秒内、0.2微秒之间

 

代码:

-module(connet_mysql).
-export([start/0,connect_mysql/0,change_mysql/2]).
-export([make_ets/1,create_ets/1,insert/2,look_data/2]).
% -export([insert_data/0,update_data/0,delete_data/0,select_data/0]).

%%---------------------------------------------------------------------------------------%%
%%------------------------------- ERLANG操作mysql数据库性能测试 ----------------------%%
%%---------------------------------------------------------------------------------------%%

%%===================================================================
%% insert,select(用主键索引),update(用主键索引),
%% delete(用主键索引) 1w, 10, 50,100w条记录分别耗时多少。
%%===================================================================

start() ->
mysql:start_link(test,"localhost","root","lengnuan","village").
% mysql:start_link(test,"localhost",3306,"root","lengnuan","village",undefined,utf8).

%%===================================================================
%% 连接MySQL
%%===================================================================
connect_mysql() ->
mysql:connect(test,localhost,3306,root,lengnuan,village,true).

%%====================================================================
%% 操作数据库
%%====================================================================
change_mysql(N,Action) ->
statistics(runtime),
statistics(wall_clock),
I = 1,
case Action of
insert ->
fac(1,N,fun(I) -> insert_data(I) end);
update ->
fac(1,N,fun(I) -> update_data(I) end);
select ->
fac(1,N,fun(I) -> select_data(I) end);
delete ->
fac(1,N,fun(I) -> delete_data(I) end)
end,
{_,Time} = statistics(runtime),
{_,Time1} = statistics(wall_clock),
U1 = Time * 1000,
% io:format("===========------------===============~s",[S]),
io:format("use time ~p-------~p~n",[U1,Time1]).

% fac(N,N,Fun) -> Fun(N);
% fac(I,N,Fun) ->
% Fun(I),
% fac(I + 1,N ,Fun).

%%=======================================================================
%% 在表中插入数据
%%=======================================================================
insert_data(I) ->
io:format("===~p~n",[I]),
mysql:fetch(
test,
[<<"insert into person(age,name) values(18,'wangfugui')">>]
).

%%=======================================================================
%% 更改表中数据
%%=======================================================================
update_data(I) ->
% io:format("===~p~n",[I]),
Sql = io_lib:format("update person set age = '30' where id = ~w ",[I]),
Result = mysql:fetch(
test,
[Sql]
).
% io:format("===result ==: ~p~n",[Result]).

%%=======================================================================
%% 查看表中的数据
%%=======================================================================
select_data(I) ->
Sql = io_lib:format("select * from person where id = ~w ",[I]),
% io:format("===~p~n",[Sql]),
Result = mysql:fetch(
test,
[Sql]
).
% io:format("is =============:~p~n",[Result]).
% test,

%%=======================================================================
%% 删除表中的数据
%%=======================================================================
delete_data(I) ->
% io:format("===~p~n",[I]),
Sql = io_lib:format("delete from person where id = ~w ",[I]),
% Sql = "delete from person where id = 35",
Result = mysql:fetch(
test,
[Sql]
).
% io:format("=====~p~n",[Result]).

% SQL1 = io_lib:format("replace into `game_info`(`cf_name`, `cf_value`) values ('version', from_unixtime(~w, '%Y-%m-%d-%H-%i-%S'))", [StartTimeN]),

%%----------------------------------------------------------------------------------------%%
%%---------------------------------------ets性能测试---------------------------------------%%
%%----------------------------------------------------------------------------------------%%
make_ets(N) ->
%%创建一个标识符为Result的ets表
Result = ets:new(?MODULE,[bag]),

%插入数据
% {Time,_} = timer:tc(fun() ->insert_value(Result) end),
% io:format("use time ~p~n",[Time/73]),
statistics(runtime),
L = insert_value(Result),
{_,Time} = statistics(runtime),
io:format(" insert ets use time :~p microsecond ~p~n",[Time * 1000/20000,L]),

R = ets:all(),
io:format(" all ets is :~p ",[R]),

% %%创建新的ets用时
% {Ref1,_} = timer:tc(?MODULE,create_ets,[N]),
% io:format(" create ets use time :~p microsecond ~n",[Ref1/N]),
% create_ets(N),

% %在ets中插入数据用时
% % {Ref2,Value} = timer:tc(?MODULE,insert,[N,Result]),
% % io:format(" insert use time :~p microsecond,~p~n",[Ref2/N,Value]),
% insert(N,Result),

% %% 查询ets表用时
% % {Ref3,Value1} = timer:tc(?MODULE,look_data,[N,Result]),
% % io:format(" lookup use time :~p microsecond ~p~n",[Ref3/N,Value1]),
% look_data(N,Result),

%%删除ets表
ets:delete(Result).

%%创建ets表
create_ets(N) ->
statistics(runtime),
fac(1,N,fun() -> ets:new(?MODULE,[bag]) end),
{_,Time} = statistics(runtime),
io:format(" create ets use time :~p microsecond ~n",[Time * 1000/N]).

%% 在ets中插入数据
insert(N,Result) ->
statistics(runtime),
L = fac(1,N,fun() -> ets:insert(Result,{person,3}) end),
{_,Time} = statistics(runtime),
io:format(" insert ets use time :~p microsecond ~p~n",[Time * 1000/N,L]),
L.
% N,
% ets:insert(Result,{person,3}).

%% 查询ets某个表
look_data(N,Result) ->
statistics(runtime),
L = fac(1,N,fun() -> ets:lookup(Result,person8) end),
{_,Time} = statistics(runtime),
io:format(" look ets use time :~p microsecond ~p~n",[Time * 1000/N,L]),
L.

fac(N,N,Fun) -> Fun();
fac(I,N,Fun) ->
Fun(),
fac(I + 1,N ,Fun).

猜你喜欢

转载自www.cnblogs.com/ye-jing/p/12705836.html
ets
今日推荐