Last active
March 4, 2016 00:48
-
-
Save yjh0502/ecb9c899fbbb020b4e33 to your computer and use it in GitHub Desktop.
simple epgsql bench
This file contains 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(epgsql_bench). | |
-export([run/2]). | |
-define(HOST, ""). | |
-define(PORT, ""). | |
-define(USER, ""). | |
-define(PW, ""). | |
-define(DB, ""). | |
run(N, QueueLen) -> | |
Ref = epgsqla:connect(?HOST, ?USER, ?PW, [{database, ?DB}, {port, ?PORT}]), | |
{C, Ref, connected} = receive A -> A end, | |
{Time, _} = timer:tc(fun() -> | |
loop(C, N, QueueLen) | |
end), | |
io:format("~p ~p ops/s~n", [Time, N * 1000000 / Time]), | |
epgsqla:close(C), | |
ok. | |
loop(C, N, QueueLen) -> | |
send(C, QueueLen), | |
loop(C, N, QueueLen, 0). | |
loop(_C, N, N, N) -> | |
ok; | |
loop(C, N, Sent, Recv) -> | |
RecvCount = recv(), | |
SendCount = min(N-Sent, RecvCount), | |
send(C, SendCount), | |
loop(C, N, Sent + SendCount, Recv + RecvCount). | |
send(_C, 0) -> | |
ok; | |
send(C, N) -> | |
send(C), | |
send(C, N-1). | |
send(C) -> | |
Name = base64:encode(crypto:strong_rand_bytes(10)), | |
epgsqla:parse(C, Name, <<"select 1">>, []). | |
recv() -> | |
recv_stmt(0, 1000). | |
recv_stmt(Count, Timeout) -> | |
receive | |
{C, _, {ok, {statement, _, _, _} = Stmt}} -> | |
epgsqla:equery(C, Stmt, []), | |
recv_stmt(Count, 1000); | |
_Resp -> | |
recv_stmt(Count+1, 0) | |
after Timeout -> | |
Count | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revised version with prepared_query/3