Skip to content

Instantly share code, notes, and snippets.

@tracy-o
Created June 6, 2024 14:43
Show Gist options
  • Select an option

  • Save tracy-o/d1d8ddef84a0d0603fe12bee8870ec14 to your computer and use it in GitHub Desktop.

Select an option

Save tracy-o/d1d8ddef84a0d0603fe12bee8870ec14 to your computer and use it in GitHub Desktop.
diff --git a/benchmark/condition_pattern_matching.ex b/benchmark/condition_pattern_matching.ex
deleted file mode 100644
index 5c006c1ae..000000000
--- a/benchmark/condition_pattern_matching.ex
+++ /dev/null
@@ -1,44 +0,0 @@
-defmodule Benchmark.ConditionPatternMatching do
- alias Belfrage.{Envelope, Envelope.Private, Envelope.Request}
- use Belfrage.Platforms
- import Fixtures.Envelope
-
- def run(_), do: experiment()
-
- def experiment() do
- benchmark_append_allowlists()
- end
-
- defp benchmark_append_allowlists do
- envelope = envelope_with_gzip_resp()
- matching_envelope = Envelope.add(envelope, :private, %{route_state_id: {"SomeRouteSpec", @webcore}})
-
- Benchee.run(
- %{
- "when condition maybe_add_mvt_allowlists/1" => fn envelope -> maybe_add_mvt_allowlists(envelope) end,
- "if condition maybe_add_mvt_allowlists/1" => fn envelope -> maybe_add_mvt_allowlists_with_if(envelope) end,
- "pattern match maybe_add_mvt_allowlists/1" => fn envelope -> maybe_add_mvt_allowlists_pattern_match(envelope) end
- },
- inputs: %{
- "envelope to not append" => envelope,
- "envelope to append" => matching_envelope
- },
- time: 10,
- memory_time: 2
- )
- end
-
- defp maybe_add_mvt_allowlists(envelope) when envelope.private.route_state_id == nil, do: :add
- defp maybe_add_mvt_allowlists(_envelope), do: :no_add
-
- defp maybe_add_mvt_allowlists_pattern_match(%Envelope{private: %Private{route_state_id: nil}}), do: :add
- defp maybe_add_mvt_allowlists_pattern_match(_envelope), do: :no_add
-
- defp maybe_add_mvt_allowlists_with_if(envelope) do
- if envelope.private.route_state_id == nil do
- :add
- else
- :no_add
- end
- end
-end
diff --git a/benchmark/envelope_add.ex b/benchmark/envelope_add.ex
deleted file mode 100644
index f4ff99fbe..000000000
--- a/benchmark/envelope_add.ex
+++ /dev/null
@@ -1,35 +0,0 @@
-defmodule Benchmark.EnvelopeAdd do
- import Fixtures.Envelope
-
- def run(_) do
- experiment()
- end
-
- def experiment() do
- benchmark_add_query_params()
- end
-
- defp benchmark_add_query_params do
- envelope = envelope_with_gzip_resp()
-
- Benchee.run(
- %{
- "elixir add query params allowlist" => fn params -> envelope_add(envelope, :request, %{query_params: params}) end,
- "erlang add query params allowlist" => fn params -> envelope_add_erlang(envelope, :request, %{query_params: params}) end
- },
- inputs: %{
- "query params" => %{"a" => "value", "b" => "value", "c" => "value"}
- },
- time: 10,
- memory_time: 2
- )
- end
-
- defp envelope_add(envelope, key, values) do
- Map.put(envelope, key, Map.merge(Map.get(envelope, key), values))
- end
-
- defp envelope_add_erlang(envelope, key, values) do
- :maps.put(key, :maps.merge(values, :maps.get(key, envelope, nil)), envelope)
- end
-end
diff --git a/benchmark/filter_request_data.ex b/benchmark/filter_request_data.ex
deleted file mode 100644
index e58b90548..000000000
--- a/benchmark/filter_request_data.ex
+++ /dev/null
@@ -1,58 +0,0 @@
-defmodule Benchmark.FilterRequestData do
- @moduledoc """
- This benchmark measures the performance of the functions called in `Processor.allowlists/1`.
-
- ### To run this experiment
- ```
- $ mix benchmark filter_request_data
- ```
- """
-
- alias Belfrage.{Allowlist, Envelope, Processor, Personalisation}
-
- @request_envelope %Envelope{
- private: %Envelope.Private{route_state_id: "ProxyPass", personalised_route: true, headers_allowlist: ["header1"]},
- request: %Envelope.Request{host: "www.bbc.co.uk", raw_headers: %{"cookie" => "cookie1"}}
- }
-
- def run(_) do
- {:ok, _started} = Application.ensure_all_started(:belfrage)
- experiment()
- end
-
- def experiment() do
- benchmark_filter_request_data_functions()
- benchmark_filter_request_data_pipeline()
- end
-
- defp benchmark_filter_request_data_functions do
- Benchee.run(
- %{
- "Personalisation.append_allowlists/1" => fn -> Personalisation.append_allowlists(@request_envelope) end,
- "Processor.maybe_add_mvt_allowlists/1" => fn -> Processor.maybe_add_mvt_allowlists(@request_envelope) end,
- "Allowlist.QueryParams.filter/1" => fn -> Allowlist.QueryParams.filter(@request_envelope) end,
- "Allowlist.Cookies.filter/1" => fn -> Allowlist.Cookies.filter(@request_envelope) end,
- "Allowlist.Headers.filter/1" => fn -> Allowlist.Headers.filter(@request_envelope) end
- },
- time: 10,
- memory_time: 2
- )
- end
-
- defp benchmark_filter_request_data_pipeline do
- Benchee.run(
- %{
- "filter_request_data" => fn ->
- @request_envelope
- |> Personalisation.append_allowlists()
- |> Processor.maybe_add_mvt_allowlists()
- |> Allowlist.QueryParams.filter()
- |> Allowlist.Cookies.filter()
- |> Allowlist.Headers.filter()
- end
- },
- time: 10,
- memory_time: 2
- )
- end
-end
diff --git a/benchmark/list_add.ex b/benchmark/list_add.ex
deleted file mode 100644
index 200489d7b..000000000
--- a/benchmark/list_add.ex
+++ /dev/null
@@ -1,36 +0,0 @@
-defmodule Benchmark.ListAdd do
- @string_list ["a", "b", "c"]
-
- def run(_) do
- experiment()
- end
-
- def experiment() do
- benchmark_list_addition()
- end
-
- defp benchmark_list_addition do
- addition = generate_random_word_list(50)
-
- Benchee.run(
- %{
- "Kernel.++/2" => fn addition -> @string_list ++ addition end,
- "Enum.concat/2" => fn addition -> Enum.concat(@string_list, addition) end,
- "list prepend" => fn addition -> [addition | @string_list] end,
- },
- inputs: %{
- "0 addition" => [],
- "1 addition" => Enum.take(addition, 1),
- "5 addition" => Enum.take(addition, 5),
- "10 addition" => Enum.take(addition, 10),
- "50 addition" => addition
- },
- time: 10,
- memory_time: 2
- )
- end
-
- defp generate_random_word_list(count, word_size \\ 3) do
- Enum.map(0..count, fn _i -> Base.encode64(:crypto.strong_rand_bytes(word_size)) end)
- end
-end
diff --git a/benchmark/map_get.ex b/benchmark/map_get.ex
deleted file mode 100644
index 2bc9a3ff5..000000000
--- a/benchmark/map_get.ex
+++ /dev/null
@@ -1,51 +0,0 @@
-defmodule Benchmark.MapGet do
- @moduledoc """
- This benchmark measures the performance of the Elixir's `Map.get/3` against Erlang's equivelant, `maps:get/3`.
-
- ### To run this experiment
- ```
- $ mix benchmark map_get
- ```
- """
-
- import Fixtures.Envelope
-
- def run(_) do
- experiment()
- end
-
- def experiment() do
- benchmark_map_get_envelope()
- benchmark_map_get()
- end
-
- defp benchmark_map_get_envelope do
- envelope = envelope_with_gzip_resp()
-
- Benchee.run(
- %{
- "Map.get/3 envelope" => fn -> Map.get(envelope, :private) end,
- ":maps.get/3 envelope" => fn -> :maps.get(:private, envelope) end,
- "map.key envelope" => fn -> envelope.private end
- },
- time: 10,
- memory_time: 2
- )
- end
-
- defp benchmark_map_get do
- Benchee.run(
- %{
- "Map.get/2" => fn map -> Map.get(map, :b) end,
- "Map.get/2 nil" => fn map -> Map.get(map, :fake_key) end,
- ":maps.get/3" => fn map -> :maps.get(:b, map, nil) end,
- ":maps.get/3 nil" => fn map -> :maps.get(:fake_key, map, nil) end
- },
- inputs: %{
- "map" => %{a: "a", b: "b", c: 3}
- },
- time: 10,
- memory_time: 2
- )
- end
-end
diff --git a/benchmark/map_put.ex b/benchmark/map_put.ex
deleted file mode 100644
index 6c9b71434..000000000
--- a/benchmark/map_put.ex
+++ /dev/null
@@ -1,66 +0,0 @@
-defmodule Benchmark.MapPut do
- @moduledoc """
- This benchmark measures the performance of the Elixir's `Map.put/3` against Erlang's equivelant, `maps:put/3`.
-
- ### To run this experiment
- ```
- $ mix benchmark map_put
- ```
- """
-
- import Fixtures.Envelope
- alias Belfrage.Envelope
-
- @map_keys Enum.map(1..100, fn i -> "param" <> to_string(i) end)
- @query_params Map.from_keys(@map_keys, "value")
-
- def run(_) do
- experiment()
- end
-
- def experiment() do
- benchmark_map_put_envelope()
- benchmark_envelope_add()
- end
-
- defp benchmark_map_put_envelope do
- query_params = Enum.take_random(@query_params, 3)
- inputs = allowlists()
-
- Benchee.run(
- %{
- "Map.put/3 envelope" => fn envelope -> Map.put(envelope, :query_params, query_params) end,
- ":maps.put/3 envelope" => fn envelope -> :maps.put(:query_params, query_params, envelope) end
- },
- inputs: inputs,
- time: 10
- )
- end
-
- defp benchmark_envelope_add do
- query_params = Enum.take_random(@query_params, 3)
- inputs = allowlists()
-
- Benchee.run(
- %{
- "Map.put/3 envelope" => fn envelope ->
- Map.put(envelope, :query_params, Map.merge(Map.get(envelope.private, :query_params), query_params))
- end,
- ":maps.put/3 envelope" => fn envelope ->
- :maps.put(:query_params, Map.merge(envelope.private.query_params, query_params), envelope)
- end
- },
- inputs: inputs,
- time: 10
- )
- end
-
- defp allowlists(envelope \\ envelope_with_gzip_resp(), keys \\ @map_keys) do
- %{
- "0 allowlist envelope" => envelope,
- "1 allowlist envelope" => Envelope.add(envelope, :private, %{query_params_allowlist: Enum.take_random(keys, 1)}),
- "5 allowlist envelope" => Envelope.add(envelope, :private, %{query_params_allowlist: Enum.take_random(keys, 5)}),
- "20 allowlist envelope" => Envelope.add(envelope, :private, %{query_params_allowlist: Enum.take_random(keys, 20)})
- }
- end
-end
diff --git a/benchmark/map_take.ex b/benchmark/map_take.ex
deleted file mode 100644
index cf8c4130a..000000000
--- a/benchmark/map_take.ex
+++ /dev/null
@@ -1,54 +0,0 @@
-defmodule Benchmark.MapTake do
- @moduledoc """
- This benchmark measures the performance of the Elixir's `Map.take/2` against Erlang's equivelant, `maps:with/2`.
-
- ### To run this experiment
- ```
- $ mix benchmark map_take
- ```
- """
- @map_keys Enum.map(1..100, fn i -> "param" <> to_string(i) end)
- @test_map Map.from_keys(@map_keys, "value")
-
- def run(_) do
- experiment()
- end
-
- def experiment() do
- benchmark_map_take()
- benchmark_map_take_empty()
- end
-
- defp benchmark_map_take do
- Benchee.run(
- %{
- "Map.take/2" => fn allowlist -> Map.take(@test_map, allowlist) end,
- ":maps.with/2" => fn allowlist -> :maps.with(allowlist, @test_map) end
- },
- inputs: allowlists(),
- time: 10,
- memory_time: 2
- )
- end
-
- defp benchmark_map_take_empty do
- Benchee.run(
- %{
- "Map.take/2 empty map" => fn allowlist -> Map.take(%{}, allowlist) end,
- ":maps.with/2 empty map" => fn allowlist -> :maps.with(allowlist, %{}) end
- },
- inputs: allowlists(),
- time: 10,
- memory_time: 2
- )
- end
-
- defp allowlists(keys \\ @map_keys) do
- %{
- "0 allowlist" => [],
- "1 allowlist" => Enum.take_random(keys, 1),
- "5 allowlist" => Enum.take_random(keys, 5),
- "20 allowlist" => Enum.take_random(keys, 20)
- }
- end
-end
diff --git a/benchmark/string_match.ex b/benchmark/string_match.ex
deleted file mode 100644
index d411d6bf6..000000000
--- a/benchmark/string_match.ex
+++ /dev/null
@@ -1,36 +0,0 @@
-defmodule Benchmark.StringMatch do
- @host "belfrage.api.test.bbc.co.uk"
-
- def run(_), do: experiment()
-
- def experiment() do
- benchmark_string_starts_with()
- benchmark_string_ends_with()
- end
-
- defp benchmark_string_starts_with do
- Benchee.run(
- %{
- "String.starts_with?/2" => fn -> String.starts_with?(@host, "belfrage.api.") end,
- "binary.part ==" => fn -> :binary.part(@host, 0, 13) == "belfrage.api." end,
- "binary.longest_common_prefix/1 ==" => fn -> :binary.longest_common_prefix([@host, <<"belfrage.api.">>]) == 13 end,
- "Kernel.=~/2" => fn -> @host =~ ~r/^belfrage\.api\./ end
- },
- time: 10,
- memory_time: 2
- )
- end
-
- defp benchmark_string_ends_with do
- Benchee.run(
- %{
- "String.ends_with?/2" => fn -> String.ends_with?(@host, "bbc.co.uk") end,
- "binary.part ==" => fn -> :binary.part(@host, byte_size(@host), -9) == "bbc.co.uk" end,
- "binary.longest_common_suffix/1 ==" => fn -> :binary.longest_common_suffix([@host, <<"bbc.co.uk">>]) == 9 end,
- "Kernel.=~/2" => fn -> @host =~ ~r/bbc\.co\.uk$/ end
- },
- time: 10,
- memory_time: 2
- )
- end
-end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment