Skip to content

Commit 27a4ab6

Browse files
committed
chore: Format code and fix bench.
1 parent 7491a81 commit 27a4ab6

File tree

6 files changed

+165
-98
lines changed

6 files changed

+165
-98
lines changed

.formatter.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Used by "mix format"
22
[
3-
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test,bench,examples}/**/*.{ex,exs}"]
44
]

bench/session_benchmark.exs

Lines changed: 112 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
alias Phoenix.SessionProcess
1313
alias Phoenix.SessionProcess.Helpers
1414

15+
Code.ensure_loaded?(Logger) or require Logger
16+
Logger.configure(level: :info)
17+
1518
IO.puts("🚀 Phoenix Session Process Benchmarking")
1619
IO.puts(String.duplicate("=", 50))
20+
{:ok, _} = Phoenix.SessionProcess.Supervisor.start_link([])
1721

1822
# Warm up the system
1923
IO.puts("Warming up...")
24+
2025
for _i <- 1..100 do
2126
SessionProcess.start("warmup_#{System.unique_integer()}")
2227
end
@@ -34,15 +39,16 @@ concurrent_ops = [10, 50, 100]
3439

3540
# 1. Session Creation Performance
3641
IO.puts("\n📊 Session Creation Benchmark")
37-
IO.puts("-" * 30)
42+
IO.puts(String.duplicate("-", 30))
3843

3944
Enum.each(num_sessions, fn count ->
40-
{time, _} = :timer.tc(fn ->
41-
1..count
42-
|> Enum.map(&"session_#{&1}")
43-
|> Helpers.start_sessions()
44-
end)
45-
45+
{time, _} =
46+
:timer.tc(fn ->
47+
1..count
48+
|> Enum.map(&"session_#{&1}")
49+
|> Helpers.start_sessions()
50+
end)
51+
4652
rate = Float.round(count / (time / 1_000_000), 2)
4753
IO.puts("Created #{count} sessions in #{time / 1_000}ms (#{rate} sessions/sec)")
4854
end)
@@ -54,7 +60,7 @@ end)
5460

5561
# 2. Session Communication Performance
5662
IO.puts("\n📊 Session Communication Benchmark")
57-
IO.puts("-" * 35)
63+
IO.puts(String.duplicate("-", 35))
5864

5965
# Create test sessions
6066
Enum.each(1..100, fn i ->
@@ -63,33 +69,35 @@ end)
6369

6470
# Benchmark calls vs casts
6571
Enum.each(concurrent_ops, fn ops ->
66-
{call_time, _} = :timer.tc(fn ->
67-
1..ops
68-
|> Enum.map(fn i ->
69-
Task.async(fn ->
70-
SessionProcess.call("comm_test_#{i}", :ping)
72+
{call_time, _} =
73+
:timer.tc(fn ->
74+
1..ops
75+
|> Enum.map(fn i ->
76+
Task.async(fn ->
77+
SessionProcess.call("comm_test_#{i}", :ping)
78+
end)
7179
end)
80+
|> Task.await_many(5000)
7281
end)
73-
|> Task.await_many(5000)
74-
end)
7582

76-
{cast_time, _} = :timer.tc(fn ->
77-
1..ops
78-
|> Enum.map(fn i ->
79-
Task.async(fn ->
80-
SessionProcess.cast("comm_test_#{i}", :ping)
83+
{cast_time, _} =
84+
:timer.tc(fn ->
85+
1..ops
86+
|> Enum.map(fn i ->
87+
Task.async(fn ->
88+
SessionProcess.cast("comm_test_#{i}", :ping)
89+
end)
8190
end)
91+
|> Task.await_many(5000)
8292
end)
83-
|> Task.await_many(5000)
84-
end)
8593

8694
IO.puts("#{ops} concurrent calls: #{call_time / 1000}ms")
8795
IO.puts("#{ops} concurrent casts: #{cast_time / 1000}ms")
8896
end)
8997

9098
# 3. Memory Usage Analysis
9199
IO.puts("\n📊 Memory Usage Analysis")
92-
IO.puts("-" * 25)
100+
IO.puts(String.duplicate("-", 25))
93101

94102
# Create varying session counts
95103
memory_tests = [100, 500, 1000]
@@ -110,12 +118,14 @@ Enum.each(memory_tests, fn count ->
110118
memory_mb = stats.memory_usage / 1024 / 1024
111119
avg_memory_kb = stats.avg_memory_per_session / 1024
112120

113-
IO.puts("#{count} sessions: #{Float.round(memory_mb, 2)}MB total, #{Float.round(avg_memory_kb, 2)}KB per session")
121+
IO.puts(
122+
"#{count} sessions: #{Float.round(memory_mb, 2)}MB total, #{Float.round(avg_memory_kb, 2)}KB per session"
123+
)
114124
end)
115125

116126
# 4. Registry Lookup Performance
117127
IO.puts("\n📊 Registry Lookup Performance")
118-
IO.puts("-" * 32)
128+
IO.puts(String.duplicate("-", 32))
119129

120130
# Create sessions for lookup tests
121131
Enum.each(1..1000, fn i ->
@@ -125,19 +135,20 @@ end)
125135
lookup_tests = [100, 1000, 5000]
126136

127137
Enum.each(lookup_tests, fn iterations ->
128-
{time, _} = :timer.tc(fn ->
129-
Enum.each(1..iterations, fn i ->
130-
SessionProcess.started?("lookup_test_#{rem(i, 1000) + 1}")
138+
{time, _} =
139+
:timer.tc(fn ->
140+
Enum.each(1..iterations, fn i ->
141+
SessionProcess.started?("lookup_test_#{rem(i, 1000) + 1}")
142+
end)
131143
end)
132-
end)
133144

134145
rate = Float.round(iterations / (time / 1_000_000), 0)
135146
IO.puts("#{iterations} registry lookups: #{time / 1000}ms (#{rate} lookups/sec)")
136147
end)
137148

138149
# 5. Error Handling Performance
139150
IO.puts("\n📊 Error Handling Performance")
140-
IO.puts("-" * 30)
151+
IO.puts(String.duplicate("-", 30))
141152

142153
error_tests = [
143154
{:invalid_session_id, "invalid@session"},
@@ -146,80 +157,107 @@ error_tests = [
146157
]
147158

148159
Enum.each(error_tests, fn {scenario, test_id} ->
149-
{time, result} = :timer.tc(fn ->
150-
case scenario do
151-
:invalid_session_id ->
152-
SessionProcess.start(test_id)
153-
:session_not_found ->
154-
SessionProcess.call(test_id, :ping)
155-
:timeout_scenario ->
156-
SessionProcess.start(test_id)
157-
SessionProcess.call(test_id, :slow_operation, 1)
158-
end
159-
end)
160+
{time, result} =
161+
:timer.tc(fn ->
162+
case scenario do
163+
:invalid_session_id ->
164+
SessionProcess.start(test_id)
165+
166+
:session_not_found ->
167+
SessionProcess.call(test_id, :ping)
168+
169+
:timeout_scenario ->
170+
SessionProcess.start(test_id)
171+
SessionProcess.call(test_id, :slow_operation, 1)
172+
end
173+
end)
160174

161175
IO.puts("#{scenario}: #{time / 1000}ms - #{inspect(result)}")
162176
end)
163177

164178
# 6. Cleanup Performance
165179
IO.puts("\n📊 Cleanup Performance")
166-
IO.puts("-" * 23)
180+
IO.puts(String.duplicate("-", 23))
181+
182+
1..10_000
183+
|> Enum.each(fn i ->
184+
SessionProcess.start("cleanup_test_#{i}")
185+
end)
167186

168187
session_count = length(SessionProcess.list_session())
169-
{cleanup_time, _} = :timer.tc(fn ->
170-
Enum.each(SessionProcess.list_session(), fn {session_id, _pid} ->
171-
SessionProcess.terminate(session_id)
188+
189+
{cleanup_time, _} =
190+
:timer.tc(fn ->
191+
Enum.each(SessionProcess.list_session(), fn {session_id, _pid} ->
192+
SessionProcess.terminate(session_id)
193+
end)
172194
end)
173-
end)
174195

175-
cleanup_rate = Float.round(session_count / (cleanup_time / 1_000_000), 2)
176-
IO.puts("Cleaned up #{session_count} sessions in #{cleanup_time / 1000}ms (#{cleanup_rate} sessions/sec)")
196+
cleanup_rate =
197+
if cleanup_time == 0 do
198+
:infinity
199+
else
200+
Float.round(session_count / (cleanup_time / 1_000_000), 2)
201+
end
202+
203+
IO.puts(
204+
"Cleaned up #{session_count} sessions in #{cleanup_time / 1000}ms (#{cleanup_rate} sessions/sec)"
205+
)
177206

178207
# 7. Helper Functions Performance
179208
IO.puts("\n📊 Helper Functions Performance")
180-
IO.puts("-" * 33)
209+
IO.puts(String.duplicate("-", 33))
181210

182211
# Test batch operations
183212
Enum.each([100, 200], fn count ->
184213
session_ids = Enum.map(1..count, &"batch_test_#{&1}")
185-
186-
{start_time, _} = :timer.tc(fn ->
187-
Helpers.start_sessions(session_ids)
188-
end)
189-
190-
{terminate_time, _} = :timer.tc(fn ->
191-
Helpers.terminate_sessions(session_ids)
192-
end)
214+
215+
{start_time, _} =
216+
:timer.tc(fn ->
217+
Helpers.start_sessions(session_ids)
218+
end)
219+
220+
{terminate_time, _} =
221+
:timer.tc(fn ->
222+
Helpers.terminate_sessions(session_ids)
223+
end)
193224

194225
IO.puts("#{count} sessions batch start: #{start_time / 1000}ms")
195226
IO.puts("#{count} sessions batch terminate: #{terminate_time / 1000}ms")
196227
end)
197228

198229
# 8. Concurrent Stress Test
199230
IO.puts("\n📊 Concurrent Stress Test")
200-
IO.puts("-" * 27)
231+
IO.puts(String.duplicate("-", 27))
201232

202233
stress_test = fn concurrent_ops ->
203-
{time, results} = :timer.tc(fn ->
204-
1..concurrent_ops
205-
|> Enum.map(fn i ->
206-
Task.async(fn ->
207-
session_id = "stress_#{i}_#{System.unique_integer()}"
208-
case SessionProcess.start(session_id) do
209-
{:ok, _pid} ->
210-
SessionProcess.call(session_id, :ping)
211-
SessionProcess.terminate(session_id)
212-
:ok
213-
error -> error
214-
end
234+
{time, results} =
235+
:timer.tc(fn ->
236+
1..concurrent_ops
237+
|> Enum.map(fn i ->
238+
Task.async(fn ->
239+
session_id = "stress_#{i}_#{System.unique_integer()}"
240+
241+
case SessionProcess.start(session_id) do
242+
{:ok, _pid} ->
243+
SessionProcess.call(session_id, :ping)
244+
SessionProcess.terminate(session_id)
245+
:ok
246+
247+
error ->
248+
error
249+
end
250+
end)
215251
end)
252+
|> Task.await_many(10000)
216253
end)
217-
|> Task.await_many(10000)
218-
end)
219254

220255
success_count = Enum.count(results, &(&1 == :ok))
221256
rate = Float.round(concurrent_ops / (time / 1_000_000), 2)
222-
IO.puts("#{concurrent_ops} concurrent ops: #{time / 1000}ms (#{rate} ops/sec) - #{success_count}/#{concurrent_ops} successful")
257+
258+
IO.puts(
259+
"#{concurrent_ops} concurrent ops: #{time / 1000}ms (#{rate} ops/sec) - #{success_count}/#{concurrent_ops} successful"
260+
)
223261
end
224262

225263
stress_test.(100)
@@ -231,4 +269,4 @@ IO.puts(String.duplicate("=", 50))
231269
# Final system stats
232270
final_stats = SessionProcess.session_stats()
233271
IO.puts("Final system state:")
234-
IO.inspect(final_stats, label: "Session Stats")
272+
IO.inspect(final_stats, label: "Session Stats")

bench/simple_bench.exs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,52 @@ alias Phoenix.SessionProcess
88
alias Phoenix.SessionProcess.Helpers
99

1010
# Configure and start required processes
11-
Application.put_env(:phoenix_session_process, :session_process, Phoenix.SessionProcess.DefaultSessionProcess)
11+
Application.put_env(
12+
:phoenix_session_process,
13+
:session_process,
14+
Phoenix.SessionProcess.DefaultSessionProcess
15+
)
16+
1217
{:ok, _} = Phoenix.SessionProcess.Supervisor.start_link([])
1318

1419
IO.puts("🔍 Simple Phoenix Session Process Benchmark")
1520
IO.puts(String.duplicate("=", 45))
1621

1722
# Quick session creation test
1823
IO.puts("\n1. Session Creation (100 sessions)")
19-
{time, results} = :timer.tc(fn ->
20-
Enum.map(1..100, fn i ->
21-
SessionProcess.start("quick_test_#{i}")
24+
25+
{time, results} =
26+
:timer.tc(fn ->
27+
Enum.map(1..100, fn i ->
28+
SessionProcess.start("quick_test_#{i}")
29+
end)
2230
end)
23-
end)
2431

2532
success_count = Enum.count(results, &match?({:ok, _}, &1))
26-
IO.puts("Created #{success_count}/100 sessions in #{time / 1000}ms (#{Float.round(success_count / (time / 1_000_000), 2)} sessions/sec)")
33+
34+
IO.puts(
35+
"Created #{success_count}/100 sessions in #{time / 1000}ms (#{Float.round(success_count / (time / 1_000_000), 2)} sessions/sec)"
36+
)
2737

2838
# Quick communication test
2939
IO.puts("\n2. Session Communication (50 calls)")
30-
{call_time, call_results} = :timer.tc(fn ->
31-
1..50
32-
|> Enum.map(fn i ->
33-
Task.async(fn ->
34-
SessionProcess.call("quick_test_#{i}", :ping)
40+
41+
{call_time, call_results} =
42+
:timer.tc(fn ->
43+
1..50
44+
|> Enum.map(fn i ->
45+
Task.async(fn ->
46+
SessionProcess.call("quick_test_#{i}", :ping)
47+
end)
3548
end)
49+
|> Task.await_many(5000)
3650
end)
37-
|> Task.await_many(5000)
38-
end)
3951

4052
call_success = Enum.count(call_results, &match?({:ok, _}, &1))
41-
IO.puts("#{call_success}/50 calls completed in #{call_time / 1000}ms (#{Float.round(call_success / (call_time / 1_000_000), 2)} calls/sec)")
53+
54+
IO.puts(
55+
"#{call_success}/50 calls completed in #{call_time / 1000}ms (#{Float.round(call_success / (call_time / 1_000_000), 2)} calls/sec)"
56+
)
4257

4358
# Memory usage
4459
stats = SessionProcess.session_stats()
@@ -49,12 +64,14 @@ IO.puts("Avg per session: #{Float.round(stats.avg_memory_per_session / 1024, 2)}
4964

5065
# Cleanup
5166
IO.puts("\n4. Cleanup Test")
52-
{cleanup_time, _} = :timer.tc(fn ->
53-
Enum.each(1..100, fn i ->
54-
SessionProcess.terminate("quick_test_#{i}")
67+
68+
{cleanup_time, _} =
69+
:timer.tc(fn ->
70+
Enum.each(1..100, fn i ->
71+
SessionProcess.terminate("quick_test_#{i}")
72+
end)
5573
end)
56-
end)
5774

5875
IO.puts("Cleaned up 100 sessions in #{cleanup_time / 1000}ms")
5976

60-
IO.puts("\n✅ Quick benchmark complete!")
77+
IO.puts("\n✅ Quick benchmark complete!")

0 commit comments

Comments
 (0)