epgsql_perf_tests.erl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. %%%
  2. -module(epgsql_perf_tests).
  3. -include_lib("eunit/include/eunit.hrl").
  4. %%
  5. perf_test_() ->
  6. [
  7. {timeout, 60, prepare_data()},
  8. {timeout, 60, get_data()}
  9. ].
  10. drop_data_test_() ->
  11. drop_data().
  12. %%
  13. -define(noise_size, 10000000).
  14. prepare_data() ->
  15. {"insert blob", with_connection(fun (C) ->
  16. Noise = noise(?noise_size),
  17. {ok, [], []} = epgsql:squery(C, "create table test_big_blobs (id int4 primary key, noise bytea)"),
  18. {ok, 1} = epgsql:equery(C, "insert into test_big_blobs (id, noise) values (1, $1)", [Noise])
  19. end)}.
  20. get_data() ->
  21. {"get blob back", with_connection(fun (C) ->
  22. {ok, _, [{Noise}]} = epgsql:equery(C, "select noise from test_big_blobs"),
  23. ?assertEqual(?noise_size, byte_size(Noise))
  24. end)}.
  25. drop_data() ->
  26. {"cleanup", with_connection(fun (C) ->
  27. {ok, [], []} = epgsql:squery(C, "drop table test_big_blobs")
  28. end)}.
  29. noise(N) ->
  30. crypto:rand_bytes(N).
  31. %%
  32. -define(host, "localhost").
  33. -define(port, 10432).
  34. with_connection(F) ->
  35. with_connection(F, "epgsql_test", []).
  36. with_connection(F, Username, Args) ->
  37. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  38. fun () ->
  39. {ok, C} = epgsql:connect(?host, Username, Args2),
  40. try
  41. F(C)
  42. after
  43. epgsql:close(C)
  44. end
  45. end.