Skip to content

Commit 06e12da

Browse files
committed
chore: Add benchmarks for Mango selector field lookups
1 parent c48db95 commit 06e12da

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

src/mango/src/mango_selector.erl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,4 +1703,100 @@ match_nor_test() ->
17031703
?assertEqual(false, match_int(SelMulti, {[{<<"x">>, 9}]})),
17041704
?assertEqual(false, match_int(SelMulti, {[]})).
17051705

1706+
% The benchmarks below can be run be adding the following to the dependencies
1707+
% in rebar.config.script:
1708+
%
1709+
% {argparse, {url, "https://github.com/max-au/argparse"}, "1.2.4"},
1710+
% {erlperf, {url, "https://github.com/max-au/erlperf"}, "2.3.0"}
1711+
%
1712+
% And then, adding `_test` to the end of any benchmark function names will
1713+
% cause them to be run during unit testing.
1714+
1715+
bench(Cases) ->
1716+
Benches = lists:map(
1717+
fun({_, Sel0, Doc}) ->
1718+
Sel = normalize(Sel0),
1719+
#{runner => fun() -> match_int(Sel, Doc) end}
1720+
end,
1721+
Cases
1722+
),
1723+
Results = erlperf:compare(Benches, #{}),
1724+
?debugFmt("", []),
1725+
lists:foreach(
1726+
fun({{Name, _, _}, Result}) ->
1727+
?debugFmt("bench [~s] = ~p", [Name, Result])
1728+
end,
1729+
lists:zip(Cases, Results)
1730+
).
1731+
1732+
field_lookup_benchmark() ->
1733+
bench([
1734+
{
1735+
"1 field",
1736+
{[{<<"a">>, 1}]},
1737+
{[{<<"a">>, 1}]}
1738+
},
1739+
{
1740+
"3 sibling fields",
1741+
{[{<<"a">>, 1}, {<<"b">>, 2}, {<<"c">>, 3}]},
1742+
{[{<<"a">>, 1}, {<<"b">>, 2}, {<<"c">>, 3}]}
1743+
},
1744+
{
1745+
"3 nested fields",
1746+
{[{<<"a">>, {[{<<"b">>, {[{<<"c">>, 1}]}}]}}]},
1747+
{[{<<"a">>, {[{<<"b">>, {[{<<"c">>, 1}]}}]}}]}
1748+
},
1749+
{
1750+
"allMatch: 1 field",
1751+
{[
1752+
{<<"a">>,
1753+
{[
1754+
{<<"$allMatch">>,
1755+
{[
1756+
{<<"b">>, {[{<<"$gt">>, 0}]}}
1757+
]}}
1758+
]}}
1759+
]},
1760+
{[
1761+
{<<"a">>, [{[{<<"b">>, N}]} || N <- lists:seq(1, 10)]}
1762+
]}
1763+
},
1764+
{
1765+
"allMatch: 3 sibling fields",
1766+
{[
1767+
{<<"a">>,
1768+
{[
1769+
{<<"$allMatch">>,
1770+
{[
1771+
{<<"b">>, {[{<<"$gt">>, 0}]}},
1772+
{<<"c">>, {[{<<"$gt">>, 0}]}},
1773+
{<<"d">>, {[{<<"$gt">>, 0}]}}
1774+
]}}
1775+
]}}
1776+
]},
1777+
{[
1778+
{<<"a">>, [
1779+
{[{<<"b">>, N}, {<<"c">>, N}, {<<"d">>, N}]}
1780+
|| N <- lists:seq(1, 10)
1781+
]}
1782+
]}
1783+
},
1784+
{
1785+
"allMatch: 3 nested fields",
1786+
{[
1787+
{<<"a">>,
1788+
{[
1789+
{<<"$allMatch">>,
1790+
{[{<<"b">>, {[{<<"c">>, {[{<<"d">>, {[{<<"$gt">>, 0}]}}]}}]}}]}}
1791+
]}}
1792+
]},
1793+
{[
1794+
{<<"a">>, [
1795+
{[{<<"b">>, {[{<<"c">>, {[{<<"d">>, N}]}}]}}]}
1796+
|| N <- lists:seq(1, 10)
1797+
]}
1798+
]}
1799+
}
1800+
]).
1801+
17061802
-endif.

0 commit comments

Comments
 (0)