Last active
March 20, 2018 06:42
-
-
Save qcam/6edc7f8a92340492b2eba73f5f12f0fa to your computer and use it in GitHub Desktop.
Benchmarking XML parser in Erlang/Elixir
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
<?xml version="1.0" encoding="UTF-8"?> | |
<breakfast_menu> | |
<food> | |
<name>Belgian Waffles</name> | |
<price>$5.95</price> | |
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> | |
<calories>650</calories> | |
</food> | |
<food> | |
<name>Strawberry Belgian Waffles</name> | |
<price>$7.95</price> | |
<description>Light Belgian waffles covered with strawberries and whipped cream</description> | |
<calories>900</calories> | |
</food> | |
<food> | |
<name>Berry-Berry Belgian Waffles</name> | |
<price>$8.95</price> | |
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> | |
<calories>900</calories> | |
</food> | |
<food> | |
<name>French Toast</name> | |
<price>$4.50</price> | |
<description>Thick slices made from our homemade sourdough bread</description> | |
<calories>600</calories> | |
</food> | |
<food> | |
<name>Homestyle Breakfast</name> | |
<price>$6.95</price> | |
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> | |
<calories>950</calories> | |
</food> | |
</breakfast_menu> |
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
defmodule EventHandler do | |
# Handler for Saxy | |
def handle_event(event_type, event_data, acc) do | |
[{event_type, event_data} | acc] # this return is wrong, it has to be {:ok, acc} | |
end | |
def handle_entity_reference(reference_name) do | |
[?&, reference_name, ?;] | |
end | |
end | |
run_count = 10_000 | |
IO.puts "Benchmarking Saxy ..." | |
total_spent = Enum.reduce(1..run_count, 0, fn _, result -> | |
payload = File.read!("./sample.xml") | |
{run_spent, _result} = :timer.tc(Saxy, :parse_string, [payload, EventHandler, nil]) | |
result + run_spent | |
end) | |
IO.puts(total_spent / run_count) # microseconds per run | |
IO.puts "Benchmarking xmerl ..." | |
total_spent = Enum.reduce(1..run_count, 0, fn _, result -> | |
{run_spent, _result} = :timer.tc(:xmerl_sax_parser, :file, ["./sample.xml", []]) | |
result + run_spent | |
end) | |
IO.puts(total_spent / run_count) # microseconds per run | |
IO.puts "Benchmarking Erlsom ..." | |
total_spent = Enum.reduce(1..run_count, 0, fn _, result -> | |
payload = File.read!("./sample.xml") | |
{run_spent, _result} = :timer.tc(:erlsom, :parse_sax, [payload, nil, fn _, _ -> nil end]) | |
result + run_spent | |
end) | |
IO.puts(total_spent / run_count) # microseconds per run |
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
defmodule EventHandler do | |
# Handler for Saxy | |
def handle_event(event_type, event_data, acc) do | |
{:ok, [{event_type, event_data} | acc]} | |
end | |
def handle_entity_reference(reference_name) do | |
[?&, reference_name, ?;] | |
end | |
# Handler for Erlsom | |
def handle_event(event, acc) do | |
[event | acc] | |
end | |
end | |
run_count = 10_000 | |
IO.puts "Benchmarking Saxy ..." | |
total_spent = Enum.reduce(1..run_count, 0, fn _, result -> | |
payload = File.read!("./sample.xml") | |
{run_spent, _result} = :timer.tc(Saxy, :parse_string, [payload, EventHandler, nil]) | |
result + run_spent | |
end) | |
IO.puts(total_spent / run_count) # microseconds per run | |
IO.puts "Benchmarking xmerl ..." | |
total_spent = Enum.reduce(1..run_count, 0, fn _, result -> | |
{run_spent, _result} = :timer.tc(:xmerl_sax_parser, :file, ["./sample.xml", []]) | |
result + run_spent | |
end) | |
IO.puts(total_spent / run_count) # microseconds per run | |
IO.puts "Benchmarking Erlsom ..." | |
total_spent = Enum.reduce(1..run_count, 0, fn _, result -> | |
payload = File.read!("./sample.xml") | |
{run_spent, _result} = :timer.tc(:erlsom, :parse_sax, [payload, nil, fn _, _ -> nil end]) | |
result + run_spent | |
end) | |
IO.puts(total_spent / run_count) # microseconds per run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment