| 1 |
#!/usr/bin/env escript |
| 2 |
%% -*- erlang -*- |
| 3 |
|
| 4 |
-define(TEST_LIBRARIES, |
| 5 |
[ |
| 6 |
{"bbmustache", |
| 7 |
fun (Template, Data) -> |
| 8 |
bbmustache:render(Template, Data, [{key_type, binary}]) |
| 9 |
end}, |
| 10 |
{"mustache.erl", |
| 11 |
fun (Template, Data) -> |
| 12 |
KeyConvFun = fun(B) -> binary_to_atom(B, latin1) end, |
| 13 |
ValueConvFun = fun(V) when is_binary(V) -> binary_to_list(V); |
| 14 |
(O) -> O |
| 15 |
end, |
| 16 |
list_to_binary(mustache:render(binary_to_list(Template), |
| 17 |
dict:from_list(conv_recursive(Data, KeyConvFun, ValueConvFun)))) |
| 18 |
end} |
| 19 |
]). |
| 20 |
|
| 21 |
main(_) -> |
| 22 |
ok = code:add_pathsz(filelib:wildcard(filename:absname("_build/**/ebin"))), |
| 23 |
ok = file:set_cwd("benchmarks/.tmp"), |
| 24 |
|
| 25 |
Jsons = filelib:wildcard("../../_build/test/lib/mustache_spec/specs/*.json"), |
| 26 |
io:format("test files:~n"), |
| 27 |
[io:format(" ~s~n", [Json]) || Json <- Jsons], |
| 28 |
Result0 = main1(Jsons, []), |
| 29 |
Result = Result0 ++ [{<<"benches">>, bench_main()}], |
| 30 |
ok = file:write_file("../README.md", |
| 31 |
bbmustache:compile(bbmustache:parse_file("../output.mustache"), Result, [{key_type, binary}])). |
| 32 |
|
| 33 |
bench_main() -> |
| 34 |
Template = "Hello {{name}} You have just won {{value}} dollars! {{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}}", |
| 35 |
MapData = #{<<"name">> => "Chris", <<"value">> => 10000, <<"taxed_value">> => 10000 - (10000 * 0.4), <<"in_ca">> => true}, |
| 36 |
DictData = dict:from_list([{name, "Chris"}, {value, 10000}, {taxed_value, 10000 - (10000 * 0.4)}, {in_ca, true}]), |
| 37 |
Benches = [ |
| 38 |
{"bbmustache", fun(T, D) -> bbmustache:render(T, D, [{key_type, binary}]) end, |
| 39 |
list_to_binary(Template), MapData}, |
| 40 |
{"mustache.erl", fun(T, D) -> mustache:render(T, D) end, |
| 41 |
Template, DictData} |
| 42 |
], |
| 43 |
[ |
| 44 |
[{<<"library">>, Library}, |
| 45 |
{<<"result">>, bench_run(Render, T, D)}] || {Library, Render, T, D} <- Benches |
| 46 |
]. |
| 47 |
|
| 48 |
bench_run(Render, Template, Data) -> |
| 49 |
lists:sum([begin {T, _} = timer:tc(Render, [Template, Data]), T end || _ <- lists:seq(1, 1000)]). |
| 50 |
|
| 51 |
main1([], Result) -> |
| 52 |
[{<<"spec_files">>, lists:reverse(Result)}]; |
| 53 |
main1([JsonPath | Rest], Result) -> |
| 54 |
{ok, JsonBin} = file:read_file(JsonPath), |
| 55 |
JsonDec = jsone:decode(JsonBin, [{object_format, proplist}]), |
| 56 |
Tests = proplists:get_value(<<"tests">>, JsonDec), |
| 57 |
Ret = main2(Tests, []), |
| 58 |
main1(Rest, [[{<<"spec">>, filename:basename(JsonPath, ".json")}, |
| 59 |
{<<"libraries">>, [[{<<"library">>, L}] || {L, _} <- ?TEST_LIBRARIES]}, |
| 60 |
{<<"tests">>, Ret} |
| 61 |
] | Result]). |
| 62 |
|
| 63 |
main2([], Result) -> |
| 64 |
lists:reverse(Result); |
| 65 |
main2([Test | Tests], Result) -> |
| 66 |
Ret = [spec_test(Render, Test) || {_, Render} <- ?TEST_LIBRARIES], |
| 67 |
main2(Tests, [[ |
| 68 |
{<<"test">>, proplists:get_value(<<"name">>, Test)}, |
| 69 |
{<<"results">>, Ret} |
| 70 |
] | Result]). |
| 71 |
|
| 72 |
spec_test(Render, Assoc) -> |
| 73 |
Data0 = proplists:get_value(<<"data">>, Assoc), |
| 74 |
Data = case is_list(Data0) of |
| 75 |
false -> Data0; |
| 76 |
true -> |
| 77 |
case proplists:get_value(<<"lambda">>, Data0) of |
| 78 |
undefined -> Data0; |
| 79 |
Lambda -> [{<<"lambda">>, lambda(Lambda)} | proplists:delete(<<"lambda">>, Data0)] |
| 80 |
end |
| 81 |
end, |
| 82 |
Expected = proplists:get_value(<<"expected">>, Assoc), |
| 83 |
Template = proplists:get_value(<<"template">>, Assoc), |
| 84 |
Partials = proplists:get_value(<<"partials">>, Assoc, []), |
| 85 |
|
| 86 |
ok = clean_dir("."), |
| 87 |
ok = lists:foreach(fun(P) -> write_file(P) end, Partials), |
| 88 |
|
| 89 |
{Pid, Ref} = spawn_monitor(fun() -> Expected = Render(Template, Data) end), |
| 90 |
Result = receive |
| 91 |
{'DOWN', Ref, _, _, Reason} -> Reason =:= normal |
| 92 |
after 3000 -> |
| 93 |
exit(Pid, kill), |
| 94 |
demonitor(Ref, [flush]), |
| 95 |
false |
| 96 |
end, |
| 97 |
[{<<"result">>, Result}]. |
| 98 |
|
| 99 |
clean_dir(Dir) -> |
| 100 |
lists:foreach(fun(F) -> file:delete(F) end, |
| 101 |
filelib:wildcard(filename:join(Dir, "*.mustache"))). |
| 102 |
|
| 103 |
write_file({PartialFilename, PartialData}) -> |
| 104 |
ok = file:write_file(<<PartialFilename/binary, ".mustache">>, PartialData); |
| 105 |
write_file(_) -> |
| 106 |
ok. |
| 107 |
|
| 108 |
conv_recursive([{} | Rest], KeyConvFun, ValueConvFun) -> |
| 109 |
conv_recursive(Rest, KeyConvFun, ValueConvFun); |
| 110 |
conv_recursive([{_, _} | _] = AssocList, KeyConvFun, ValueConvFun) -> |
| 111 |
lists:foldl(fun({Key, [{_, _} | _] = Value}, Acc) -> |
| 112 |
[{KeyConvFun(Key), conv_recursive(Value, KeyConvFun, ValueConvFun)} | Acc]; |
| 113 |
({Key, Value}, Acc) when is_list(Value) -> |
| 114 |
[{KeyConvFun(Key), [conv_recursive(X, KeyConvFun, ValueConvFun) || X <- Value]} | Acc]; |
| 115 |
({Key, Value}, Acc) -> |
| 116 |
[{KeyConvFun(Key), ValueConvFun(Value)} | Acc] |
| 117 |
end, [], AssocList); |
| 118 |
conv_recursive(Other, _, _) -> |
| 119 |
Other. |
| 120 |
|
| 121 |
list_to_dict_recursive([{_, _} | _] = AssocList) -> |
| 122 |
lists:foldl(fun({Key, [{_, _} | _] = Value}, Dict) -> |
| 123 |
dict:store(Key, list_to_dict_recursive(Value), Dict); |
| 124 |
({Key, Value}, Dict) when is_list(Value) -> |
| 125 |
dict:store(Key, [list_to_dict_recursive(X) || X <- Value], Dict); |
| 126 |
({Key, Value}, Dict) -> |
| 127 |
dict:store(Key, Value, Dict) |
| 128 |
end, dict:new(), AssocList); |
| 129 |
list_to_dict_recursive(Other) -> |
| 130 |
Other. |
| 131 |
|
| 132 |
lambda(LambdaList) -> |
| 133 |
LambdaStr = binary_to_list(proplists:get_value(<<"erlang">>, LambdaList)), |
| 134 |
io:format("~s~n", [LambdaStr]), |
| 135 |
{ok, Token, _} = erl_scan:string(LambdaStr), |
| 136 |
{ok, [Form]} = erl_parse:parse_exprs(Token), |
| 137 |
{value, Fun, _} = erl_eval:expr(Form, erl_eval:new_bindings()), |
| 138 |
Fun. |