-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathets_vs_gen_server_write.exs
48 lines (38 loc) · 1.21 KB
/
ets_vs_gen_server_write.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
defmodule RetrieveState.Fast do
def put_state(ets_pid, state) do
:ets.insert(ets_pid, {:stored_state, state})
end
end
defmodule StateHolder do
use GenServer
def init(_), do: {:ok, %{}}
def start_link(state \\ []), do: GenServer.start_link(__MODULE__, state, name: __MODULE__)
def put_state(value), do: GenServer.call(__MODULE__, {:put_state, value})
def handle_call({:put_state, value}, _from, state), do: {:reply, true, Map.put(state, :stored_state, value)}
end
defmodule RetrieveState.Medium do
def put_state(value) do
StateHolder.put_state(value)
end
end
defmodule RetrieveState.Slow do
def put_state(value) do
:persistent_term.put(:stored_state, value)
end
end
defmodule RetrieveState.Benchmark do
def benchmark do
ets_pid = :ets.new(:state_store, [:set, :public])
StateHolder.start_link()
Benchee.run(
%{
"ets table" => fn -> RetrieveState.Fast.put_state(ets_pid, :returned_value) end,
"gen server" => fn -> RetrieveState.Medium.put_state(:returned_value) end,
"persistent term" => fn -> RetrieveState.Slow.put_state(:returned_value) end
},
time: 10,
print: [fast_warning: false]
)
end
end
RetrieveState.Benchmark.benchmark()