@@ -51,6 +51,7 @@ defmodule BenchReducer do
5151 Task . async ( fn ->
5252 dispatch . ( "bench.increment" )
5353 end )
54+
5455 fn -> :ok end
5556
5657 _ ->
@@ -84,17 +85,22 @@ IO.puts(String.duplicate("-", 40))
8485iterations = [ 100 , 500 , 1000 , 5000 ]
8586
8687Enum . each ( iterations , fn count ->
87- { time , _ } = :timer . tc ( fn ->
88- Enum . each ( 1 .. count , fn _ ->
89- :ok = Phoenix.SessionProcess . dispatch ( session_id , "bench.increment" )
88+ { time , _ } =
89+ :timer . tc ( fn ->
90+ Enum . each ( 1 .. count , fn _ ->
91+ :ok = Phoenix.SessionProcess . dispatch ( session_id , "bench.increment" )
92+ end )
93+
94+ # Allow dispatches to complete
95+ Process . sleep ( 10 )
9096 end )
91- Process . sleep ( 10 ) # Allow dispatches to complete
92- end )
9397
9498 rate = Float . round ( count / ( time / 1_000_000 ) , 2 )
9599 avg_time = Float . round ( time / count / 1000 , 3 )
96100
97- IO . puts ( " #{ String . pad_leading ( to_string ( count ) , 5 ) } dispatches: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec (avg: #{ avg_time } ms/op)" )
101+ IO . puts (
102+ " #{ String . pad_leading ( to_string ( count ) , 5 ) } dispatches: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec (avg: #{ avg_time } ms/op)"
103+ )
98104end )
99105
100106# Reset state
@@ -106,23 +112,28 @@ IO.puts("\n📊 2. Async Dispatch with Cancellation")
106112IO . puts ( String . duplicate ( "-" , 40 ) )
107113
108114Enum . each ( [ 100 , 500 , 1000 ] , fn count ->
109- { time , results } = :timer . tc ( fn ->
110- Enum . map ( 1 .. count , fn _ ->
111- Phoenix.SessionProcess . dispatch_async ( session_id , "bench.increment" , nil , async: true )
115+ { time , results } =
116+ :timer . tc ( fn ->
117+ Enum . map ( 1 .. count , fn _ ->
118+ Phoenix.SessionProcess . dispatch_async ( session_id , "bench.increment" , nil , async: true )
119+ end )
112120 end )
113- end )
114121
115- success_count = Enum . count ( results , fn
116- { :ok , _cancel_fn } -> true
117- _ -> false
118- end )
122+ success_count =
123+ Enum . count ( results , fn
124+ { :ok , _cancel_fn } -> true
125+ _ -> false
126+ end )
119127
120128 rate = Float . round ( success_count / ( time / 1_000_000 ) , 2 )
121129 avg_time = Float . round ( time / count / 1000 , 3 )
122130
123- IO . puts ( " #{ String . pad_leading ( to_string ( count ) , 5 ) } async dispatches: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec (avg: #{ avg_time } ms/op)" )
131+ IO . puts (
132+ " #{ String . pad_leading ( to_string ( count ) , 5 ) } async dispatches: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec (avg: #{ avg_time } ms/op)"
133+ )
124134
125- Process . sleep ( 50 ) # Allow async tasks to complete
135+ # Allow async tasks to complete
136+ Process . sleep ( 50 )
126137end )
127138
128139# Reset state
@@ -139,22 +150,27 @@ Enum.each(concurrent_tasks, fn task_count ->
139150 dispatches_per_task = 100
140151 total_dispatches = task_count * dispatches_per_task
141152
142- { time , _ } = :timer . tc ( fn ->
143- tasks = Enum . map ( 1 .. task_count , fn _ ->
144- Task . async ( fn ->
145- Enum . each ( 1 .. dispatches_per_task , fn _ ->
146- :ok = Phoenix.SessionProcess . dispatch ( session_id , "bench.increment" )
153+ { time , _ } =
154+ :timer . tc ( fn ->
155+ tasks =
156+ Enum . map ( 1 .. task_count , fn _ ->
157+ Task . async ( fn ->
158+ Enum . each ( 1 .. dispatches_per_task , fn _ ->
159+ :ok = Phoenix.SessionProcess . dispatch ( session_id , "bench.increment" )
160+ end )
161+ end )
147162 end )
148- end )
149- end )
150163
151- Task . await_many ( tasks , 30_000 )
152- Process . sleep ( 50 ) # Allow all dispatches to complete
153- end )
164+ Task . await_many ( tasks , 30_000 )
165+ # Allow all dispatches to complete
166+ Process . sleep ( 50 )
167+ end )
154168
155169 rate = Float . round ( total_dispatches / ( time / 1_000_000 ) , 2 )
156170
157- IO . puts ( " #{ String . pad_leading ( to_string ( task_count ) , 3 ) } tasks × #{ dispatches_per_task } dispatches: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec" )
171+ IO . puts (
172+ " #{ String . pad_leading ( to_string ( task_count ) , 3 ) } tasks × #{ dispatches_per_task } dispatches: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec"
173+ )
158174end )
159175
160176# Verify final count
@@ -175,49 +191,57 @@ subscription_counts = [1, 5, 10]
175191
176192Enum . each ( subscription_counts , fn sub_count ->
177193 # Subscribe multiple times
178- sub_ids = Enum . map ( 1 .. sub_count , fn i ->
179- event_name = :"counter_changed_#{ i } "
180- { :ok , sub_id } = Phoenix.SessionProcess . subscribe (
181- session_id ,
182- fn state -> state . bench . counter end ,
183- event_name
184- )
185-
186- # Clear initial notification
187- receive do
188- { ^ event_name , _ } -> :ok
189- after
190- 100 -> :ok
191- end
192-
193- sub_id
194- end )
194+ sub_ids =
195+ Enum . map ( 1 .. sub_count , fn i ->
196+ event_name = :"counter_changed_#{ i } "
197+
198+ { :ok , sub_id } =
199+ Phoenix.SessionProcess . subscribe (
200+ session_id ,
201+ fn state -> state . bench . counter end ,
202+ event_name
203+ )
204+
205+ # Clear initial notification
206+ receive do
207+ { ^ event_name , _ } -> :ok
208+ after
209+ 100 -> :ok
210+ end
211+
212+ sub_id
213+ end )
195214
196215 # Benchmark dispatches with subscriptions
197216 dispatch_count = 100
198217
199- { time , _ } = :timer . tc ( fn ->
200- Enum . each ( 1 .. dispatch_count , fn _ ->
201- :ok = Phoenix.SessionProcess . dispatch ( session_id , "bench.increment" )
218+ { time , _ } =
219+ :timer . tc ( fn ->
220+ Enum . each ( 1 .. dispatch_count , fn _ ->
221+ :ok = Phoenix.SessionProcess . dispatch ( session_id , "bench.increment" )
222+ end )
223+
224+ # Allow notifications to be sent
225+ Process . sleep ( 50 )
202226 end )
203- Process . sleep ( 50 ) # Allow notifications to be sent
204- end )
205227
206228 rate = Float . round ( dispatch_count / ( time / 1_000_000 ) , 2 )
207229
208- IO . puts ( " #{ String . pad_leading ( to_string ( sub_count ) , 2 ) } subscriptions: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec" )
230+ IO . puts (
231+ " #{ String . pad_leading ( to_string ( sub_count ) , 2 ) } subscriptions: #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec"
232+ )
209233
210234 # Clear notification queue
211235 :timer . sleep ( 10 )
212236
213237 # Flush any remaining messages
214238 ( fn flush ->
215- receive do
216- _ -> flush . ( flush )
217- after
218- 0 -> :ok
219- end
220- end ) . ( fn f ->
239+ receive do
240+ _ -> flush . ( flush )
241+ after
242+ 0 -> :ok
243+ end
244+ end ) . ( fn f ->
221245 receive do
222246 _ -> f . ( f )
223247 after
@@ -248,27 +272,39 @@ action_types = [
248272Enum . each ( action_types , fn
249273 { label , action_type } ->
250274 count = 1000
251- { time , _ } = :timer . tc ( fn ->
252- Enum . each ( 1 .. count , fn _ ->
253- :ok = Phoenix.SessionProcess . dispatch ( session_id , action_type )
275+
276+ { time , _ } =
277+ :timer . tc ( fn ->
278+ Enum . each ( 1 .. count , fn _ ->
279+ :ok = Phoenix.SessionProcess . dispatch ( session_id , action_type )
280+ end )
281+
282+ Process . sleep ( 10 )
254283 end )
255- Process . sleep ( 10 )
256- end )
257284
258285 rate = Float . round ( count / ( time / 1_000_000 ) , 2 )
259- IO . puts ( " #{ String . pad_trailing ( label , 30 ) } : #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec" )
286+
287+ IO . puts (
288+ " #{ String . pad_trailing ( label , 30 ) } : #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec"
289+ )
260290
261291 { label , action_type , payload } ->
262292 count = 1000
263- { time , _ } = :timer . tc ( fn ->
264- Enum . each ( 1 .. count , fn _ ->
265- :ok = Phoenix.SessionProcess . dispatch ( session_id , action_type , payload )
293+
294+ { time , _ } =
295+ :timer . tc ( fn ->
296+ Enum . each ( 1 .. count , fn _ ->
297+ :ok = Phoenix.SessionProcess . dispatch ( session_id , action_type , payload )
298+ end )
299+
300+ Process . sleep ( 10 )
266301 end )
267- Process . sleep ( 10 )
268- end )
269302
270303 rate = Float . round ( count / ( time / 1_000_000 ) , 2 )
271- IO . puts ( " #{ String . pad_trailing ( label , 30 ) } : #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec" )
304+
305+ IO . puts (
306+ " #{ String . pad_trailing ( label , 30 ) } : #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec"
307+ )
272308end )
273309
274310# Benchmark 6: State Selector Performance
@@ -289,24 +325,32 @@ Enum.each(selectors, fn {label, selector} ->
289325 count = 10000
290326
291327 # Client-side selection
292- { time , _ } = :timer . tc ( fn ->
293- Enum . each ( 1 .. count , fn _ ->
294- _result = Phoenix.SessionProcess . get_state ( session_id , selector )
328+ { time , _ } =
329+ :timer . tc ( fn ->
330+ Enum . each ( 1 .. count , fn _ ->
331+ _result = Phoenix.SessionProcess . get_state ( session_id , selector )
332+ end )
295333 end )
296- end )
297334
298335 rate = Float . round ( count / ( time / 1_000_000 ) , 2 )
299- IO . puts ( " #{ String . pad_trailing ( label <> " (client)" , 35 ) } : #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec" )
336+
337+ IO . puts (
338+ " #{ String . pad_trailing ( label <> " (client)" , 35 ) } : #{ String . pad_leading ( Float . to_string ( rate ) , 10 ) } ops/sec"
339+ )
300340
301341 # Server-side selection
302- { time2 , _ } = :timer . tc ( fn ->
303- Enum . each ( 1 .. count , fn _ ->
304- _result = Phoenix.SessionProcess . select_state ( session_id , selector )
342+ { time2 , _ } =
343+ :timer . tc ( fn ->
344+ Enum . each ( 1 .. count , fn _ ->
345+ _result = Phoenix.SessionProcess . select_state ( session_id , selector )
346+ end )
305347 end )
306- end )
307348
308349 rate2 = Float . round ( count / ( time2 / 1_000_000 ) , 2 )
309- IO . puts ( " #{ String . pad_trailing ( label <> " (server)" , 35 ) } : #{ String . pad_leading ( Float . to_string ( rate2 ) , 10 ) } ops/sec" )
350+
351+ IO . puts (
352+ " #{ String . pad_trailing ( label <> " (server)" , 35 ) } : #{ String . pad_leading ( Float . to_string ( rate2 ) , 10 ) } ops/sec"
353+ )
310354end )
311355
312356# Summary
0 commit comments