From ec67c2379f48728af30fd18c39426e4bfd8cea19 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sat, 19 Aug 2017 22:56:17 +0200 Subject: [PATCH] Add various benchmarks regarding URI creation --- bench/uris.exs | 90 +++++++++++++++++++++++++++++++++++++++++++++++ config/config.exs | 2 +- mix.exs | 4 ++- mix.lock | 5 ++- 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 bench/uris.exs diff --git a/bench/uris.exs b/bench/uris.exs new file mode 100644 index 0000000..face871 --- /dev/null +++ b/bench/uris.exs @@ -0,0 +1,90 @@ +hash = %{"http://example.com/foo/bar" => RDF.uri("http://example.com/foo/bar")} + +Benchee.run(%{ + "URI.parse" => fn -> + RDF.uri("http://example.com/foo/bar") + end, + "RDF.IRI.new" => fn -> + RDF.IRI.new("http://example.com/foo/bar") + end, + "RDF.IRI.new!" => fn -> + RDF.IRI.new!("http://example.com/foo/bar") + end, + "hash lookup" => fn -> + hash["http://example.com/foo/bar"] + end, +}) + + +Benchee.run(%{ + "bare uri" => fn -> + RDF.uri("http://example.com/foo/bar") + end, + "bare triple" => fn -> + RDF.triple({RDF.uri("http://example.com/foo/bar"), + RDF.uri("http://example.com/foo/baz"), + RDF.uri("http://example.com/foo/quux") + }) + end, + "Graph with triple" => fn -> + RDF.Graph.new( + RDF.triple({RDF.uri("http://example.com/foo/bar"), + RDF.uri("http://example.com/foo/baz"), + RDF.uri("http://example.com/foo/quux") + }) + ) + end, +}) + +Benchee.run(%{ + "1_000_000 bare uris" => fn -> + for i <- 1..1_000_000 do + RDF.uri("http://example.com/foo/bar#{i}") + end + end, + "1_000_000 bare triples" => fn -> + for i <- 1..1_000_000 do + RDF.triple({RDF.uri("http://example.com/foo/bar#{i}"), + RDF.uri("http://example.com/foo/baz#{i}"), + RDF.uri("http://example.com/foo/quux#{i}") + }) + end + end, + "Graph with 1_000_000 triples" => fn -> + Enum.reduce 1..1_000_000, RDF.Graph.new(), fn i, graph -> + RDF.Graph.add graph, + RDF.triple({RDF.uri("http://example.com/foo/bar#{i}"), + RDF.uri("http://example.com/foo/baz#{i}"), + RDF.uri("http://example.com/foo/quux#{i}") + }) + end + end, +}) + + +IO.puts "\n\nErlang Term Info (Memory Consumption)" + +uris = + for i <- 1..1_000_000 do + RDF.uri("http://example.com/foo/bar#{i}") + end +IO.puts "1_000_000 bare uris: #{:erlang_term.byte_size(uris)}" + +triples = + for i <- 1..1_000_000 do + RDF.triple({RDF.uri("http://example.com/foo/bar#{i}"), + RDF.uri("http://example.com/foo/baz#{i}"), + RDF.uri("http://example.com/foo/quux#{i}") + }) + end +IO.puts "1_000_000 bare triples: #{:erlang_term.byte_size(triples)}" + +graph = + Enum.reduce 1..1_000_000, RDF.Graph.new(), fn i, graph -> + RDF.Graph.add graph, + RDF.triple({RDF.uri("http://example.com/foo/bar#{i}"), + RDF.uri("http://example.com/foo/baz#{i}"), + RDF.uri("http://example.com/foo/quux#{i}") + }) + end +IO.puts "Graph with 1_000_000 triples: #{:erlang_term.byte_size(graph)}" diff --git a/config/config.exs b/config/config.exs index f40c2f6..a0f5a31 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,5 +1,5 @@ use Mix.Config -unless Mix.env == :docs do +unless Mix.env in ~w[docs bench]a do import_config "#{Mix.env}.exs" end diff --git a/mix.exs b/mix.exs index cb6a306..4b53509 100644 --- a/mix.exs +++ b/mix.exs @@ -64,7 +64,9 @@ defmodule RDF.Mixfile do {:ex_doc, "~> 0.14", only: :dev, runtime: false}, {:mix_test_watch, "~> 0.3", only: :dev, runtime: false}, {:excoveralls, "~> 0.7", only: :test}, - {:inch_ex, only: :docs}, + {:inch_ex, "~> 0.5", only: [:dev, :test]}, + {:benchee, "~> 0.9", only: :bench}, + {:erlang_term, "~> 1.7", only: :bench}, ] end end diff --git a/mix.lock b/mix.lock index c86a041..dbc16b2 100644 --- a/mix.lock +++ b/mix.lock @@ -1,8 +1,11 @@ -%{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []}, +%{"benchee": {:hex, :benchee, "0.9.0", "433d946b0e4755e186fe564568ead4f593b0d15337fcffa95ed7d5b8a6612670", [:mix], [{:deep_merge, "~> 0.1", [hex: :deep_merge, optional: false]}]}, + "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []}, "certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], []}, "credo": {:hex, :credo, "0.8.1", "137efcc99b4bc507c958ba9b5dff70149e971250813cbe7d4537ec7e36997402", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]}, + "deep_merge": {:hex, :deep_merge, "0.1.1", "c27866a7524a337b6a039eeb8dd4f17d458fd40fbbcb8c54661b71a22fffe846", [:mix], []}, "dialyxir": {:hex, :dialyxir, "0.5.0", "5bc543f9c28ecd51b99cc1a685a3c2a1a93216990347f259406a910cf048d1d7", [:mix], []}, "earmark": {:hex, :earmark, "1.2.2", "f718159d6b65068e8daeef709ccddae5f7fdc770707d82e7d126f584cd925b74", [:mix], []}, + "erlang_term": {:hex, :erlang_term, "1.7.1", "f83d470b27e1fabdbd19fb91489d3394a05813ea69643ad4354572700d715aac", [:rebar3], []}, "ex_doc": {:hex, :ex_doc, "0.16.1", "b4b8a23602b4ce0e9a5a960a81260d1f7b29635b9652c67e95b0c2f7ccee5e81", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]}, "excoveralls": {:hex, :excoveralls, "0.7.2", "f69ede8c122ccd3b60afc775348a53fc8c39fe4278aee2f538f0d81cc5e7ff3a", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]}, "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},