File: ping.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (61 lines) | stat: -rw-r--r-- 1,831 bytes parent folder | download | duplicates (2)
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
61
:- module(ping,
          []).
:- use_module(library(stomp)).
:- use_module(library(main)).
:- use_module(library(option)).

/** <module> A simple demo and performance evaluation

To run, update the connection details below in this file and in pong.pl.
Now run in one terminal

    swipl pong.pl

and in another one e.g.,

    time swipl ping.pl --count=1000

Timing on Ubuntu 20.04  on  an  AMD3950X   system  is  about  0.4ms  per
iteration. Note that each iteration implies   two  exchanges as the pong
part just echos without decrementing. This   settles  a send and receive
and about 0.2ms. The server  is  RabbitMQ   on  Ubuntu  using  the STOMP
plugin.

Note    that    this     compare     fairly      poorly     to     e.g.,
[rclswi](https://github.com/SWI-Prolog/rclswi),    using    the     ROS2
middleware which performs roughly 7 times better.

@license This code is in the public domain
*/

:- initialization(main, main).

main(Argv) :-
    connect(Connection),
    argv_options(Argv, _, Options),
    option(count(Count), Options, 10),
    stomp_send_json(Connection, '/queue/pong', _{}, _{pong:Count}),
    thread_get_message(_).


connect(Connection) :-
    stomp_connection('127.0.0.1':32772,
                     '/',
                     _{'heart-beat': '5000,5000',
                       login: guest,
                       passcode: guest
                      },
                     on_frame, Connection),
    stomp_connect(Connection).

on_frame(connected, Connection, _Header, _Body) :-
    stomp_subscribe(Connection, '/queue/ping', 0, _{}).
on_frame(message, Connection, _Header, _{ping:Count}) :-
    debug(ping, 'Got ~D', [Count]),
    (   Count == 0
    ->  thread_send_message(main, done)
    ;   Count1 is Count-1,
        stomp_send_json(Connection, '/queue/pong', _{}, _{pong:Count1})
    ).