Skip to content

Commit b578f24

Browse files
committed
Refactor benchmarks
1 parent 762737e commit b578f24

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

bench.exs renamed to benchmarks/bench.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule SQL.Repo do
1515
end
1616
end
1717
Application.put_env(:sql, :ecto_repos, [SQL.Repo])
18-
Application.put_env(:sql, SQL.Repo, log: false, username: "postgres", password: "postgres", hostname: "localhost", database: "sql_test#{System.get_env("MIX_TEST_PARTITION")}", pool: Ecto.Adapters.SQL.Sandbox, pool_size: 10)
18+
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: 10)
1919
SQL.Repo.__adapter__().storage_up(SQL.Repo.config())
2020
SQL.Repo.start_link()
2121
query = "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])
@@ -32,13 +32,13 @@ Benchee.run(
3232
"lex" => fn _ -> SQL.Lexer.lex("with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)") end,
3333
"parse" => fn _ -> SQL.Parser.parse(tokens, context) end,
3434
"iodata" => fn _ -> pcontext.module.to_iodata(ptokens, pcontext) end,
35-
"format" => fn _ -> SQL.Format.to_iodata(ptokens, pcontext) end,
35+
"format" => fn _ -> SQL.Format.to_iodata(ptokens, pcontext, 0, false) end,
3636
"lex+parse+iodata" => fn _ ->
3737
{:ok, _, tokens} = SQL.Lexer.lex("with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)")
3838
{:ok, pcontext, tokens} = SQL.Parser.parse(tokens, context)
3939
pcontext.module.to_iodata(tokens, pcontext)
4040
end,
41-
"parse/1" => 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,
41+
"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,
4242
"sql" => fn _ -> SQL.Repo.sql() end,
4343
"ecto" => fn _ -> SQL.Repo.ecto() end,
4444
"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,

benchmarks/pool.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import SQL
2+
import Ecto.Query
3+
SQL.Pool.start_link(%{name: :mypool, protocol: :tcp, size: 10})
4+
defmodule SQL.Repo do
5+
use Ecto.Repo, otp_app: :sql, adapter: Ecto.Adapters.Postgres
6+
use SQL, adapter: SQL.Adapters.Postgres, repo: __MODULE__
7+
8+
def sql() do
9+
{idx, _} = SQL.Pool.checkout(SQL.parse("with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)"), :mypool)
10+
SQL.Pool.checkin(idx, :mypool)
11+
end
12+
13+
def ecto() do
14+
checkout(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)
15+
end
16+
end
17+
Application.put_env(:sql, :ecto_repos, [SQL.Repo])
18+
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: 10)
19+
SQL.Repo.__adapter__().storage_up(SQL.Repo.config())
20+
SQL.Repo.start_link()
21+
Benchee.run(
22+
%{
23+
"sql" => fn _ -> SQL.Repo.sql() end,
24+
"ecto" => fn _ -> SQL.Repo.ecto() end,
25+
},
26+
parallel: 50, time: 1,
27+
inputs: %{"1..100_000" => Enum.to_list(1..100_000)},
28+
memory_time: 2,
29+
reduction_time: 2,
30+
unit_scaling: :smallest,
31+
measure_function_call_overhead: true,
32+
profile_after: :eprof
33+
)

benchmarks/v0.4.0.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
➜ sql git:(main) ✗ mix sql.bench
2+
Operating System: macOS
3+
CPU Information: Apple M1 Max
4+
Number of Available Cores: 10
5+
Available memory: 64 GB
6+
Elixir 1.20.0-dev
7+
Erlang 28.1
8+
JIT enabled: true
9+
10+
Benchmark suite executing with the following configuration:
11+
warmup: 2 s
12+
time: 5 s
13+
memory time: 2 s
14+
reduction time: 2 s
15+
parallel: 1
16+
inputs: 1..100_000
17+
Estimated total run time: 1 min 28 s
18+
19+
Measured function call overhead as: 0 ns
20+
Benchmarking comptime ecto with input 1..100_000 ...
21+
Benchmarking format with input 1..100_000 ...
22+
Benchmarking iodata with input 1..100_000 ...
23+
Benchmarking lex with input 1..100_000 ...
24+
Benchmarking lex+parse+iodata with input 1..100_000 ...
25+
Benchmarking parse with input 1..100_000 ...
26+
Benchmarking parse/3 with input 1..100_000 ...
27+
Benchmarking runtime ecto with input 1..100_000 ...
28+
Calculating statistics...
29+
Formatting results...
30+
31+
##### With input 1..100_000 #####
32+
Name ips average deviation median 99th %
33+
iodata 1627.29 K 614.52 ns ±3180.45% 542 ns 709 ns
34+
format 1296.69 K 771.20 ns ±2729.73% 708 ns 917 ns
35+
parse 1049.15 K 953.15 ns ±1490.08% 875 ns 1083 ns
36+
lex 830.98 K 1203.40 ns ±1480.24% 1084 ns 1584 ns
37+
lex+parse+iodata 383.17 K 2609.80 ns ±478.57% 2500 ns 3125 ns
38+
parse/3 308.89 K 3237.35 ns ±316.26% 3000 ns 8125 ns
39+
comptime ecto 199.44 K 5014.02 ns ±156.64% 4791 ns 8958 ns
40+
runtime ecto 185.75 K 5383.47 ns ±169.39% 5084 ns 13579.31 ns
41+
42+
Comparison:
43+
iodata 1627.29 K
44+
format 1296.69 K - 1.25x slower +156.68 ns
45+
parse 1049.15 K - 1.55x slower +338.63 ns
46+
lex 830.98 K - 1.96x slower +588.88 ns
47+
lex+parse+iodata 383.17 K - 4.25x slower +1995.29 ns
48+
parse/3 308.89 K - 5.27x slower +2622.83 ns
49+
comptime ecto 199.44 K - 8.16x slower +4399.50 ns
50+
runtime ecto 185.75 K - 8.76x slower +4768.95 ns
51+
52+
Memory usage statistics:
53+
54+
Name Memory usage
55+
iodata 1.35 KB
56+
format 1.42 KB - 1.05x memory usage +0.0703 KB
57+
parse 2.23 KB - 1.65x memory usage +0.88 KB
58+
lex 8.63 KB - 6.39x memory usage +7.28 KB
59+
lex+parse+iodata 12.21 KB - 9.03x memory usage +10.86 KB
60+
parse/3 12.73 KB - 9.42x memory usage +11.38 KB
61+
comptime ecto 17.78 KB - 13.16x memory usage +16.43 KB
62+
runtime ecto 20.21 KB - 14.95x memory usage +18.86 KB
63+
64+
**All measurements for memory usage were the same**
65+
66+
Reduction count statistics:
67+
68+
Name Reduction count
69+
iodata 175
70+
format 188 - 1.07x reduction count +13
71+
parse 194 - 1.11x reduction count +19
72+
lex 345 - 1.97x reduction count +170
73+
lex+parse+iodata 714 - 4.08x reduction count +539
74+
parse/3 763 - 4.36x reduction count +588
75+
comptime ecto 1132 - 6.47x reduction count +957
76+
runtime ecto 1193 - 6.82x reduction count +1018
77+
78+
**All measurements for reduction count were the same**

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule SQL.MixProject do
1616
name: "SQL",
1717
docs: docs(),
1818
package: package(),
19-
aliases: ["sql.bench": "run bench.exs"]
19+
aliases: ["sql.bench": "run benchmarks/bench.exs", "sql.bench.pool": "run benchmarks/pool.exs"]
2020
]
2121
end
2222

0 commit comments

Comments
 (0)