okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

nekomimi.petopenai_api: own HTTP/1.1 client for https too, keep-alive connection pool, drop gunda0bc9f

main
1.9 KiB47 linesraw
1-module(albedo_openai_bench).
2-export([env/1, measure/1, text/1]).
3
4env(Name) ->
5    case os:getenv(binary_to_list(Name)) of
6        false -> <<>>;
7        Value -> unicode:characters_to_binary(Value)
8    end.
9
10text(Bytes) when Bytes > 0 ->
11    case get(first_text) of undefined -> put(first_text, erlang:monotonic_time(microsecond)); _ -> ok end,
12    put(text_bytes, get(text_bytes) + Bytes),
13    put(text_chunks, get(text_chunks) + 1),
14    nil;
15text(_) -> nil.
16
17measure(Run) ->
18    Owner = self(),
19    Baseline = erlang:memory(binary),
20    {Pid, Ref} = spawn_monitor(fun() ->
21        put(text_bytes, 0), put(text_chunks, 0),
22        Start = erlang:monotonic_time(microsecond),
23        Result = Run(),
24        End = erlang:monotonic_time(microsecond),
25        First = case get(first_text) of undefined -> none; Time -> {some, (Time - Start) / 1000} end,
26        {reductions, Reductions} = process_info(self(), reductions),
27        {memory, Memory} = process_info(self(), memory),
28        Owner ! {self(), measured, Result, (End - Start) / 1000, First,
29            get(text_bytes), get(text_chunks), Memory, Reductions},
30        receive release -> ok end
31    end),
32    sample(Pid, Ref, Baseline, 0, Baseline).
33
34sample(Pid, Ref, Baseline, PeakProcess, PeakBinary) ->
35    receive
36        {Pid, measured, Result, Elapsed, First, Bytes, Chunks, Memory, Reductions} ->
37            Binary = erlang:memory(binary),
38            Pid ! release,
39            demonitor(Ref, [flush]),
40            {Result, {metrics, Elapsed, First, Bytes, Chunks,
41                max(PeakProcess, Memory), max(0, max(PeakBinary, Binary) - Baseline), Reductions}};
42        {'DOWN', Ref, process, Pid, Reason} -> error({benchmark_worker_crashed, Reason})
43    after 10 ->
44        Memory = case process_info(Pid, memory) of {memory, M} -> M; undefined -> 0 end,
45        sample(Pid, Ref, Baseline, max(PeakProcess, Memory), max(PeakBinary, erlang:memory(binary)))
46    end.
47