Skip to content

Instantly share code, notes, and snippets.

@nickva
Last active April 18, 2016 15:25
Show Gist options
  • Save nickva/34c6c295a33772652b126eabab40641c to your computer and use it in GitHub Desktop.
Save nickva/34c6c295a33772652b126eabab40641c to your computer and use it in GitHub Desktop.
Erlang tool to benchmark writing n blocks of size s (for a total of n*s bytes). Optionally issue fsync after each write
#!/usr/bin/env escript
%% -*- erlang -*-
%
% File writer benchmarking tool
%
% chmod a+x make sure escript is there, then run as:
% $ ./dderl block_count block_size fsync|nofsync
%
% Example:
% $ ./dderl 10000 4096 fsync
% fsync 10000*4096B = 40960000B, Rate:0.37MB/s Time:104.614
-moulde(compile).
main([BCntStr, BSizeStr, FSyncStr]) ->
BCnt = list_to_integer(BCntStr),
BSize = list_to_integer(BSizeStr),
AllowedFSync = [fsync, nofsync],
FSync = list_to_atom(FSyncStr),
case lists:member(FSync, AllowedFSync) of
true -> ok;
false -> usage()
end,
FName = fname(),
FD = open(FName),
Data = data(BSize),
{TimeUSec, _Value} = timer:tc(fun() -> write_n(FD, Data, FSync, BCnt) end),
file:close(FD),
file:delete(FName),
TimeSec = TimeUSec / 1000000.0,
Total = BCnt * BSize,
RateMBps = (Total / TimeSec) / (1024 * 1024),
io:format("~w ~w*~wB = ~wB, Rate:~.2fMB/s Time:~.3f~nsec",
[FSync, BCnt, BSize, Total, RateMBps, TimeSec]),
ok;
main(_) ->
usage().
usage() ->
io:format("usage: " ++ escript:script_name() ++ " BCnt BSize fsync|nofsync~n"),
halt(1).
fname()->
escript:script_name() ++ ".out".
open(FN) ->
{ok, FD} = file:open(FN, [write, raw]),
FD.
data(N) ->
binary:list_to_bin([1 || _ <- lists:seq(1, N)]).
write_n(_, _, _, 0) ->
ok;
write_n(FD, Data, FSync, N) ->
ok = file:write(FD, Data),
case FSync of
fsync -> ok = file:sync(FD);
nofsync -> ok
end,
write_n(FD, Data, FSync, N-1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment