Current section
Files
Jump to
Current section
Files
lib/bench.exs
defmodule Bench do
def run do
template = File.read!(Path.join(__DIR__, "../fixture/template_1.hbs"))
payload = File.read!(Path.join(__DIR__, "../fixture/template_1.json")) |> Poison.decode!()
# template =
# "Hello {{name}} You have just won {{value}} dollars! {{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}}"
# payload = %{
# "name" => "Chris",
# "value" => 10000,
# "taxed_value" => 10000 - 10000 * 0.4,
# "in_ca" => true
# }
bbmustache_template = :bbmustache.parse_binary(template)
ex_mustache_template = ExMustache.parse(template)
if :bbmustache.compile(bbmustache_template, payload, key_type: :binary) !=
ExMustache.render(ex_mustache_template, payload)
|> IO.iodata_to_binary() do
IO.puts(:bbmustache.compile(bbmustache_template, payload, key_type: :binary))
IO.puts("------------------")
IO.puts(
ExMustache.render(ex_mustache_template, payload)
|> IO.iodata_to_binary()
)
raise "Error"
end
# data =
# ExMustache.render(ex_mustache_template, payload)
# |> IO.iodata_to_binary()
# File.write!("test.html", data)
# bench_compile(template)
bench_render(template, payload)
# bench_complete(template, payload)
nil
end
# @template File.read!(Path.join(__DIR__, "../fixture/template.hbs"))
# def test_run do
# ex_mustache_template = ExMustache.parse(@template)
# end
def bench_compile(template) do
Benchee.run(
%{
"compile: ex_mustache" => fn ->
ExMustache.parse(template)
end,
"compile: bbmustache" => fn ->
:bbmustache.parse_binary(template)
end
},
time: 10,
memory_time: 2,
formatters: [{Benchee.Formatters.Console, extended_statistics: true}]
)
end
def bench_render(template, payload) do
bbmustache_template = :bbmustache.parse_binary(template)
ex_mustache_template = ExMustache.parse(template)
Benchee.run(
%{
"render: ex_mustache" => fn ->
ExMustache.render(ex_mustache_template, payload)
|> IO.iodata_to_binary()
end,
"render: bbmustache" => fn ->
:bbmustache.compile(bbmustache_template, payload, key_type: :binary)
end
},
time: 10,
memory_time: 2,
formatters: [{Benchee.Formatters.Console, extended_statistics: true}]
)
end
def bench_complete(template, payload) do
Benchee.run(
%{
"complate: ex_mustache" => fn ->
ExMustache.parse(template)
|> ExMustache.render(payload)
|> IO.iodata_to_binary()
end,
"complate: bbmustache" => fn ->
:bbmustache.render(template, payload, key_type: :binary)
end
},
time: 10,
memory_time: 2,
formatters: [{Benchee.Formatters.Console, extended_statistics: true}]
)
end
end