aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus_test/src/tests/speed/cpp-client.cpp
blob: bfd2faf735e8114fc3ca3c0d8a6394a991d1e3cb (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/messagebus/messagebus.h>
#include <vespa/messagebus/routing/retrytransienterrorspolicy.h>
#include <vespa/messagebus/rpcmessagebus.h>
#include <vespa/messagebus/network/rpcnetworkparams.h>
#include <vespa/messagebus/testlib/simplemessage.h>
#include <vespa/messagebus/testlib/simpleprotocol.h>
#include <vespa/messagebus/testlib/simplereply.h>
#include <vespa/vespalib/util/time.h>
#include <thread>
#include <vespa/vespalib/util/signalhandler.h>

using namespace mbus;
using namespace std::chrono_literals;

class Client : public IReplyHandler
{
private:
    std::mutex        _lock;
    uint32_t          _okCnt;
    uint32_t          _failCnt;
    SourceSession::UP _session;
    static uint64_t   _seq;
public:
    Client(MessageBus &bus, const SourceSessionParams &params);
    ~Client() override;
    void send();
    void send(uint64_t seq);
    void sample(uint32_t &okCnt, uint32_t &failCnt);
    void handleReply(Reply::UP reply) override;
};
uint64_t Client::_seq = 100000;

Client::Client(MessageBus &bus, const SourceSessionParams &params)
    : _lock(),
      _okCnt(0),
      _failCnt(0),
      _session(bus.createSourceSession(*this, params))
{
}

Client::~Client()
{
    _session->close();
}

void
Client::send() {
    send(++_seq);
}

void
Client::send(uint64_t seq) {
    Message::UP msg(new SimpleMessage("message", true, seq));
    _session->send(std::move(msg), "test");
}

void
Client::sample(uint32_t &okCnt, uint32_t &failCnt) {
    std::lock_guard guard(_lock);
    okCnt = _okCnt;
    failCnt = _failCnt;
}

void
Client::handleReply(Reply::UP reply) {
    if ((reply->getProtocol() == SimpleProtocol::NAME)
        && (reply->getType() == SimpleProtocol::REPLY)
        && (static_cast<SimpleReply&>(*reply).getValue() == "OK"))
    {
        std::lock_guard guard(_lock);
        ++_okCnt;
    } else {
        fprintf(stderr, "BAD REPLY\n");
        for (uint32_t i = 0; i < reply->getNumErrors(); ++i) {
            fprintf(stderr, "ERR[%d]: code=%d, msg=%s\n", i,
                    reply->getError(i).getCode(),
                    reply->getError(i).getMessage().c_str());
        }
        std::lock_guard guard(_lock);
        ++_failCnt;
    }
    send();
}

class App
{
public:
    int main(int argc, char **argv);
};

int
App::main(int, char **)
{
    auto retryPolicy = std::make_shared<RetryTransientErrorsPolicy>();
    retryPolicy->setBaseDelay(0.1);
    RPCMessageBus mb(MessageBusParams().setRetryPolicy(retryPolicy).addProtocol(std::make_shared<SimpleProtocol>()),
                     RPCNetworkParams(config::ConfigUri("file:slobrok.cfg")).setIdentity(Identity("server/cpp")),
                     config::ConfigUri("file:routing.cfg"));
    Client client(mb.getMessageBus(), SourceSessionParams().setTimeout(30s));

    // let the system 'warm up'
    std::this_thread::sleep_for(5s);

    // inject messages into the feedback loop
    for (uint32_t i = 0; i < 1024; ++i) {
        client.send(i);
    }

    // let the system 'warm up'
    std::this_thread::sleep_for(5s);

    vespalib::Timer timer;
    uint32_t okBefore   = 0;
    uint32_t okAfter    = 0;
    uint32_t failBefore = 0;
    uint32_t failAfter  = 0;

    client.sample(okBefore, failBefore);
    std::this_thread::sleep_for(10s); // Benchmark time
    vespalib::duration elapsed = timer.elapsed();
    client.sample(okAfter, failAfter);
    double time = vespalib::count_ms(elapsed);
    double msgCnt = (double)(okAfter - okBefore);
    double throughput = (msgCnt / time) * 1000.0;
    fprintf(stdout, "CPP-CLIENT: %g msg/s\n", throughput);
    if (failAfter > failBefore) {
        fprintf(stderr, "CPP-CLIENT: FAILED (%d -> %d)\n", failBefore, failAfter);
        return 1;
    }
    return 0;
}

int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    fprintf(stderr, "started '%s'\n", argv[0]);
    fflush(stderr);
    App app;
    int r = app.main(argc, argv);
    fprintf(stderr, "stopping '%s'\n", argv[0]);
    fflush(stderr);
    return r;
}