Skip to content

Instantly share code, notes, and snippets.

@joladev

joladev/bench.rb Secret

Last active April 19, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joladev/34f85961849f78e5de39ad615d93cad4 to your computer and use it in GitHub Desktop.
Save joladev/34f85961849f78e5de39ad615d93cad4 to your computer and use it in GitHub Desktop.
Benchmark comparing a pure `to_list` implementation with one based on `to_list_match`
{:ok, _} = TestRegistry.start_link(keys: :unique, name: Registry.UniqueTest)
{:ok, _} = TestRegistry.start_link(keys: :duplicate, name: Registry.DuplicateTest)
{:ok, _} = TestRegistry.start_link(keys: :unique, partitions: 8, name: Registry.PartitionsUniqueTest)
{:ok, _} = TestRegistry.start_link(keys: :duplicate, partitions: 8, name: Registry.PartitionsDuplicateTest)
items = 1_000_000
for i <- 1..items, do: TestRegistry.register(Registry.UniqueTest, "unique-#{i}", :value)
for i <- 1..items, do: TestRegistry.register(Registry.DuplicateTest, "duplicate-#{rem(i, 1000)}", :value)
for i <- 1..items, do: TestRegistry.register(Registry.PartitionsUniqueTest, "partitions_unique-#{i}", :value)
for i <- 1..items, do: TestRegistry.register(Registry.PartitionsDuplicateTest, "partitions_duplicate-#{rem(i, 1000)}", :value)
inputs = %{
"unique" => Registry.UniqueTest,
"duplicate" => Registry.DuplicateTest,
"partitions_unique" => Registry.PartitionsUniqueTest,
"partitions_duplicate" => Registry.PartitionsDuplicateTest
}
Benchee.run(
%{
"to_list using to_list_match underneath" => fn registry -> TestRegistry.to_list(registry) end,
"to_list using :ets.tab2list" => fn registry -> TestRegistry.to_list_separate(registry) end,
"to_list letting ets shape the output" => fn registry -> TestRegistry.to_list_direct(registry) end,
},
inputs: inputs
)
Operating System: macOS
CPU Information: Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.8.0
Erlang 21.2.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: duplicate, partitions_duplicate, partitions_unique, unique
Estimated total run time: 1.40 min
Benchmarking to_list letting ets shape the output with input duplicate...
Benchmarking to_list letting ets shape the output with input partitions_duplicate...
Benchmarking to_list letting ets shape the output with input partitions_unique...
Benchmarking to_list letting ets shape the output with input unique...
Benchmarking to_list using :ets.tab2list with input duplicate...
Benchmarking to_list using :ets.tab2list with input partitions_duplicate...
Benchmarking to_list using :ets.tab2list with input partitions_unique...
Benchmarking to_list using :ets.tab2list with input unique...
Benchmarking to_list using to_list_match underneath with input duplicate...
Benchmarking to_list using to_list_match underneath with input partitions_duplicate...
Benchmarking to_list using to_list_match underneath with input partitions_unique...
Benchmarking to_list using to_list_match underneath with input unique...
##### With input duplicate #####
Name ips average deviation median 99th %
to_list letting ets shape the output 3.58 278.97 ms ±20.29% 275.10 ms 446.46 ms
to_list using to_list_match underneath 3.01 332.70 ms ±23.16% 342.19 ms 564.22 ms
to_list using :ets.tab2list 2.08 481.92 ms ±21.68% 439.15 ms 639.09 ms
Comparison:
to_list letting ets shape the output 3.58
to_list using to_list_match underneath 3.01 - 1.19x slower +53.73 ms
to_list using :ets.tab2list 2.08 - 1.73x slower +202.96 ms
##### With input partitions_duplicate #####
Name ips average deviation median 99th %
to_list letting ets shape the output 2.78 359.14 ms ±13.37% 339.48 ms 468.56 ms
to_list using to_list_match underneath 2.47 404.65 ms ±19.17% 391.87 ms 604.03 ms
to_list using :ets.tab2list 2.15 465.88 ms ±17.43% 464.47 ms 612.02 ms
Comparison:
to_list letting ets shape the output 2.78
to_list using to_list_match underneath 2.47 - 1.13x slower +45.51 ms
to_list using :ets.tab2list 2.15 - 1.30x slower +106.74 ms
##### With input partitions_unique #####
Name ips average deviation median 99th %
to_list letting ets shape the output 2.91 343.70 ms ±18.52% 343.75 ms 531.81 ms
to_list using to_list_match underneath 2.50 400.20 ms ±22.09% 378.96 ms 633.75 ms
to_list using :ets.tab2list 2.16 463.65 ms ±10.66% 464.74 ms 537.96 ms
Comparison:
to_list letting ets shape the output 2.91
to_list using to_list_match underneath 2.50 - 1.16x slower +56.49 ms
to_list using :ets.tab2list 2.16 - 1.35x slower +119.95 ms
##### With input unique #####
Name ips average deviation median 99th %
to_list letting ets shape the output 2.99 334.65 ms ±17.45% 318.29 ms 508.22 ms
to_list using to_list_match underneath 2.56 390.78 ms ±20.07% 371.75 ms 633.18 ms
to_list using :ets.tab2list 2.10 475.64 ms ±23.36% 460.30 ms 776.41 ms
Comparison:
to_list letting ets shape the output 2.99
to_list using to_list_match underneath 2.56 - 1.17x slower +56.12 ms
to_list using :ets.tab2list 2.10 - 1.42x slower +140.98 ms
def to_list_separate(registry) do
case key_info!(registry) do
{_kind, partitions, nil} ->
Enum.flat_map(0..(partitions - 1), fn partition_index ->
:ets.tab2list(key_ets!(registry, partition_index))
|> Enum.map(&to_entry/1)
end)
{_kind, 1, key_ets} ->
:ets.tab2list(key_ets)
|> Enum.map(&to_entry/1)
end
end
def to_list_direct(registry) do
spec = [{{:"$1", {:"$2", :"$3"}}, [], [{{:"$1", :"$2", :"$3"}}]}]
case key_info!(registry) do
{_kind, partitions, nil} ->
Enum.flat_map(0..(partitions - 1), fn partition_index ->
:ets.select(key_ets!(registry, partition_index), spec)
end)
{_kind, 1, key_ets} ->
:ets.select(key_ets, spec)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment