summaryrefslogtreecommitdiff
path: root/test/samples/update_counter.exs
blob: 1d8e837e50bf51c421d22478e0318cce15b00828 (plain) (blame)
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
:ets.new(:limiter_bench, [:public, :named_table])

:ets.new(:limiter_bench_concurrent, [
  :public,
  :named_table,
  {:read_concurrency, false},
  {:write_concurrency, true}
])

atomics = :atomics.new(1, [])

update_counter = %{
  "ets:update_counter" => fn ->
    :ets.update_counter(:limiter_bench, "bench", {2, 1}, {"bench", 0})
  end,
  "ets:update_counter concurrent" => fn ->
    :ets.update_counter(:limiter_bench, "bench", {2, 1}, {"bench", 0})
  end,
  "atomics:add_get" => fn ->
    :atomics.add_get(atomics, 1, 1)
  end
}

Benchee.run(update_counter, parallel: 1)
Benchee.run(update_counter, parallel: System.schedulers_online())