-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_http.cpp
82 lines (73 loc) · 2.32 KB
/
bench_http.cpp
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
//
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/net/HttpOutboundConnection.h"
#include "td/net/HttpQuery.h"
#include "td/net/SslStream.h"
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/utils/buffer.h"
#include "td/utils/BufferedFd.h"
#include "td/utils/logging.h"
#include "td/utils/port/IPAddress.h"
#include "td/utils/port/SocketFd.h"
#include "td/utils/Status.h"
#include <atomic>
#include <limits>
namespace td {
std::atomic<int> counter;
class HttpClient final : public HttpOutboundConnection::Callback {
void start_up() final {
IPAddress addr;
addr.init_ipv4_port("127.0.0.1", 8082).ensure();
auto fd = SocketFd::open(addr);
LOG_CHECK(fd.is_ok()) << fd.error();
connection_ = create_actor<HttpOutboundConnection>("Connect", BufferedFd<SocketFd>(fd.move_as_ok()), SslStream{},
std::numeric_limits<size_t>::max(), 0, 0,
ActorOwn<HttpOutboundConnection::Callback>(actor_id(this)));
yield();
cnt_ = 100000;
counter++;
}
void tear_down() final {
if (--counter == 0) {
Scheduler::instance()->finish();
}
}
void loop() final {
if (cnt_-- < 0) {
return stop();
}
send_closure(connection_, &HttpOutboundConnection::write_next, BufferSlice("GET / HTTP/1.1\r\n\r\n"));
send_closure(connection_, &HttpOutboundConnection::write_ok);
LOG(INFO) << "SEND";
}
void handle(unique_ptr<HttpQuery> result) final {
loop();
}
void on_connection_error(Status error) final {
LOG(ERROR) << "ERROR: " << error;
}
ActorOwn<HttpOutboundConnection> connection_;
int cnt_;
};
int main() {
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
auto scheduler = make_unique<ConcurrentScheduler>();
scheduler->init(0);
scheduler->create_actor_unsafe<HttpClient>(0, "Client1").release();
scheduler->create_actor_unsafe<HttpClient>(0, "Client2").release();
scheduler->start();
while (scheduler->run_main(10)) {
// empty
}
scheduler->finish();
return 0;
}
} // namespace td
int main() {
return td::main();
}