speed_test_pubsub.cpp
1.29 KB
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
#include <iostream>
#include "redox.hpp"
using namespace std;
using redox::Redox;
using redox::Command;
using redox::Subscriber;
double time_s() {
unsigned long ms = chrono::system_clock::now().time_since_epoch() / chrono::microseconds(1);
return (double)ms / 1e6;
}
int main(int argc, char *argv[]) {
Redox rdx_pub;
rdx_pub.noWait(true);
Subscriber rdx_sub;
rdx_sub.noWait(true);
if(!rdx_pub.connect()) return 1;
if(!rdx_sub.connect()) return 1;
atomic_int count(0);
auto got_message = [&count](const string& topic, const string& msg) {
count += 1;
};
auto subscribed = [](const string& topic) {
cout << "> Subscribed to " << topic << endl;
};
auto unsubscribed = [](const string& topic) {
cout << "> Unsubscribed from " << topic << endl;
};
rdx_sub.subscribe("speedtest", got_message, subscribed, unsubscribed);
double t0 = time_s();
double t1 = t0;
double tspan = 5;
while(t1 - t0 < tspan) {
rdx_pub.publish("speedtest", "hello");
t1 = time_s();
}
this_thread::sleep_for(chrono::milliseconds(10));
double t = t1 - t0;
cout << "Total of messages sent in " << t << "s is " << count << endl;
double msg_per_s = count / t;
cout << "Messages per second: " << msg_per_s << endl;
rdx_sub.disconnect();
rdx_pub.disconnect();
return 0;
}