wsload.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. //------------------------------------------------------------------------------
  10. //
  11. // wsload
  12. //
  13. // Measure the performance of a WebSocket server
  14. //
  15. //------------------------------------------------------------------------------
  16. #include <boost/beast/core.hpp>
  17. #include <boost/beast/websocket.hpp>
  18. #include <boost/beast/_experimental/unit_test/dstream.hpp>
  19. #include <boost/asio.hpp>
  20. #include <atomic>
  21. #include <chrono>
  22. #include <cstdlib>
  23. #include <functional>
  24. #include <iostream>
  25. #include <memory>
  26. #include <mutex>
  27. #include <random>
  28. #include <thread>
  29. #include <vector>
  30. namespace beast = boost::beast; // from <boost/beast.hpp>
  31. namespace http = beast::http; // from <boost/beast/http.hpp>
  32. namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
  33. namespace net = boost::asio; // from <boost/asio.hpp>
  34. using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
  35. class test_buffer
  36. {
  37. char data_[4096];
  38. net::const_buffer b_;
  39. public:
  40. using const_iterator =
  41. net::const_buffer const*;
  42. using value_type = net::const_buffer;
  43. test_buffer()
  44. : b_(data_, sizeof(data_))
  45. {
  46. std::mt19937_64 rng;
  47. std::uniform_int_distribution<unsigned short> dist;
  48. for(auto& c : data_)
  49. c = static_cast<unsigned char>(dist(rng));
  50. }
  51. const_iterator
  52. begin() const
  53. {
  54. return &b_;
  55. }
  56. const_iterator
  57. end() const
  58. {
  59. return begin() + 1;
  60. }
  61. };
  62. class report
  63. {
  64. std::mutex m_;
  65. std::size_t bytes_ = 0;
  66. std::size_t messages_ = 0;
  67. public:
  68. void
  69. insert(std::size_t messages, std::size_t bytes)
  70. {
  71. std::lock_guard<std::mutex> lock(m_);
  72. bytes_ += bytes;
  73. messages_ += messages;
  74. }
  75. std::size_t
  76. bytes() const
  77. {
  78. return bytes_;
  79. }
  80. std::size_t
  81. messages() const
  82. {
  83. return messages_;
  84. }
  85. };
  86. void
  87. fail(beast::error_code ec, char const* what)
  88. {
  89. std::cerr << what << ": " << ec.message() << "\n";
  90. }
  91. class connection
  92. : public std::enable_shared_from_this<connection>
  93. {
  94. websocket::stream<tcp::socket> ws_;
  95. tcp::endpoint ep_;
  96. std::size_t messages_;
  97. report& rep_;
  98. test_buffer const& tb_;
  99. net::strand<
  100. net::io_context::executor_type> strand_;
  101. beast::flat_buffer buffer_;
  102. std::mt19937_64 rng_;
  103. std::size_t count_ = 0;
  104. std::size_t bytes_ = 0;
  105. public:
  106. connection(
  107. net::io_context& ioc,
  108. tcp::endpoint const& ep,
  109. std::size_t messages,
  110. bool deflate,
  111. report& rep,
  112. test_buffer const& tb)
  113. : ws_(ioc)
  114. , ep_(ep)
  115. , messages_(messages)
  116. , rep_(rep)
  117. , tb_(tb)
  118. , strand_(ioc.get_executor())
  119. {
  120. websocket::permessage_deflate pmd;
  121. pmd.client_enable = deflate;
  122. ws_.set_option(pmd);
  123. ws_.binary(true);
  124. ws_.auto_fragment(false);
  125. ws_.write_buffer_bytes(64 * 1024);
  126. }
  127. ~connection()
  128. {
  129. rep_.insert(count_, bytes_);
  130. }
  131. void
  132. run()
  133. {
  134. ws_.next_layer().async_connect(ep_,
  135. beast::bind_front_handler(
  136. &connection::on_connect,
  137. this->shared_from_this()));
  138. }
  139. private:
  140. void
  141. on_connect(beast::error_code ec)
  142. {
  143. if(ec)
  144. return fail(ec, "on_connect");
  145. ws_.async_handshake(
  146. ep_.address().to_string() + ":" + std::to_string(ep_.port()),
  147. "/",
  148. beast::bind_front_handler(
  149. &connection::on_handshake,
  150. this->shared_from_this()));
  151. }
  152. void
  153. on_handshake(beast::error_code ec)
  154. {
  155. if(ec)
  156. return fail(ec, "handshake");
  157. do_write();
  158. }
  159. void
  160. do_write()
  161. {
  162. std::geometric_distribution<std::size_t> dist{
  163. double(4) / beast::buffer_bytes(tb_)};
  164. ws_.async_write_some(true,
  165. beast::buffers_prefix(dist(rng_), tb_),
  166. beast::bind_front_handler(
  167. &connection::on_write,
  168. this->shared_from_this()));
  169. }
  170. void
  171. on_write(beast::error_code ec, std::size_t)
  172. {
  173. if(ec)
  174. return fail(ec, "write");
  175. if(messages_--)
  176. return do_read();
  177. ws_.async_close({},
  178. beast::bind_front_handler(
  179. &connection::on_close,
  180. this->shared_from_this()));
  181. }
  182. void
  183. do_read()
  184. {
  185. ws_.async_read(buffer_,
  186. beast::bind_front_handler(
  187. &connection::on_read,
  188. this->shared_from_this()));
  189. }
  190. void
  191. on_read(beast::error_code ec, std::size_t)
  192. {
  193. if(ec)
  194. return fail(ec, "read");
  195. ++count_;
  196. bytes_ += buffer_.size();
  197. buffer_.consume(buffer_.size());
  198. do_write();
  199. }
  200. void
  201. on_close(beast::error_code ec)
  202. {
  203. if(ec)
  204. return fail(ec, "close");
  205. }
  206. };
  207. class timer
  208. {
  209. using clock_type =
  210. std::chrono::system_clock;
  211. clock_type::time_point when_;
  212. public:
  213. using duration =
  214. clock_type::duration;
  215. timer()
  216. : when_(clock_type::now())
  217. {
  218. }
  219. duration
  220. elapsed() const
  221. {
  222. return clock_type::now() - when_;
  223. }
  224. };
  225. inline
  226. std::uint64_t
  227. throughput(
  228. std::chrono::duration<double> const& elapsed,
  229. std::uint64_t items)
  230. {
  231. using namespace std::chrono;
  232. return static_cast<std::uint64_t>(
  233. 1 / (elapsed/items).count());
  234. }
  235. int
  236. main(int argc, char** argv)
  237. {
  238. beast::unit_test::dstream dout(std::cerr);
  239. try
  240. {
  241. // Check command line arguments.
  242. if(argc != 8)
  243. {
  244. std::cerr <<
  245. "Usage: bench-wsload <address> <port> <trials> <messages> <workers> <threads> <compression:0|1>";
  246. return EXIT_FAILURE;
  247. }
  248. auto const address = net::ip::make_address(argv[1]);
  249. auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
  250. auto const trials = static_cast<std::size_t>(std::atoi(argv[3]));
  251. auto const messages= static_cast<std::size_t>(std::atoi(argv[4]));
  252. auto const workers = static_cast<std::size_t>(std::atoi(argv[5]));
  253. auto const threads = static_cast<std::size_t>(std::atoi(argv[6]));
  254. auto const deflate = std::atoi(argv[7]) != 0;
  255. auto const work = (messages + workers - 1) / workers;
  256. test_buffer tb;
  257. for(auto i = trials; i != 0; --i)
  258. {
  259. report rep;
  260. net::io_context ioc{1};
  261. for(auto j = workers; j; --j)
  262. {
  263. auto sp =
  264. std::make_shared<connection>(
  265. ioc,
  266. tcp::endpoint{address, port},
  267. work,
  268. deflate,
  269. rep,
  270. tb);
  271. sp->run();
  272. }
  273. timer clock;
  274. std::vector<std::thread> tv;
  275. if(threads > 1)
  276. {
  277. tv.reserve(threads);
  278. tv.emplace_back([&ioc]{ ioc.run(); });
  279. }
  280. ioc.run();
  281. for(auto& t : tv)
  282. t.join();
  283. auto const elapsed = clock.elapsed();
  284. dout <<
  285. throughput(elapsed, rep.bytes()) << " bytes/s in " <<
  286. (std::chrono::duration_cast<
  287. std::chrono::milliseconds>(
  288. elapsed).count() / 1000.) << "ms and " <<
  289. rep.bytes() << " bytes" << std::endl;
  290. }
  291. }
  292. catch(std::exception const& e)
  293. {
  294. std::cerr << "Error: " << e.what() << std::endl;
  295. return EXIT_FAILURE;
  296. }
  297. return EXIT_SUCCESS;
  298. }