forked from majek/dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqptest.erl
executable file
·61 lines (48 loc) · 2.18 KB
/
amqptest.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env escript
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
-include_lib("amqp_client/include/amqp_client.hrl").
-define(COUNT, 20000).
main(_) ->
{ok, Connection} = amqp_connection:start(network,
#amqp_params{host = "localhost"}),
{ok, Channel} = amqp_connection:open_channel(Connection),
{ok, Channel2} = amqp_connection:open_channel(Connection),
#'exchange.declare_ok'{} = amqp_channel:call(Channel, #'exchange.declare'{
exchange = <<"test_direct">>,
type = <<"direct">>}),
#'queue.declare_ok'{} = amqp_channel:call(Channel,
#'queue.declare'{
queue = <<"hello">>,
auto_delete = true}),
#'queue.bind_ok'{} = amqp_channel:call(Channel,
#'queue.bind'{
exchange = <<"test_direct">>,
queue = <<"hello">>,
routing_key = <<"hello">>}),
amqp_channel:subscribe(Channel2, #'basic.consume'{queue = <<"hello">>,
no_ack = true}, self()),
receive
#'basic.consume_ok'{} -> ok
end,
io:format("Start~n"),
T0 = now(),
[amqp_channel:cast(Channel,
#'basic.publish'{
exchange = <<"test_direct">>,
routing_key = <<"hello">>},
#amqp_msg{payload = <<"Hello World!">>})
|| X <- lists:seq(0, ?COUNT-1)],
loop(Channel2, ?COUNT),
Td = timer:now_diff(now(), T0) / 1000000.0,
io:format("Done ~.3fsec ~.3f MPS~n", [Td, ?COUNT/Td]),
ok = amqp_channel:close(Channel),
ok = amqp_channel:close(Channel2),
ok = amqp_connection:close(Connection),
ok.
loop(Channel, 0) ->
ok;
loop(Channel, Cnt) ->
receive
{#'basic.deliver'{}, #amqp_msg{payload = Body}} ->
loop(Channel, Cnt-1)
end.