-module(binary_test). -compile(export_all). -define(NL_PAT, binary:compile_pattern(<<"\n">>)). -define(CM_PAT, binary:compile_pattern(<<",">>)). read_csv() -> {ok, Data} = file:read_file("med.csv"), Data. tester(F, Data, Result) -> receive {From,data,NewData} -> From ! ok, tester(F, NewData, Result); {From,gc} -> garbage_collect(), From ! ok, tester(F, Data, Result); {From,run} -> NewResult = ?MODULE:F(Data), From ! ok, tester(F, Data, NewResult); {From,shutdown} -> From ! ok end. show_memory(Msg) -> [{total,Total}, _, {processes_used,PU}, _, _, _, {binary,Bin}, _, _] = erlang:memory(), MB = 1024 * 1024, TotalMB = Total / MB, PUMB = PU / MB, BinMB = Bin / MB, io:format("*** ~s~n",[Msg]), io:format(" ~20s | ~20s | ~20s ~n",["total", "processes used", "binary"]), io:format(" ~18.2fMB | ~18.2fMB | ~18.2fMB~n~n",[TotalMB, PUMB, BinMB]). do_run(Data, TestFun) -> S = self(), io:format("==========RUNNING ~-40..=s~n",[TestFun]), show_memory("Start"), Pid = spawn_opt(?MODULE, tester, [TestFun, undefined, undefined], [{fullsweep_after, 0}]), Pid ! {S,data,Data}, wait(), show_memory("After send data"), Pid ! {S,run}, wait(), show_memory("After run"), Pid ! {S,gc}, wait(), show_memory("After GC"), Pid ! {S,shutdown}, wait(), show_memory("After shutdown"), io:format("==========DONE ~-43..=s~n~n",[TestFun]). wait() -> receive ok -> ok; _ -> uh_oh end. %% break CSV out into individual columns %% e.g. %% %% [[<<"col1">>,<<"col2">>,<<"col3">>], %% [<<"val1">>,<<"val2">>,<<"val3">>]] list_of_cols(Data) -> NL = ?NL_PAT, CM = ?CM_PAT, list_of_cols(Data, [], {NL, CM}). list_of_cols(<<>>, Acc, _) -> lists:reverse(Acc); list_of_cols(Data, Acc, {NL, CM}) -> [Line,Rest] = binary:split(Data, NL), Cols = binary:split(Line, CM, [global]), list_of_cols(Rest, [Cols|Acc], {NL, CM}). list_of_lines(Data) -> NL = ?NL_PAT, list_of_lines(Data, [], NL). list_of_lines(<<>>, Acc, _) -> lists:reverse(Acc); list_of_lines(Data, Acc, NL) -> [Line,Rest] = binary:split(Data, NL), list_of_lines(Rest, [Line|Acc], NL). list_of_byte_size(Data, Size) -> list_of_byte_size(Data, Size, []). list_of_byte_size(<<>>, _, Acc) -> lists:reverse(Acc); list_of_byte_size(Data, Size, Acc) when size(Data) >= Size -> <> = Data, list_of_byte_size(Rest, Size, [Bytes|Acc]); list_of_byte_size(<>, _, Acc) -> lists:reverse([LastChunk|Acc]). list_of_32byte(Data) -> list_of_byte_size(Data, 32). list_of_64byte(Data) -> list_of_byte_size(Data, 64). list_of_128byte(Data) -> list_of_byte_size(Data, 128). list_of_256byte(Data) -> list_of_byte_size(Data, 256). list_of_512byte(Data) -> list_of_byte_size(Data, 512). run_tests() -> Data = read_csv(), show_memory("After read"), do_run(Data, list_of_cols), do_run(Data, list_of_lines), do_run(Data, list_of_32byte), do_run(Data, list_of_64byte), do_run(Data, list_of_128byte), do_run(Data, list_of_256byte), do_run(Data, list_of_512byte).