Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement cache using persistent_terms #3169

Merged
merged 1 commit into from
Jul 5, 2021

Conversation

NelsonVides
Copy link
Collaborator

@NelsonVides NelsonVides commented Jul 2, 2021

TL;DR: it looks like in more or less all cases, the ets implementation is 2.8x slower and uses 1.6x more memory.


Using the following scenarios:

## TTL = 1, Users = 2^12, simulate tables rotating faster in relation to the queries
:mod_cache_users.start("ets", [ttl: 1, number_of_segments: 5])
:mod_cache_users_pt.start("pt", [ttl: 1, number_of_segments: 5])
Benchee.run(
  %{
    "ets cache" => fn num -> :bench_erl.fun_ets(num) end,
    "persistent term" => fn num -> :bench_erl.fun_pt(num) end
  },
  inputs: %{
    "some" => :rand.uniform(4096)
  },
  parallel: 12,
  time: 10,
  memory_time: 10
)
:mod_cache_users.stop("ets")
:mod_cache_users_pt.stop("pt")

## TTL = 60, Users = 2^12 simulates tables rotating very slowly
:mod_cache_users.start("ets", [ttl: 60, number_of_segments: 5])
:mod_cache_users_pt.start("pt", [ttl: 60, number_of_segments: 5])
Benchee.run(
  %{
    "ets cache" => fn num -> :bench_erl.fun_ets(num) end,
    "persistent term" => fn num -> :bench_erl.fun_pt(num) end
  },
  inputs: %{
    "some" => :rand.uniform(4096)
  },
  parallel: 12,
  time: 10,
  memory_time: 10
)
:mod_cache_users.stop("ets")
:mod_cache_users_pt.stop("pt")


## TTL = 1, Users = 2^20
:mod_cache_users.start("ets", [ttl: 1, number_of_segments: 5])
:mod_cache_users_pt.start("pt", [ttl: 1, number_of_segments: 5])
Benchee.run(
  %{
    "ets cache" => fn num -> :bench_erl.fun_ets(num) end,
    "persistent term" => fn num -> :bench_erl.fun_pt(num) end
  },
  inputs: %{
    "some" => :rand.uniform(1048576)
  },
  parallel: 12,
  time: 10,
  memory_time: 10
)
:mod_cache_users.stop("ets")
:mod_cache_users_pt.stop("pt")

## TTL = 1, Users = 2^20
:mod_cache_users.start("ets", [ttl: 60, number_of_segments: 5])
:mod_cache_users_pt.start("pt", [ttl: 60, number_of_segments: 5])
Benchee.run(
  %{
    "ets cache" => fn num -> :bench_erl.fun_ets(num) end,
    "persistent term" => fn num -> :bench_erl.fun_pt(num) end
  },
  inputs: %{
    "some" => :rand.uniform(1048576)
  },
  parallel: 12,
  time: 10,
  memory_time: 10
)
:mod_cache_users.stop("ets")
:mod_cache_users_pt.stop("pt")

where the code benchmarking code looks like

fun_ets(N) ->
    Jid = gen_user(N, <<"ets">>),
    Host = element(3, Jid),
    Bool = mod_cache_users:does_cached_user_exist(false, Host, Jid, stored),
    mod_cache_users:maybe_put_user_into_cache(Bool, Host, Jid, stored).

fun_pt(N) ->
    Jid = gen_user(N, <<"pt">>),
    Host = element(3, Jid),
    Bool = mod_cache_users_pt:does_cached_user_exist(false, Host, Jid, stored),
    mod_cache_users_pt:maybe_put_user_into_cache(Bool, Host, Jid, stored).

gen_user(N, Host) ->
    User = <<"user_", (integer_to_binary(N))/binary>>,
    {jid, User, Host, <<>>, User, Host, <<>>}.

and the benchmarked code looks like the old and new mod_cache_users but removing MIM's implementation details,

Results are as follows:

17:53:44.097 [error] [tag: :mod_cache_users_ets, what: :DEBUG_MESSAGE]

17:53:44.103 [error] [tag: :mod_cache_users_pt, what: :DEBUG_MESSAGE]

Operating System: Linux
CPU Information: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Number of Available Cores: 12
Available memory: 30.99 GB
Elixir 1.11.0
Erlang 23.3.3

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 10 s
parallel: 12
inputs: some
Estimated total run time: 44 s

Benchmarking ets cache with input some...
Benchmarking persistent term with input some...

##### With input some #####
Name                      ips        average  deviation         median         99th %
persistent term      324.37 K        3.08 μs  ±1163.51%        2.79 μs        3.69 μs
ets cache            114.62 K        8.72 μs   ±373.83%        7.98 μs       21.99 μs

Comparison:
persistent term      324.37 K
ets cache            114.62 K - 2.83x slower +5.64 μs

Memory usage statistics:

Name                    average  deviation         median         99th %
persistent term           480 B     ±0.00%          480 B          480 B
ets cache              768.00 B     ±0.02%          768 B          768 B

Comparison:
persistent term           480 B
ets cache              768.00 B - 1.60x memory usage +288.00 B

17:55:16.678 [error] [tag: :mod_cache_users_ets, what: :DEBUG_MESSAGE]

17:55:16.678 [error] [tag: :mod_cache_users_pt, what: :DEBUG_MESSAGE]
Operating System: Linux
CPU Information: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Number of Available Cores: 12
Available memory: 30.99 GB
Elixir 1.11.0
Erlang 23.3.3

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 10 s
parallel: 12
inputs: some
Estimated total run time: 44 s

Benchmarking ets cache with input some...
Benchmarking persistent term with input some...

##### With input some #####
Name                      ips        average  deviation         median         99th %
persistent term      302.16 K        3.31 μs  ±1067.30%        3.02 μs        4.00 μs
ets cache            103.61 K        9.65 μs   ±434.32%        9.02 μs       11.69 μs

Comparison:
persistent term      302.16 K
ets cache            103.61 K - 2.92x slower +6.34 μs

Memory usage statistics:

Name               Memory usage
persistent term           480 B
ets cache                 768 B - 1.60x memory usage +288 B

**All measurements for memory usage were the same**

17:56:44.248 [error] [tag: :mod_cache_users_ets, what: :DEBUG_MESSAGE]

17:56:44.248 [error] [tag: :mod_cache_users_pt, what: :DEBUG_MESSAGE]
Operating System: Linux
CPU Information: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Number of Available Cores: 12
Available memory: 30.99 GB
Elixir 1.11.0
Erlang 23.3.3

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 10 s
parallel: 12
inputs: some
Estimated total run time: 44 s

Benchmarking ets cache with input some...
Benchmarking persistent term with input some...

##### With input some #####
Name                      ips        average  deviation         median         99th %
persistent term      297.20 K        3.36 μs  ±1068.35%        3.10 μs        4.00 μs
ets cache            110.10 K        9.08 μs   ±429.29%        8.45 μs       11.44 μs

Comparison:
persistent term      297.20 K
ets cache            110.10 K - 2.70x slower +5.72 μs

Memory usage statistics:

Name                    average  deviation         median         99th %
persistent term           480 B     ±0.00%          480 B          480 B
ets cache              768.00 B     ±0.02%          768 B          768 B

Comparison:
persistent term           480 B
ets cache              768.00 B - 1.60x memory usage +288.00 B

17:58:13.114 [error] [tag: :mod_cache_users_ets, what: :DEBUG_MESSAGE]

17:58:13.114 [error] [tag: :mod_cache_users_pt, what: :DEBUG_MESSAGE]
Operating System: Linux
CPU Information: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Number of Available Cores: 12
Available memory: 30.99 GB
Elixir 1.11.0
Erlang 23.3.3

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 10 s
parallel: 12
inputs: some
Estimated total run time: 44 s

Benchmarking ets cache with input some...
Benchmarking persistent term with input some...

##### With input some #####
Name                      ips        average  deviation         median         99th %
persistent term      301.10 K        3.32 μs  ±1131.07%        3.02 μs        3.97 μs
ets cache            107.50 K        9.30 μs   ±451.42%        8.65 μs       11.73 μs

Comparison:
persistent term      301.10 K
ets cache            107.50 K - 2.80x slower +5.98 μs

Memory usage statistics:

Name               Memory usage
persistent term           480 B
ets cache                 768 B - 1.60x memory usage +288 B

**All measurements for memory usage were the same**

It looks like in more or less all cases, the ets implementation is 2.8x slower and uses 1.6x more memory.

@codecov
Copy link

codecov bot commented Jul 2, 2021

Codecov Report

Merging #3169 (355ce03) into master (f8218b2) will increase coverage by 2.82%.
The diff coverage is 93.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3169      +/-   ##
==========================================
+ Coverage   77.41%   80.23%   +2.82%     
==========================================
  Files         396      396              
  Lines       32306    32317      +11     
==========================================
+ Hits        25010    25931     +921     
+ Misses       7296     6386     -910     
Impacted Files Coverage Δ
src/mod_cache_users.erl 85.43% <93.75%> (+1.74%) ⬆️
...bal_distrib/mod_global_distrib_hosts_refresher.erl 71.69% <0.00%> (-1.89%) ⬇️
src/mod_bosh_socket.erl 77.63% <0.00%> (-1.25%) ⬇️
src/global_distrib/mod_global_distrib_utils.erl 64.48% <0.00%> (-0.94%) ⬇️
src/muc_light/mod_muc_light.erl 85.13% <0.00%> (-0.75%) ⬇️
src/ejabberd_sm.erl 84.26% <0.00%> (-0.33%) ⬇️
src/mod_muc_room.erl 76.92% <0.00%> (-0.18%) ⬇️
src/pubsub/mod_pubsub.erl 73.23% <0.00%> (+0.05%) ⬆️
src/ejabberd_c2s.erl 89.20% <0.00%> (+0.14%) ⬆️
src/pubsub/mod_pubsub_db_mnesia.erl 92.82% <0.00%> (+0.42%) ⬆️
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8218b2...355ce03. Read the comment docs.

Copy link
Contributor

@arcusfelis arcusfelis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@arcusfelis arcusfelis merged commit 6a1899b into master Jul 5, 2021
@arcusfelis arcusfelis deleted the cache_users/persistent_term branch July 5, 2021 15:20
@Premwoik Premwoik added this to the 5.0.0 milestone Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants