Skip to content

Conversation

ruslandoga
Copy link
Contributor

Ideas for #259

end,
"custom multipart encode" => fn input ->
DBConnection.Query.Ch.Query.custom_multipart_encode(input.query, input.params, input.opts)
end
Copy link
Contributor Author

@ruslandoga ruslandoga Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results
$ env MIX_ENV=bench mix run bench/query.exs
Operating System: macOS
CPU Information: Apple M2
Number of Available Cores: 8
Available memory: 8 GB
Elixir 1.18.4
Erlang 28.0.1
JIT enabled: true

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: 0 params, 1 named param, 10 named params, 10 positional params, 100 positional params
Estimated total run time: 1 min 45 s

Benchmarking custom multipart encode with input 0 params ...
Benchmarking custom multipart encode with input 1 named param ...
Benchmarking custom multipart encode with input 10 named params ...
Benchmarking custom multipart encode with input 10 positional params ...
Benchmarking custom multipart encode with input 100 positional params ...
Benchmarking multipart encode with input 0 params ...
Benchmarking multipart encode with input 1 named param ...
Benchmarking multipart encode with input 10 named params ...
Benchmarking multipart encode with input 10 positional params ...
Benchmarking multipart encode with input 100 positional params ...
Benchmarking query string encode with input 0 params ...
Benchmarking query string encode with input 1 named param ...
Benchmarking query string encode with input 10 named params ...
Benchmarking query string encode with input 10 positional params ...
Benchmarking query string encode with input 100 positional params ...
Calculating statistics...
Formatting results...

##### With input 0 params #####
Name                              ips        average  deviation         median         99th %
query string encode           11.23 M       89.07 ns ±42180.35%          42 ns          84 ns
custom multipart encode        1.59 M      627.63 ns  ±3688.39%         500 ns         916 ns
multipart encode               0.48 M     2076.18 ns   ±729.98%        1625 ns        3792 ns

Comparison:
query string encode           11.23 M
custom multipart encode        1.59 M - 7.05x slower +538.55 ns
multipart encode               0.48 M - 23.31x slower +1987.11 ns

##### With input 1 named param #####
Name                              ips        average  deviation         median         99th %
query string encode            6.02 M      166.10 ns ±18451.53%         125 ns         250 ns
custom multipart encode        1.40 M      715.88 ns  ±2991.42%         583 ns         792 ns
multipart encode               0.33 M     3041.13 ns   ±475.63%        2418 ns        5709 ns

Comparison:
query string encode            6.02 M
custom multipart encode        1.40 M - 4.31x slower +549.77 ns
multipart encode               0.33 M - 18.31x slower +2875.02 ns

##### With input 10 named params #####
Name                              ips        average  deviation         median         99th %
query string encode         1900.51 K        0.53 μs  ±4765.09%        0.46 μs        0.67 μs
custom multipart encode      454.49 K        2.20 μs   ±462.59%        1.75 μs        4.58 μs
multipart encode              82.65 K       12.10 μs    ±38.17%       11.33 μs       33.46 μs

Comparison:
query string encode         1900.51 K
custom multipart encode      454.49 K - 4.18x slower +1.67 μs
multipart encode              82.65 K - 22.99x slower +11.57 μs

##### With input 10 positional params #####
Name                              ips        average  deviation         median         99th %
query string encode         1364.13 K        0.73 μs  ±2293.63%        0.71 μs        0.96 μs
custom multipart encode      962.79 K        1.04 μs  ±1354.36%        0.88 μs        1.38 μs
multipart encode              86.87 K       11.51 μs    ±59.70%          10 μs       37.06 μs

Comparison:
query string encode         1364.13 K
custom multipart encode      962.79 K - 1.42x slower +0.31 μs
multipart encode              86.87 K - 15.70x slower +10.78 μs

##### With input 100 positional params #####
Name                              ips        average  deviation         median         99th %
custom multipart encode      207.53 K        4.82 μs   ±120.68%        4.33 μs        9.63 μs
query string encode          132.51 K        7.55 μs   ±404.52%        7.25 μs       10.42 μs
multipart encode               8.45 K      118.36 μs    ±19.60%      113.04 μs      203.48 μs

Comparison:
custom multipart encode      207.53 K
query string encode          132.51 K - 1.57x slower +2.73 μs
multipart encode               8.45 K - 24.56x slower +113.54 μs

@@ -208,6 +244,59 @@ defimpl DBConnection.Query, for: Ch.Query do
end
end

defp multipart_params(params, boundary) when is_map(params) do
multipart_named_params(Map.to_list(params), boundary, [])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make acc default to [] ? cleaner implementation imo

@hawkyre
Copy link

hawkyre commented Jul 23, 2025

This looks great, what are the benchmarks saying? Is the custom impl faster?

@ruslandoga
Copy link
Contributor Author

Yes, the custom impl seems to be a bit faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants