From 733f32861337daf65ffe19f9da215b3e4c34c48a Mon Sep 17 00:00:00 2001 From: AICells <1713699517@qq.com> Date: Fri, 19 Jun 2020 00:01:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/testCase/ListPdTs/utListPdTs.erl | 145 +++++++++++++++++++++++++++ src/testCase/ListPdTs/utListsTs.erl | 38 +++++++ src/testCase/ListPdTs/utPdTs.erl | 40 ++++++++ 3 files changed, 223 insertions(+) create mode 100644 src/testCase/ListPdTs/utListPdTs.erl create mode 100644 src/testCase/ListPdTs/utListsTs.erl create mode 100644 src/testCase/ListPdTs/utPdTs.erl diff --git a/src/testCase/ListPdTs/utListPdTs.erl b/src/testCase/ListPdTs/utListPdTs.erl new file mode 100644 index 0000000..8ceea05 --- /dev/null +++ b/src/testCase/ListPdTs/utListPdTs.erl @@ -0,0 +1,145 @@ +-module(utListPdTs). +-compile([export_all, nowarn_unused_function, nowarn_unused_vars, nowarn_export_all]). +-record(tempCnt, { + insert = [] + , read = [] + , update = [] + , for = [] + , delete = [] +}). + +-define(Cnt, 12). +-define(V_NUM, [8, 16, 32, 64, 128, 256, 516, 1024, 2048]). +-define(DsList, [utPdTs, utListTs]). + + + +start() -> + %%erlang:process_flag(trap_exit, true), + erlang:erase(), + printLog("Ts benchmark...", []), + + runTs(?DsList, ?V_NUM), + printLog("Ts benchmark...Over calculate the AVG~n", []), + runAvg(?DsList, ?V_NUM). + + +runTs([Ds | T], VNumList) -> + printTitle(), + runNum(VNumList, Ds), + runTs(T, VNumList); +runTs([], _VNumList) -> + ok. + +runNum([Num | T], Ds) -> + runCnt(?Cnt, Num, Ds), + runNum(T, Ds); +runNum([], _Ds) -> + ok. + +runCnt(0, Num, Ds) -> + ok; +runCnt(Cnt, Num, Ds) -> + runExe(Num, Ds), + runCnt(Cnt - 1, Num, Ds). + +runExe(Num, Ds) -> + Pid = erlang:spawn_link(Ds, start, [Num, self()]), + receive + {over, Pid, Insert, Read, Update, For, Delete} -> + storeStatistics(Ds, Num, Insert, Read, Delete), + {_, DsName} = lists:split(2, atom_to_list(Ds)), + printLog("~-10.s ~8.s ~12.s ~12.s ~10.s ~12.s ~10.s ~14.s ~10.s ~12.s ~12.s ~12.s ~n", + [DsName, integer_to_list(Num), timeToStr(Insert), calcPer(Insert, Num), timeToStr(Read), calcPer(Read, Num), timeToStr(Update), calcPer(Update, Num), timeToStr(For), calcPer(For, Num), timeToStr(Delete), calcPer(Delete, Num)]); + {'EXIT', Pid, normal} -> + ok; + _ShutDown -> + io:format("Ds test shutDown ~p ~p ~p ~n", [Ds, Num, _ShutDown]) + end. + +runAvg([Ds | T], VNumList) -> + printAvg(), + runCal(VNumList, Ds), + runAvg(T, VNumList); +runAvg([], _VNumList) -> + ok. + +runCal([Num | T], Ds) -> + #tempCnt{insert = InsertList, read = ReadList, delete = DeleteList} = getStatistics(Ds, Num), + {_, DsName} = lists:split(2, atom_to_list(Ds)), + printLog("~-10.s ~8.s ~12.s ~12.s ~12.s~n", + [DsName, integer_to_list(Num), calcAvg(InsertList, Num), calcAvg(ReadList, Num), calcAvg(DeleteList, Num)]), + runCal(T, Ds); +runCal([], _Ds) -> + ok. + +-define(S, 1000000000). +-define(MS, 1000000). +-define(US, 1000). +-define(NS, 1). + +timeToStr(not_support) -> + <<"noSupport">>; +timeToStr(skip) -> + <<"skip">>; +timeToStr(Time) when Time > ?S -> + float_to_list(Time / ?S, [{decimals, 2}]) ++ "s"; +timeToStr(Time) when Time > ?MS -> + float_to_list(Time / ?MS, [{decimals, 2}]) ++ "ms"; +timeToStr(Time) when Time > ?US -> + float_to_list(Time / ?US, [{decimals, 2}]) ++ "us"; +timeToStr(Time) -> + integer_to_list(Time) ++ "ns". + +calcPer(not_support, _Num) -> + <<"notSupport">>; +calcPer(skip, _Num) -> + <<"skip">>; +calcPer(Time, Num) -> + float_to_list(Time / Num, [{decimals, 2}]) ++ "ns". + +calcAvg([not_support | _], Num) -> + <<"notSupport">>; +calcAvg([skip | _], Num) -> + <<"skip">>; +calcAvg(CntList, Num) -> + %% 去掉最大值与最小值 然后求平均值 + AvgCnt = ?Cnt - 2, + SortList = lists:sort(CntList), + AvgList = lists:sublist(SortList, 2, AvgCnt), + float_to_list(lists:sum(AvgList) / AvgCnt / Num, [{decimals, 2}]) ++ "ns". + +storeStatistics(Ds, Num, Insert, Read, Delete) -> + #tempCnt{insert = InsertList, read = ReadList, delete = DeleteList} = + case erlang:get({Ds, Num}) of + undefined -> + #tempCnt{}; + TempCnt -> + TempCnt + end, + NewTempCnt = #tempCnt{insert = [Insert | InsertList], read = [Read | ReadList], delete = [Delete | DeleteList]}, + erlang:put({Ds, Num}, NewTempCnt). + +getStatistics(Ds, Num) -> + erlang:get({Ds, Num}). + +printTitle() -> + printLog("~n~-10.s ~8.s ~12.s ~12.s ~10.s ~12.s ~12.s ~12.s ~n", + ["TsName", "V_Num", "insert", "insert/per", "read", "read/per", "delete", "delete/per"]), + printLog("~s ~n", [[$= || _ <- lists:seq(1, 145)]]). + +printAvg() -> + printLog("~n~-10.s ~8.s ~12.s ~12.s ~12.s~n", + ["DsName", "V_Num", "insert/per", "read/per", "delete/per"]), + printLog("~s ~n", [[$= || _ <- lists:seq(1, 85)]]). + +printLog(Format, Args) -> + % {ok, File} = file:open("src/docs/erlang-DsBenchMark.txt", [write, append]), + % io:format(File, Format, Args), + % file:close(File). + io:format(Format, Args). + + +makeV(N) -> + {N, test, [list, 123, 456.789, "test"], {23231, "gggggg"}, <<"12345678901234567890">>}. + diff --git a/src/testCase/ListPdTs/utListsTs.erl b/src/testCase/ListPdTs/utListsTs.erl new file mode 100644 index 0000000..40e87bb --- /dev/null +++ b/src/testCase/ListPdTs/utListsTs.erl @@ -0,0 +1,38 @@ +-module(utListsTs). +-compile([nowarn_unused_function, nowarn_unused_vars, nowarn_export_all]). + +-export([start/2]). + +start(Num, Pid) when Num =< 32768 -> + Ds = init(Num), + Time1 = erlang:system_time(nanosecond), + NewDsI = insert(Num, Ds), + Time2 = erlang:system_time(nanosecond), + NewDsU = read(Num, NewDsI), + Time3 = erlang:system_time(nanosecond), + delete(NewDsU), + Time4 = erlang:system_time(nanosecond), + erlang:send(Pid, {over, self(), Time2 - Time1, Time3 - Time2, Time4 - Time3}), + exit(normal); +start(_Num, Pid) -> + erlang:send(Pid, {over, self(), skip, skip, skip, skip, skip}), + exit(normal). + +init(_Num) -> + []. + +insert(0, Ds) -> + Ds; +insert(Num, Ds) -> + Value = utTestDs:makeV(Num), + insert(Num - 1, [Value | Ds]). + +read(_Num, Ds) -> + lists:reverse(Ds). + +delete([]) -> + ok; +delete([_H | T]) -> + delete(T). + + diff --git a/src/testCase/ListPdTs/utPdTs.erl b/src/testCase/ListPdTs/utPdTs.erl new file mode 100644 index 0000000..7759062 --- /dev/null +++ b/src/testCase/ListPdTs/utPdTs.erl @@ -0,0 +1,40 @@ +-module(utPdTs). +-compile([nowarn_unused_function, nowarn_unused_vars, nowarn_export_all]). + +-export([start/2]). + +start(Num, Pid) -> + Ds = init(Num), + Time1 = erlang:system_time(nanosecond), + NewDsI = insert(Num, Ds), + Time2 = erlang:system_time(nanosecond), + NewDsR = read(Num, NewDsI, undefined), + Time3 = erlang:system_time(nanosecond), + delete(Num, NewDsR), + Time4 = erlang:system_time(nanosecond), + erlang:send(Pid, {over, self(), Time2 - Time1, Time3 - Time2, Time4 - Time3}), + exit(normal). + +init(Num) -> + undefined. + +insert(0, Ds) -> + Ds; +insert(Num, Ds) -> + Value = utTestDs:makeV(Num), + erlang:put(Num, Value), + insert(Num - 1, Ds). + +read(0, Ds, _V) -> + Ds; +read(Num, Ds, _V) -> + V = erlang:get(Num), + read(Num - 1, Ds, V). + +delete(0, _Ds) -> + ok; +delete(Num, Ds) -> + erlang:erase(Num), + delete(Num - 1, Ds). + +