Skip to content

Commit 4eb1050

Browse files
committed
refactor pool
1 parent 5d2b5d1 commit 4eb1050

File tree

4 files changed

+241
-161
lines changed

4 files changed

+241
-161
lines changed

benchmarks/bench.exs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,61 @@ defmodule SQL.Repo do
77
use Ecto.Repo, otp_app: :sql, adapter: Ecto.Adapters.Postgres
88
use SQL, adapter: SQL.Adapters.Postgres
99
import Ecto.Query
10-
def sql() do
10+
def sql(type \\ :transaction)
11+
def sql(:statement) do
12+
Enum.to_list(~SQL"SELECT 1")
13+
end
14+
def sql(:empty) do
15+
SQL.transaction do
16+
:ok
17+
end
18+
end
19+
def sql(:transaction) do
20+
SQL.transaction do
21+
Enum.to_list(~SQL"SELECT 1")
22+
end
23+
end
24+
def sql(:savepoint) do
1125
SQL.transaction do
1226
SQL.transaction do
1327
Enum.to_list(~SQL"SELECT 1")
1428
end
1529
end
1630
end
31+
def sql(:cursor) do
32+
SQL.transaction do
33+
Stream.run(~SQL"SELECT g, repeat(md5(g::text), 4) FROM generate_series(1, 5000000) AS g")
34+
end
35+
end
1736

18-
def ecto() do
37+
def ecto(type \\ :transaction)
38+
def ecto(:statement) do
39+
SQL.Repo.all(select(from("users"), [1]))
40+
end
41+
def ecto(:empty) do
42+
SQL.Repo.transaction(fn ->
43+
:ok
44+
end)
45+
end
46+
def ecto(:transaction) do
47+
SQL.Repo.transaction(fn ->
48+
SQL.Repo.all(select(from("users"), [1]))
49+
end)
50+
end
51+
def ecto(:savepoint) do
1952
SQL.Repo.transaction(fn ->
2053
SQL.Repo.transaction(fn ->
2154
SQL.Repo.all(select(from("users"), [1]))
2255
end)
2356
end)
2457
end
58+
def ecto(:cursor) do
59+
SQL.Repo.transaction(fn ->
60+
from(row in fragment("SELECT g, repeat(md5(g::text), 4) FROM generate_series(1, ?) AS g", 5000000), select: {fragment("?::int", row.g), fragment("?::text", row.repeat)})
61+
|> SQL.Repo.stream()
62+
|> Stream.run()
63+
end)
64+
end
2565
end
2666
Application.put_env(:sql, :ecto_repos, [SQL.Repo])
2767
Application.put_env(:sql, SQL.Repo, log: false, username: "postgres", password: "postgres", hostname: "localhost", database: "sql_test#{System.get_env("MIX_TEST_PARTITION")}", pool_size: :erlang.system_info(:schedulers_online), ssl: false)
@@ -49,20 +89,31 @@ Benchee.run(
4989
# pcontext.module.to_iodata(tokens, pcontext)
5090
# end,
5191
# "parse/3" => fn _ -> SQL.parse("with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)") end,
52-
"sql" => fn -> SQL.Repo.sql() end,
53-
"ecto" => fn -> SQL.Repo.ecto() end,
92+
# "sql" => fn -> SQL.Repo.sql(Enum.random([:statement, :transaction])) end,
93+
# "ecto" => fn -> SQL.Repo.ecto(Enum.random([:statement, :transaction])) end,
94+
"sql" => fn -> SQL.Repo.sql(:transaction) end,
95+
"ecto" => fn -> SQL.Repo.ecto(:transaction) end,
96+
# "sql" => fn -> SQL.Repo.sql(:transaction) end,
97+
# "ecto" => fn -> SQL.Repo.ecto(:transaction) end,
98+
# "sql" => fn -> SQL.Repo.sql(:savepoint) end,
99+
# "ecto" => fn -> SQL.Repo.ecto(:savepoint) end,
100+
# "sql" => fn -> SQL.Repo.sql(:cursor) end,
101+
# "ecto" => fn -> SQL.Repo.ecto(:cursor) end,
102+
# "sql" => fn -> SQL.Repo.sql(:empty) end,
103+
# "ecto" => fn -> SQL.Repo.ecto(:empty) end,
54104
# "runtime to_string" => fn _ -> to_string(~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]) end,
55105
# "runtime to_sql" => fn _ -> SQL.to_sql(~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]) end,
56106
# "runtime inspect" => fn _ -> inspect(~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]) end,
57107
# "runtime ecto" => fn _ -> SQL.Repo.to_sql(:all, "temp" |> recursive_ctes(true) |> with_cte("temp", as: ^union_all(select("temp", [t], %{n: 0, fact: 1}), ^where(select("temp", [t], [t.n+1, t.n+1*t.fact]), [t], t.n < 9))) |> select([t], [t.n])) end,
58108
# "comptime ecto" => fn _ -> SQL.Repo.to_sql(:all, query) end
59109
},
60-
parallel: 1,
110+
parallel: 500,
111+
warmup: 10,
61112
memory_time: 2,
62113
reduction_time: 2,
63114
unit_scaling: :smallest,
64-
measure_function_call_overhead: true,
115+
measure_function_call_overhead: true
65116
# profile_after: :eprof
66117
# profile_after: :cprof
67-
profile_after: :fprof
118+
# profile_after: :fprof
68119
)

0 commit comments

Comments
 (0)