Skip to content

Instantly share code, notes, and snippets.

@akoutmos
Created December 22, 2022 16:28
Show Gist options
  • Save akoutmos/31bf59623b6c70826a2ef42d1d877577 to your computer and use it in GitHub Desktop.
Save akoutmos/31bf59623b6c70826a2ef42d1d877577 to your computer and use it in GitHub Desktop.
Livebook + Benchee example
# Untitled notebook
```elixir
Mix.install([
{:kino_benchee, github: "livebook-dev/kino_benchee"},
{:benchee,
github: "bencheeorg/benchee", branch: "akoutmos-adding-table-support", override: true}
])
```
## Section
```elixir
defmodule RetrieveState.Fast do
def get_state(ets_pid) do
:ets.lookup(ets_pid, :stored_state)
end
end
defmodule StateHolder do
use GenServer
def init(_), do: {:ok, %{stored_state: :returned_state}}
def start_link(state \\ []), do: GenServer.start_link(__MODULE__, state, name: __MODULE__)
def get_state(key), do: GenServer.call(__MODULE__, {:get_state, key})
def handle_call({:get_state, key}, _from, state), do: {:reply, state[key], state}
end
defmodule RetrieveState.Slow do
def get_state do
StateHolder.get_state(:stored_state)
end
end
defmodule RetrieveState.Benchmark do
def benchmark do
ets_pid = :ets.new(:state_store, [:set, :public])
:ets.insert(ets_pid, {:stored_state, :returned_state})
StateHolder.start_link()
Benchee.run(
%{
"ets table" => fn -> RetrieveState.Fast.get_state(ets_pid) end,
"gen server" => fn -> RetrieveState.Slow.get_state() end
},
time: 3,
memory_time: 3,
reduction_time: 3,
print: [fast_warning: false]
)
end
end
RetrieveState.Benchmark.benchmark()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment