11# SPDX-License-Identifier: Apache-2.0
22# SPDX-FileCopyrightText: 2025 DBVisor
33
4- import SQL
4+ # import SQL
55import Ecto.Query
66defmodule SQL.Repo do
77 use Ecto.Repo , otp_app: :sql , adapter: Ecto.Adapters.Postgres
8- use SQL , adapter: SQL.Adapters.Postgres , repo: __MODULE__
9-
8+ use SQL , adapter: SQL.Adapters.Postgres
9+ import Ecto.Query
1010 def sql ( ) do
11- case SQL.Pool . checkout ( % { id: 1 } , :default ) do
12- { idx , _socket } ->
13- Process . sleep ( 5 )
14- SQL.Pool . checkin ( idx , :default )
15- :none ->
16- Process . sleep ( 5 )
17- end
11+ # SQL.transaction do
12+ # SQL.transaction do
13+ Enum . to_list ( ~SQL" SELECT 1" )
14+ # end
15+ # end
1816 end
1917
2018 def ecto ( ) do
21- try do
22- checkout ( fn ->
23- Process . sleep ( 5 )
24- end )
25- rescue
26- _ -> :error
27- Process . sleep ( 5 )
28- end
19+ # SQL.Repo.transaction(fn ->
20+ # SQL.Repo.transaction(fn ->
21+ SQL.Repo . all ( select ( from ( "users" ) , [ 1 ] ) )
22+ # end)
23+ # end)
2924 end
3025end
3126Application . put_env ( :sql , :ecto_repos , [ SQL.Repo ] )
32- 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 )
27+ 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 , ssl: false )
3328SQL.Repo . __adapter__ ( ) . storage_up ( SQL.Repo . config ( ) )
3429SQL.Repo . start_link ( )
35- SQL.Pool . start_link ( % { name: :default , size: 10 , protocol: :tcp } )
36- 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 ] )
37- sql = ~SQL[ with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]
38- result = Tuple . to_list ( 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)" ) )
39- tokens = Enum . at ( result , - 1 )
40- context = Map . merge ( Enum . at ( result , 1 ) , % { sql_lock: nil , module: SQL.Adapters.ANSI } )
41- { :ok , pcontext , ptokens } = SQL.Parser . parse ( tokens , context )
30+ SQL.Pool . start_link ( % { username: "postgres" , password: "postgres" , hostname: "localhost" , database: "sql_test #{ System . get_env ( "MIX_TEST_PARTITION" ) } " , adapter: SQL.Adapters.Postgres , ssl: false } )
31+ # 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+ # sql = ~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]
33+ # result = Tuple.to_list(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)"))
34+ # tokens = Enum.at(result, -1)
35+ # context = Map.merge(Enum.at(result, 1), %{sql_lock: nil, module: SQL.Adapters.ANSI})
36+ # {:ok, pcontext, ptokens} = SQL.Parser.parse(tokens, context)
4237
4338Benchee . run (
4439 % {
@@ -55,19 +50,20 @@ Benchee.run(
5550 # pcontext.module.to_iodata(tokens, pcontext)
5651 # end,
5752 # "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,
58- "sql" => fn _ -> SQL.Repo . sql ( ) end ,
59- "ecto" => fn _ -> SQL.Repo . ecto ( ) end ,
53+ "sql" => fn -> SQL.Repo . sql ( ) end ,
54+ "ecto" => fn -> SQL.Repo . ecto ( ) end ,
6055 # "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,
6156 # "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,
6257 # "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,
6358 # "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,
6459 # "comptime ecto" => fn _ -> SQL.Repo.to_sql(:all, query) end
6560 } ,
66- parallel: 10 , time: 1 ,
67- inputs: % { "1..100_000" => Enum . to_list ( 1 .. 100_000 ) } ,
61+ parallel: 50 ,
6862 memory_time: 2 ,
6963 reduction_time: 2 ,
7064 unit_scaling: :smallest ,
7165 measure_function_call_overhead: true ,
72- profile_after: :eprof
66+ # profile_after: :eprof
67+ # profile_after: :cprof
68+ profile_after: :fprof
7369)
0 commit comments