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