Skip to content

Instantly share code, notes, and snippets.

@ulissesalmeida
Created April 27, 2019 09:25
Show Gist options
  • Save ulissesalmeida/b260d9330f368995b63d7635a4561161 to your computer and use it in GitHub Desktop.
Save ulissesalmeida/b260d9330f368995b63d7635a4561161 to your computer and use it in GitHub Desktop.
Elixir Application Config performance
defmodule Kloak do
use GenServer
@normal_table :"#{__MODULE__}.Config"
@concurrent_table :"#{__MODULE__}.ConfigConcurrent"
@config [json_library: Jason,
ciphers: [
default:
{Cloak.Ciphers.AES.GCM,
tag: "AES.GCM.V1", key: Base.decode64!("3Jnb0hZiHIzHTOih7t2cTEPEpY98Tu1wvQkPfq/XwqE=")},
secondary:
{Cloak.Ciphers.AES.CTR,
tag: "AES.CTR.V1", key: Base.decode64!("o5IzV8xlunc0m0/8HNHzh+3MCBBvYZa0mv4CsZic5qI=")}
]]
def init(args) do
{:ok, args}
end
def start_link() do
{:ok, pid} = GenServer.start_link(__MODULE__, [], name: __MODULE__)
:ets.new(@normal_table, [:named_table, :protected])
:ets.new(@concurrent_table, [:named_table, :protected, read_concurrency: true])
:ets.insert(@normal_table, {:config, @config})
:ets.insert(@concurrent_table, {:config, @config})
{:ok, pid}
end
def read_config() do
:ets.lookup(@normal_table, :config)
end
def read_concurrent_config() do
:ets.lookup(@concurrent_table, :config)
end
def read_application_env() do
Application.get_env(:myapp, MyVault)
end
end
Application.put_env(:myapp, MyVault, [json_library: Jason,
ciphers: [
default:
{Cloak.Ciphers.AES.GCM,
tag: "AES.GCM.V1", key: Base.decode64!("3Jnb0hZiHIzHTOih7t2cTEPEpY98Tu1wvQkPfq/XwqE=")},
secondary:
{Cloak.Ciphers.AES.CTR,
tag: "AES.CTR.V1", key: Base.decode64!("o5IzV8xlunc0m0/8HNHzh+3MCBBvYZa0mv4CsZic5qI=")}
]])
Kloak.start_link()
# Benchee.run(
# %{
# "Kloak.read_config/0" => fn ->
# for _i <- 1..1000 do
# Kloak.read_config()
# end
# end,
# "Kloak.read_concurrent_config/0" => fn ->
# for _i <- 1..1000 do
# Kloak.read_concurrent_config()
# end
# end,
# "Kloak.read_application_env/0" => fn ->
# for _i <- 1..1000 do
# Kloak.read_application_env()
# end
# end
# },
# warmup: 5,
# time: 10,
# memory_time: 1
# )
# Operating System: macOS
# CPU Information: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
# Number of Available Cores: 12
# Available memory: 16 GB
# Elixir 1.8.1
# Erlang 21.1
#
# Benchmark suite executing with the following configuration:
# warmup: 5 s
# time: 10 s
# memory time: 1 s
# parallel: 1
# inputs: none specified
# Estimated total run time: 48 s
#
# Benchmarking Kloak.read_application_env/0...
# Benchmarking Kloak.read_concurrent_config/0...
# Benchmarking Kloak.read_config/0...
#
# Name ips average deviation median 99th %
# Kloak.read_config/0 3.99 K 250.40 μs ±24.17% 290.99 μs 373.99 μs
# Kloak.read_concurrent_config/0 3.36 K 297.81 μs ±25.47% 325.99 μs 510.99 μs
# Kloak.read_application_env/0 2.16 K 463.02 μs ±17.43% 433.99 μs 783.99 μs
#
# Comparison:
# Kloak.read_config/0 3.99 K
# Kloak.read_concurrent_config/0 3.36 K - 1.19x slower +47.42 μs
# Kloak.read_application_env/0 2.16 K - 1.85x slower +212.62 μs
#
# Memory usage statistics:
#
# Name Memory usage
# Kloak.read_config/0 578.22 KB
# Kloak.read_concurrent_config/0 578.22 KB - 1.00x memory usage +0 KB
# Kloak.read_application_env/0 664.30 KB - 1.15x memory usage +86.09 KB
#
# **All measurements for memory usage were the same**
Benchee.run(
%{
"Kloak.read_config/0" => fn ->
for _i <- 1..1000 do
Kloak.read_config()
end
end,
"Kloak.read_concurrent_config/0" => fn ->
for _i <- 1..1000 do
Kloak.read_concurrent_config()
end
end,
"Kloak.read_application_env/0" => fn ->
for _i <- 1..1000 do
Kloak.read_application_env()
end
end
},
warmup: 5,
parallel: 12,
time: 10,
memory_time: 1
)
#
# Operating System: macOS
# CPU Information: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
# Number of Available Cores: 12
# Available memory: 16 GB
# Elixir 1.8.1
# Erlang 21.1
#
# Benchmark suite executing with the following configuration:
# warmup: 5 s
# time: 10 s
# memory time: 1 s
# parallel: 12
# inputs: none specified
# Estimated total run time: 48 s
#
# Benchmarking Kloak.read_application_env/0...
# Benchmarking Kloak.read_concurrent_config/0...
# Benchmarking Kloak.read_config/0...
#
# Name ips average deviation median 99th %
# Kloak.read_concurrent_config/0 1081.08 0.92 ms ±32.31% 0.91 ms 1.70 ms
# Kloak.read_config/0 738.80 1.35 ms ±27.82% 1.36 ms 2.21 ms
# Kloak.read_application_env/0 633.68 1.58 ms ±37.28% 1.45 ms 4.09 ms
#
# Comparison:
# Kloak.read_concurrent_config/0 1081.08
# Kloak.read_config/0 738.80 - 1.46x slower +0.43 ms
# Kloak.read_application_env/0 633.68 - 1.71x slower +0.65 ms
#
# Memory usage statistics:
#
# Name Memory usage
# Kloak.read_concurrent_config/0 578.22 KB
# Kloak.read_config/0 578.22 KB - 1.00x memory usage +0 KB
# Kloak.read_application_env/0 664.30 KB - 1.15x memory usage +86.09 KB
#
# **All measurements for memory usage were the same**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment