Current section
Files
Jump to
Current section
Files
src/bench@query_bench.erl
-module(bench@query_bench).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/bench/query_bench.gleam").
-export([main/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/bench/query_bench.gleam", 18).
?DOC(" Create a simple user schema for benchmarking\n").
-spec user_schema() -> cquill@schema:schema().
user_schema() ->
_pipe = cquill@schema:new(<<"users"/utf8>>),
_pipe@2 = cquill@schema:field(
_pipe,
begin
_pipe@1 = cquill@schema@field:integer(<<"id"/utf8>>),
cquill@schema@field:primary_key(_pipe@1)
end
),
_pipe@4 = cquill@schema:field(
_pipe@2,
begin
_pipe@3 = cquill@schema@field:string(<<"email"/utf8>>),
cquill@schema@field:not_null(_pipe@3)
end
),
_pipe@5 = cquill@schema:field(
_pipe@4,
cquill@schema@field:string(<<"name"/utf8>>)
),
_pipe@6 = cquill@schema:field(
_pipe@5,
cquill@schema@field:boolean(<<"active"/utf8>>)
),
_pipe@7 = cquill@schema:field(
_pipe@6,
cquill@schema@field:integer(<<"age"/utf8>>)
),
cquill@schema:single_primary_key(_pipe@7, <<"id"/utf8>>).
-file("src/bench/query_bench.gleam", 29).
?DOC(" Create a more complex schema with many fields\n").
-spec complex_schema() -> cquill@schema:schema().
complex_schema() ->
_pipe = cquill@schema:new(<<"products"/utf8>>),
_pipe@2 = cquill@schema:field(
_pipe,
begin
_pipe@1 = cquill@schema@field:integer(<<"id"/utf8>>),
cquill@schema@field:primary_key(_pipe@1)
end
),
_pipe@5 = cquill@schema:field(
_pipe@2,
begin
_pipe@3 = cquill@schema@field:string(<<"sku"/utf8>>),
_pipe@4 = cquill@schema@field:not_null(_pipe@3),
cquill@schema@field:unique(_pipe@4)
end
),
_pipe@7 = cquill@schema:field(
_pipe@5,
begin
_pipe@6 = cquill@schema@field:string(<<"name"/utf8>>),
cquill@schema@field:not_null(_pipe@6)
end
),
_pipe@8 = cquill@schema:field(
_pipe@7,
cquill@schema@field:string(<<"description"/utf8>>)
),
_pipe@9 = cquill@schema:field(
_pipe@8,
cquill@schema@field:decimal(<<"price"/utf8>>, 10, 2)
),
_pipe@10 = cquill@schema:field(
_pipe@9,
cquill@schema@field:integer(<<"quantity"/utf8>>)
),
_pipe@11 = cquill@schema:field(
_pipe@10,
cquill@schema@field:integer(<<"category_id"/utf8>>)
),
_pipe@12 = cquill@schema:field(
_pipe@11,
cquill@schema@field:integer(<<"brand_id"/utf8>>)
),
_pipe@13 = cquill@schema:field(
_pipe@12,
cquill@schema@field:boolean(<<"active"/utf8>>)
),
_pipe@14 = cquill@schema:field(
_pipe@13,
cquill@schema@field:boolean(<<"featured"/utf8>>)
),
_pipe@15 = cquill@schema:field(
_pipe@14,
cquill@schema@field:datetime(<<"created_at"/utf8>>)
),
_pipe@16 = cquill@schema:field(
_pipe@15,
cquill@schema@field:datetime(<<"updated_at"/utf8>>)
),
cquill@schema:single_primary_key(_pipe@16, <<"id"/utf8>>).
-file("src/bench/query_bench.gleam", 51).
?DOC(" Benchmark simple query construction\n").
-spec bench_simple_query() -> nil.
bench_simple_query() ->
User_schema = user_schema(),
_ = cquill@query:from(User_schema),
nil.
-file("src/bench/query_bench.gleam", 58).
?DOC(" Benchmark query with single where clause\n").
-spec bench_query_with_where() -> nil.
bench_query_with_where() ->
User_schema = user_schema(),
_ = begin
_pipe = cquill@query:from(User_schema),
cquill@query:where(_pipe, cquill@query:eq(<<"active"/utf8>>, true))
end,
nil.
-file("src/bench/query_bench.gleam", 67).
?DOC(" Benchmark query with multiple where clauses\n").
-spec bench_query_with_multiple_where() -> nil.
bench_query_with_multiple_where() ->
User_schema = user_schema(),
_ = begin
_pipe = cquill@query:from(User_schema),
_pipe@1 = cquill@query:where(
_pipe,
cquill@query:eq(<<"active"/utf8>>, true)
),
_pipe@2 = cquill@query:where(
_pipe@1,
cquill@query:gt(<<"age"/utf8>>, 18)
),
cquill@query:where(
_pipe@2,
cquill@query:not_eq(<<"email"/utf8>>, <<""/utf8>>)
)
end,
nil.
-file("src/bench/query_bench.gleam", 78).
?DOC(" Benchmark complex query with many operations\n").
-spec bench_complex_query() -> nil.
bench_complex_query() ->
Product_schema = complex_schema(),
_ = begin
_pipe = cquill@query:from(Product_schema),
_pipe@1 = cquill@query:select(
_pipe,
[<<"id"/utf8>>, <<"sku"/utf8>>, <<"name"/utf8>>, <<"price"/utf8>>]
),
_pipe@2 = cquill@query:where(
_pipe@1,
cquill@query:eq(<<"active"/utf8>>, true)
),
_pipe@3 = cquill@query:where(
_pipe@2,
cquill@query:gt(<<"quantity"/utf8>>, 0)
),
_pipe@4 = cquill@query:where(
_pipe@3,
cquill@query:is_in(<<"category_id"/utf8>>, [1, 2, 3, 4, 5])
),
_pipe@5 = cquill@query:order_by_desc(_pipe@4, <<"created_at"/utf8>>),
_pipe@6 = cquill@query:limit(_pipe@5, 50),
_pipe@7 = cquill@query:offset(_pipe@6, 0),
cquill@query:distinct(_pipe@7)
end,
nil.
-file("src/bench/query_bench.gleam", 94).
?DOC(" Benchmark query with joins\n").
-spec bench_query_with_joins() -> nil.
bench_query_with_joins() ->
Product_schema = complex_schema(),
_ = begin
_pipe = cquill@query:from(Product_schema),
_pipe@1 = cquill@query:join(
_pipe,
<<"categories"/utf8>>,
cquill@query:eq(
<<"products.category_id"/utf8>>,
<<"categories.id"/utf8>>
)
),
_pipe@2 = cquill@query:left_join(
_pipe@1,
<<"brands"/utf8>>,
cquill@query:eq(<<"products.brand_id"/utf8>>, <<"brands.id"/utf8>>)
),
_pipe@3 = cquill@query:select(
_pipe@2,
[<<"products.id"/utf8>>,
<<"products.name"/utf8>>,
<<"categories.name"/utf8>>,
<<"brands.name"/utf8>>]
),
cquill@query:where(
_pipe@3,
cquill@query:eq(<<"products.active"/utf8>>, true)
)
end,
nil.
-file("src/bench/query_bench.gleam", 114).
?DOC(" Benchmark query composition (building up a query step by step)\n").
-spec bench_query_composition() -> nil.
bench_query_composition() ->
User_schema = user_schema(),
Base = cquill@query:from(User_schema),
With_active = begin
_pipe = Base,
cquill@query:where(_pipe, cquill@query:eq(<<"active"/utf8>>, true))
end,
With_age = begin
_pipe@1 = With_active,
cquill@query:where(_pipe@1, cquill@query:gte(<<"age"/utf8>>, 21))
end,
Sorted = begin
_pipe@2 = With_age,
cquill@query:order_by_asc(_pipe@2, <<"name"/utf8>>)
end,
_ = begin
_pipe@3 = Sorted,
cquill@query:paginate(_pipe@3, 1, 20)
end,
nil.
-file("src/bench/query_bench.gleam", 133).
?DOC(" Benchmark OR conditions\n").
-spec bench_or_conditions() -> nil.
bench_or_conditions() ->
User_schema = user_schema(),
_ = begin
_pipe = cquill@query:from(User_schema),
cquill@query:where(
_pipe,
cquill@query:'or'(
[cquill@query:eq(<<"email"/utf8>>, <<"admin@example.com"/utf8>>),
cquill@query:eq(
<<"email"/utf8>>,
<<"super@example.com"/utf8>>
),
cquill@query:'and'(
[cquill@query:eq(<<"active"/utf8>>, true),
cquill@query:gt(<<"age"/utf8>>, 30)]
)]
)
)
end,
nil.
-file("src/bench/query_bench.gleam", 151).
?DOC(" Benchmark BETWEEN condition\n").
-spec bench_between_condition() -> nil.
bench_between_condition() ->
User_schema = user_schema(),
_ = begin
_pipe = cquill@query:from(User_schema),
_pipe@1 = cquill@query:where(
_pipe,
cquill@query:between(<<"age"/utf8>>, 18, 65)
),
cquill@query:where(_pipe@1, cquill@query:eq(<<"active"/utf8>>, true))
end,
nil.
-file("src/bench/query_bench.gleam", 161).
?DOC(" Benchmark LIKE conditions\n").
-spec bench_like_conditions() -> nil.
bench_like_conditions() ->
User_schema = user_schema(),
_ = begin
_pipe = cquill@query:from(User_schema),
_pipe@1 = cquill@query:where(
_pipe,
cquill@query:like(<<"email"/utf8>>, <<"%@example.com"/utf8>>)
),
cquill@query:where(
_pipe@1,
cquill@query:not_like(<<"name"/utf8>>, <<"Test%"/utf8>>)
)
end,
nil.
-file("src/bench/query_bench.gleam", 171).
?DOC(" Benchmark query from table name (no schema)\n").
-spec bench_query_from_table() -> nil.
bench_query_from_table() ->
_ = begin
_pipe = cquill@query:from_table(<<"users"/utf8>>),
_pipe@1 = cquill@query:select(
_pipe,
[<<"id"/utf8>>, <<"name"/utf8>>, <<"email"/utf8>>]
),
_pipe@2 = cquill@query:where(
_pipe@1,
cquill@query:eq(<<"active"/utf8>>, true)
),
cquill@query:limit(_pipe@2, 10)
end,
nil.
-file("src/bench/query_bench.gleam", 185).
?DOC(" Benchmark query inspection operations\n").
-spec bench_query_inspection() -> nil.
bench_query_inspection() ->
User_schema = user_schema(),
Q = begin
_pipe = cquill@query:from(User_schema),
_pipe@1 = cquill@query:where(
_pipe,
cquill@query:eq(<<"active"/utf8>>, true)
),
_pipe@2 = cquill@query:where(
_pipe@1,
cquill@query:gt(<<"age"/utf8>>, 18)
),
_pipe@3 = cquill@query:order_by_asc(_pipe@2, <<"name"/utf8>>),
cquill@query:limit(_pipe@3, 10)
end,
_ = cquill@query:get_conditions(Q),
_ = cquill@query:get_order_bys(Q),
_ = cquill@query:get_select(Q),
_ = cquill@query:get_limit(Q),
_ = cquill@query:has_conditions(Q),
_ = cquill@query:has_pagination(Q),
_ = cquill@query:condition_count(Q),
nil.
-file("src/bench/query_bench.gleam", 205).
?DOC(" Benchmark query debug string generation\n").
-spec bench_query_debug_string() -> nil.
bench_query_debug_string() ->
User_schema = user_schema(),
Q = begin
_pipe = cquill@query:from(User_schema),
_pipe@1 = cquill@query:where(
_pipe,
cquill@query:eq(<<"active"/utf8>>, true)
),
_pipe@2 = cquill@query:where(
_pipe@1,
cquill@query:gt(<<"age"/utf8>>, 18)
),
_pipe@3 = cquill@query:order_by_asc(_pipe@2, <<"name"/utf8>>),
cquill@query:limit(_pipe@3, 10)
end,
_ = cquill@query:to_debug_string(Q),
nil.
-file("src/bench/query_bench.gleam", 223).
?DOC(" Benchmark creating many queries\n").
-spec bench_batch_query_construction() -> nil.
bench_batch_query_construction() ->
User_schema = user_schema(),
_ = begin
_pipe = gleam@list:range(1, 100),
gleam@list:map(
_pipe,
fun(I) -> _pipe@1 = cquill@query:from(User_schema),
cquill@query:where(_pipe@1, cquill@query:eq(<<"id"/utf8>>, I)) end
)
end,
nil.
-file("src/bench/query_bench.gleam", 238).
-spec main() -> nil.
main() ->
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(<<"cquill Query Building Benchmarks"/utf8>>),
gleam_stdlib:println(<<"================================="/utf8>>),
gleam_stdlib:println(<<""/utf8>>),
Config = bench@bench:default_config(),
Suite = bench@bench:run_suite(
<<"Query Construction"/utf8>>,
[{<<"Simple query (from schema)"/utf8>>, fun bench_simple_query/0},
{<<"Query with single WHERE"/utf8>>, fun bench_query_with_where/0},
{<<"Query with multiple WHERE"/utf8>>,
fun bench_query_with_multiple_where/0},
{<<"Complex query (full features)"/utf8>>,
fun bench_complex_query/0},
{<<"Query with JOINs"/utf8>>, fun bench_query_with_joins/0},
{<<"Query composition pipeline"/utf8>>,
fun bench_query_composition/0},
{<<"OR conditions"/utf8>>, fun bench_or_conditions/0},
{<<"BETWEEN condition"/utf8>>, fun bench_between_condition/0},
{<<"LIKE conditions"/utf8>>, fun bench_like_conditions/0},
{<<"Query from table name"/utf8>>, fun bench_query_from_table/0}],
Config
),
Inspection_suite = bench@bench:run_suite(
<<"Query Inspection"/utf8>>,
[{<<"Query inspection ops"/utf8>>, fun bench_query_inspection/0},
{<<"Debug string generation"/utf8>>, fun bench_query_debug_string/0}],
Config
),
Batch_suite = bench@bench:run_suite(
<<"Batch Operations"/utf8>>,
[{<<"Create 100 queries"/utf8>>, fun bench_batch_query_construction/0}],
Config
),
bench@bench:print_suite_summary(Suite),
bench@bench:print_suite_summary(Inspection_suite),
bench@bench:print_suite_summary(Batch_suite),
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(<<"Markdown Output:"/utf8>>),
gleam_stdlib:println(
begin
_pipe = <<"-"/utf8>>,
gleam@string:repeat(_pipe, 60)
end
),
gleam_stdlib:println(bench@bench:to_markdown(Suite)),
gleam_stdlib:println(bench@bench:to_markdown(Inspection_suite)),
gleam_stdlib:println(bench@bench:to_markdown(Batch_suite)).