Created
September 26, 2016 20:23
-
-
Save orzen/54828d5a62ce9ca986bebf35f36ba0b2 to your computer and use it in GitHub Desktop.
ertorrent_hash_benchmark.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(readex). | |
-export([calc_hash/1, | |
read_case0/2, | |
pread2_case0/2, | |
benchmark/0]). | |
calc_hash(Data) -> | |
% Hash the binary part | |
<<Hash_int:160/integer>> = crypto:hash(sha, Data), | |
% Convert the hash to a string representation | |
lists:flatten(io_lib:format("~40.16.0b", [Hash_int])). | |
read_test(Fd, Piece_length, Acc, Time) -> | |
case file:read(Fd, Piece_length) of | |
{ok, Data} when Piece_length > byte_size(Data) -> | |
Hash = calc_hash(Data), | |
io:format("Hash time: ~w~n", [Time/100000]), | |
{ok, lists:reverse(Acc), {rest, Hash, Data}}; | |
{ok, Data} -> | |
{Time0, Hash} = timer:tc(?MODULE, calc_hash, [Data]), | |
read_test(Fd, Piece_length, [Hash|Acc], Time+Time0); | |
% Hash = calc_hash(Data), | |
% read_test(Fd, Piece_length, [Hash|Acc], 1); | |
eof -> | |
{ok, Acc, {rest, 0}}; | |
{error, Reason} -> | |
io:format("Error occured: ~s~n", [Reason]) | |
end. | |
pread_test0(Fd, File, Piece_length) -> | |
File_size = filelib:file_size(File), | |
Num_pieces = trunc(File_size/Piece_length), | |
Remaining_bytes = File_size rem Piece_length, | |
Locations = lists:seq(0, Piece_length*Num_pieces, Piece_length), | |
Loc_nums = [{Loc, Num} || Loc <- Locations, Num <- [Piece_length]], | |
Rest = {Num_pieces * Piece_length, Remaining_bytes}, | |
Loc_nums_rest = lists:append(Loc_nums, [Rest]), | |
case file:pread(Fd, Loc_nums_rest) of | |
{ok, DataL} -> | |
Hashes = lists:map(fun(X) -> calc_hash(X) end, DataL), | |
{ok, Hashes}; | |
eof -> | |
{ok, eof}; | |
{error, Reason} -> | |
io:format("Error occured: ~s~n", [Reason]) | |
end. | |
read_case0(File, Piece_length) -> | |
{ok, Fd} = file:open(File, [binary]), | |
read_test(Fd, Piece_length, [], 0). | |
pread2_case0(File, Piece_length) -> | |
{ok, Fd} = file:open(File, [binary]), | |
pread_test0(Fd, File, Piece_length). | |
benchmark() -> | |
% Size 1.7G | |
File0 = "", | |
Size0 = filelib:file_size(File0), | |
% Size 60M | |
File1 = "", | |
Size1 = filelib:file_size(File1), | |
Piece_length0 = 8192, | |
Piece_length1 = 16384, | |
Piece_length2 = 65536, | |
Piece_length3 = 252144, | |
Piece_length4 = 4194304, | |
% File0 | |
{Time0, _Value0} = timer:tc(?MODULE, read_case0, [File0, Piece_length0]), | |
{Time1, _Value1} = timer:tc(?MODULE, pread2_case0, [File0, Piece_length0]), | |
{Time2, _Value2} = timer:tc(?MODULE, read_case0, [File0, Piece_length1]), | |
{Time3, _Value3} = timer:tc(?MODULE, pread2_case0, [File0, Piece_length1]), | |
{Time4, _Value4} = timer:tc(?MODULE, read_case0, [File0, Piece_length2]), | |
{Time5, _Value5} = timer:tc(?MODULE, pread2_case0, [File0, Piece_length2]), | |
{Time6, _Value6} = timer:tc(?MODULE, read_case0, [File0, Piece_length3]), | |
{Time7, _Value7} = timer:tc(?MODULE, pread2_case0, [File0, Piece_length3]), | |
{Time8, _Value8} = timer:tc(?MODULE, read_case0, [File0, Piece_length4]), | |
{Time9, _Value9} = timer:tc(?MODULE, pread2_case0, [File0, Piece_length4]), | |
% File1 | |
{Time10, _Value10} = timer:tc(?MODULE, read_case0, [File1, Piece_length0]), | |
{Time11, _Value11} = timer:tc(?MODULE, pread2_case0, [File1, Piece_length0]), | |
{Time12, _Value12} = timer:tc(?MODULE, read_case0, [File1, Piece_length1]), | |
{Time13, _Value13} = timer:tc(?MODULE, pread2_case0, [File1, Piece_length1]), | |
{Time14, _Value14} = timer:tc(?MODULE, read_case0, [File1, Piece_length2]), | |
{Time15, _Value15} = timer:tc(?MODULE, pread2_case0, [File1, Piece_length2]), | |
{Time16, _Value16} = timer:tc(?MODULE, read_case0, [File1, Piece_length3]), | |
{Time17, _Value17} = timer:tc(?MODULE, pread2_case0, [File1, Piece_length3]), | |
{Time18, _Value18} = timer:tc(?MODULE, read_case0, [File1, Piece_length4]), | |
{Time19, _Value19} = timer:tc(?MODULE, pread2_case0, [File1, Piece_length4]), | |
io:format("test case 0~n file size: ~w~n piece size: ~w~n~n", [Size0, Piece_length0]), | |
io:format("read/2 result: ~w~n", [Time0/100000]), | |
io:format("pread/2 result: ~w~n", [Time1/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 1~n file size: ~w~n piece size: ~w~n~n", [Size0, Piece_length1]), | |
io:format("read/2 result: ~w~n", [Time2/100000]), | |
io:format("pread/2 result: ~w~n", [Time3/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 2~n file size: ~w~n piece size: ~w~n~n", [Size0, Piece_length2]), | |
io:format("read/2 result: ~w~n", [Time4/100000]), | |
io:format("pread/2 result: ~w~n", [Time5/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 3~n file size: ~w~n piece size: ~w~n~n", [Size0, Piece_length3]), | |
io:format("read/2 result: ~w~n", [Time6/100000]), | |
io:format("pread/2 result: ~w~n", [Time7/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 4~n file size: ~w~n piece size: ~w~n~n", [Size0, Piece_length4]), | |
io:format("read/2 result: ~w~n", [Time8/100000]), | |
io:format("pread/2 result: ~w~n", [Time9/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 5~n file size: ~w~n piece size: ~w~n~n", [Size1, Piece_length0]), | |
io:format("read/2 result: ~w~n", [Time10/100000]), | |
io:format("pread/2 result: ~w~n", [Time11/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 6~n file size: ~w~n piece size: ~w~n~n", [Size1, Piece_length1]), | |
io:format("read/2 result: ~w~n", [Time12/100000]), | |
io:format("pread/2 result: ~w~n", [Time13/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 7~n file size: ~w~n piece size: ~w~n~n", [Size1, Piece_length2]), | |
io:format("read/2 result: ~w~n", [Time14/100000]), | |
io:format("pread/2 result: ~w~n", [Time15/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 8~n file size: ~w~n piece size: ~w~n~n", [Size1, Piece_length3]), | |
io:format("read/2 result: ~w~n", [Time16/100000]), | |
io:format("pread/2 result: ~w~n", [Time17/100000]), | |
io:format("~n--------------~n", []), | |
io:format("test case 9~n file size: ~w~n piece size: ~w~n~n", [Size1, Piece_length4]), | |
io:format("read/2 result: ~w~n", [Time18/100000]), | |
io:format("pread/2 result: ~w~n", [Time19/100000]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment