Skip to content

Commit e915941

Browse files
authored
test: add benchmark that runs a given block range (#675)
1 parent 072e7c3 commit e915941

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

bench/block_processing.exs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ alias LambdaEthereumConsensus.Store.BlockStore
66
alias LambdaEthereumConsensus.Store.StateStore
77
alias Types.{BeaconState, SignedBeaconBlock}
88

9-
{:ok, _} = Store.Db.start_link(nil)
10-
{:ok, _} = Store.Blocks.start_link(nil)
9+
Logger.configure(level: :warning)
10+
11+
{:ok, _} = Store.Db.start_link([])
12+
{:ok, _} = Store.Blocks.start_link([])
13+
{:ok, _} = Store.BlockStates.start_link([])
1114
Cache.initialize_cache()
1215

1316
# NOTE: this slot must be at the beginning of an epoch (i.e. a multiple of 32)
14-
slot = 8_210_240
17+
slot = 4_213_280
1518

1619
IO.puts("fetching blocks...")
1720
{:ok, %BeaconState{} = state} = StateStore.get_state_by_slot(slot)

bench/multiple_blocks_processing.exs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
alias LambdaEthereumConsensus.ForkChoice
2+
alias LambdaEthereumConsensus.ForkChoice.Handlers
3+
alias LambdaEthereumConsensus.StateTransition.Cache
4+
alias LambdaEthereumConsensus.Store
5+
alias LambdaEthereumConsensus.Store.BlockStore
6+
alias LambdaEthereumConsensus.Store.StateStore
7+
alias Types.{BeaconState, SignedBeaconBlock}
8+
9+
Logger.configure(level: :warning)
10+
11+
{:ok, _} = Store.Db.start_link([])
12+
{:ok, _} = Store.Blocks.start_link([])
13+
{:ok, _} = Store.BlockStates.start_link([])
14+
Cache.initialize_cache()
15+
16+
# NOTE: this slot must be at the beginning of an epoch (i.e. a multiple of 32)
17+
start_slot = 4_213_280
18+
count = 10
19+
end_slot = start_slot + count
20+
21+
IO.puts("fetching blocks...")
22+
{:ok, %BeaconState{} = state} = StateStore.get_state_by_slot(start_slot)
23+
{:ok, %SignedBeaconBlock{} = signed_block} = BlockStore.get_block_by_slot(state.slot)
24+
25+
blocks =
26+
(start_slot + 1)..end_slot
27+
# NOTE: we have to consider empty slots
28+
|> Enum.flat_map(fn slot ->
29+
case BlockStore.get_block_by_slot(slot) do
30+
{:ok, block} -> [block]
31+
:not_found -> []
32+
end
33+
end)
34+
35+
IO.puts("initializing store...")
36+
{:ok, store} = Types.Store.get_forkchoice_store(state, signed_block)
37+
store = Handlers.on_tick(store, :os.system_time(:second))
38+
39+
IO.puts("Running slots from #{start_slot} to #{end_slot}")
40+
41+
start_time = System.monotonic_time(:millisecond)
42+
43+
Enum.reduce(blocks, store, fn block, store ->
44+
start_time = System.monotonic_time(:millisecond)
45+
{:ok, new_store} = Handlers.on_block(store, block)
46+
47+
# process block attestations
48+
{:ok, new_store} =
49+
signed_block.message.body.attestations
50+
|> ForkChoice.apply_handler(new_store, &Handlers.on_attestation(&1, &2, true))
51+
52+
# process block attester slashings
53+
{:ok, new_store} =
54+
signed_block.message.body.attester_slashings
55+
|> ForkChoice.apply_handler(new_store, &Handlers.on_attester_slashing/2)
56+
57+
end_time = System.monotonic_time(:millisecond)
58+
IO.puts("Slot #{block.message.slot} took #{end_time - start_time} ms")
59+
new_store
60+
end)
61+
62+
end_time = System.monotonic_time(:millisecond)
63+
IO.puts("Total: took #{end_time - start_time} ms")

lib/lambda_ethereum_consensus/store/lru_cache.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule LambdaEthereumConsensus.Store.LRUCache do
3434
@spec put(atom(), key(), value()) :: :ok
3535
def put(table, key, value) do
3636
cache_value(table, key, value)
37-
GenServer.cast(__MODULE__, {:put, key, value})
37+
GenServer.cast(table, {:put, key, value})
3838
end
3939

4040
@spec get(atom(), key(), (key() -> value() | nil)) :: value() | nil
@@ -93,7 +93,7 @@ defmodule LambdaEthereumConsensus.Store.LRUCache do
9393
cache_miss(table, key, fetch_func)
9494

9595
v ->
96-
GenServer.cast(__MODULE__, {:touch_entry, key})
96+
:ok = GenServer.cast(table, {:touch_entry, key})
9797
v
9898
end
9999
end
@@ -107,7 +107,7 @@ defmodule LambdaEthereumConsensus.Store.LRUCache do
107107

108108
defp cache_value(table, key, value) do
109109
:ets.insert_new(table, {key, value, nil})
110-
GenServer.cast(__MODULE__, {:touch_entry, key})
110+
GenServer.cast(table, {:touch_entry, key})
111111
value
112112
end
113113

test/integration/fork_choice/handlers_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ defmodule Integration.ForkChoice.HandlersTest do
22
use ExUnit.Case
33

44
alias LambdaEthereumConsensus.ForkChoice.Handlers
5+
alias LambdaEthereumConsensus.StateTransition.Cache
6+
alias LambdaEthereumConsensus.Store.Blocks
57
alias LambdaEthereumConsensus.Store.BlockStore
68
alias LambdaEthereumConsensus.Store.Db
79
alias LambdaEthereumConsensus.Store.StateStore
810

911
setup_all do
1012
start_supervised!(Db)
13+
start_supervised!(Blocks)
14+
start_supervised!(BlockStates)
15+
Cache.initialize_cache()
1116
:ok
1217
end
1318

19+
# TODO: refactor to use randomized fixtures
1420
@tag :skip
1521
test "on_block w/data from DB" do
1622
# NOTE: this test requires a DB with a state, and blocks for the state's slot and the next slot.
@@ -20,7 +26,7 @@ defmodule Integration.ForkChoice.HandlersTest do
2026
{:ok, signed_block} = BlockStore.get_block_by_slot(state.slot)
2127
{:ok, new_signed_block} = BlockStore.get_block_by_slot(state.slot + 1)
2228

23-
assert {:ok, store} = Types.Store.get_forkchoice_store(state, signed_block.message)
29+
assert {:ok, store} = Types.Store.get_forkchoice_store(state, signed_block)
2430
new_store = Handlers.on_tick(store, :os.system_time(:second))
2531

2632
assert {:ok, _} = Handlers.on_block(new_store, new_signed_block)

0 commit comments

Comments
 (0)