Created
January 28, 2025 00:53
-
-
Save nickva/f2175a6bee8399f7f9ed38d5e9d3be5d to your computer and use it in GitHub Desktop.
Benchmark couch work queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(bench). | |
-export([main/1]). | |
producer(Q) -> | |
receive | |
{produce, Pid, N, Size} -> | |
{ok, Dt} = produce(Q, N, erlang:monotonic_time(), Size), | |
Pid ! {produced, Dt}, | |
producer(Q) | |
end. | |
produce(_Q, 0, T0, _Size) -> | |
{ok, erlang:monotonic_time() - T0}; | |
produce(Q, N, T0, Size) when is_integer(N), N>0 -> | |
TItem = erlang:monotonic_time(), | |
ok = couch_work_queue:queue(Q, {TItem, lists:duplicate(Size, {[<<"x">>, <<"y">>]})}), | |
garbage_collect(), | |
produce(Q, N - 1, T0, Size). | |
consumer(Q) -> | |
receive | |
{consume, Pid, N, MaxSize} -> | |
{ok, Dt, DtItemMin, DtItemMax} = consume(Q, N, MaxSize, undefined, 1 bsl 63, 0), | |
Pid ! {consumed, Dt, DtItemMin, DtItemMax}, | |
consumer(Q) | |
end. | |
consume(_Q, 0, _MaxSize, T0, DtItemMin, DtItemMax) -> | |
{ok, erlang:monotonic_time() - T0, DtItemMin, DtItemMax}; | |
consume(Q, N, MaxSize, T0, DtItemMin, DtItemMax) -> | |
{ok, Items} = couch_work_queue:dequeue(Q, MaxSize), | |
TNow = erlang:monotonic_time(), | |
T = case T0 of | |
undefined -> TNow; | |
_ -> T0 | |
end, | |
{DtItemMin1, DtItemMax1} = lists:foldl(fun({T0Item, Item}, {AccMin, AccMax}) -> | |
_ = Item, | |
DtItem = TNow - T0Item, | |
{min(DtItem, AccMin), max(DtItem, AccMax)} | |
end, {DtItemMin, DtItemMax}, Items), | |
consume(Q, N - length(Items), MaxSize, T, DtItemMin1, DtItemMax1). | |
main({N, MaxSize, MaxItems, DequeueSize, Size}) -> | |
{ok, Q} = couch_work_queue:new([{max_size, MaxSize}, {max_items, MaxItems}]), | |
QRef = monitor(process, Q), | |
%unlink(Q), | |
{Producer, PRef} = spawn_monitor(fun() -> producer(Q) end), | |
{Consumer, CRef} = spawn_monitor(fun() -> consumer(Q) end), | |
timer:sleep(1000), | |
Consumer ! {consume, self(), N, DequeueSize}, | |
Producer ! {produce, self(), N, Size}, | |
ProducerDt = receive {produced, Dt} -> Dt end, | |
{ConsumerDt, IMinDt, IMaxDt} = receive {consumed, CDt, Min, Max} -> {CDt, Min, Max} end, | |
ok = couch_work_queue:close(Q), | |
exit(Producer, kill), | |
exit(Consumer, kill), | |
receive {'DOWN', QRef, _, _, _} -> ok end, | |
receive {'DOWN', PRef, _, _, _} -> ok end, | |
receive {'DOWN', CRef, _, _, _} -> ok end, | |
#{ | |
p_dt => erlang:convert_time_unit(ProducerDt, native, microsecond), | |
c_dt => erlang:convert_time_unit(ConsumerDt, native, microsecond), | |
imin_dt => erlang:convert_time_unit(IMinDt, native, microsecond), | |
imax_dt => erlang:convert_time_unit(IMaxDt, native, microsecond) | |
}. |
Author
nickva
commented
Jan 28, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment