Skip to content

Instantly share code, notes, and snippets.

@kaos
Created September 13, 2013 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaos/6556874 to your computer and use it in GitHub Desktop.
Save kaos/6556874 to your computer and use it in GitHub Desktop.
erlydtl scanner benchmark
#!/usr/bin/env escript
%% -*- mode: erlang -*-
%% Usage: ./bench <run time in seconds> <template file to scan>
%% Defaults to 2 seconds on 'bench.dtl'.
main([]) ->
run_benchmark(2, "bench.dtl");
main([Time]) ->
run_benchmark(list_to_integer(Time), "bench.dtl");
main([Time, File]) ->
run_benchmark(list_to_integer(Time), File).
run_benchmark(Time, File) ->
{ok, Bin} = file:read_file(File),
Template = binary_to_list(Bin),
io:format("Running benchmarks for ~b seconds on ~b bytes (~s)...~n", [Time, length(Template), File]),
New = run_benchmark("new scanner", Time, Template, fun erlydtl_scanner:scan/1),
Old = run_benchmark("old scanner", Time, Template, fun erlydtl_scanner:scan_old/1),
results(New, Old),
io:format("Done.~n~n").
run_benchmark(Title, Time, Data, Scanner) ->
io:format("Running ~s, hold on... ", [Title]),
{Start, _} = statistics(wall_clock),
{{Count, Min, Max}, End} = run_scans({0, 16#ffff, 0}, Start + (Time * 1000), Data, Scanner),
io:format("ok~n"),
Tot = (End - Start) / 1000,
Speed = ((Count * length(Data)) / Tot) / 1024,
{Count, Tot, Min / 1000, Max / 1000, Tot / Count, Speed}.
run_scans({Count, Min, Max}, End, Data, Scanner) ->
{ok, _} = Scanner(Data),
{Time, Dur} = statistics(wall_clock),
Stats = {Count + 1, erlang:min(Min, Dur), erlang:max(Max, Dur)},
if Time < End -> run_scans(Stats, End, Data, Scanner);
true -> {Stats, Time}
end.
results(New, Old) ->
print_stats("New scanner", New),
print_stats("Old scanner", Old),
io:format("Deltas~n"),
[compare_stats(T, N, O)
|| {T, N, O} <- lists:zip3(
["Count", "Total time", "Min", "Max", "Avg", "Kbytes/s"],
tuple_to_list(New), tuple_to_list(Old))].
print_stats(Title, {Count, Tot, Min, Max, Avg, Speed}) ->
io:format("~s scanned ~6b times in ~.3f seconds @ ~.1f Kbytes/sec~n"
" min ~.3f max ~.3f avg ~.6f~n",
[Title, Count, Tot, Speed, Min, Max, Avg]).
compare_stats(Title, New, Old) ->
io:format(" ~.10s: ~12.3f", [Title, (New - Old)/1]),
if Old /= 0 ->
io:format(" ~6.1f%~n", [100 * ((New - Old)/Old)]);
true ->
io:format(" ~~%~n")
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment