lithium.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "lithium_http_backend.hh"
  2. #if TFB_MYSQL
  3. #include "lithium_mysql.hh"
  4. #elif TFB_PGSQL
  5. #include "lithium_pgsql.hh"
  6. #endif
  7. #include "symbols.hh"
  8. using namespace li;
  9. template <typename B>
  10. void escape_html_entities(B& buffer, const std::string& data)
  11. {
  12. for(size_t pos = 0; pos != data.size(); ++pos) {
  13. switch(data[pos]) {
  14. case '&': buffer << "&amp;"; break;
  15. case '\"': buffer << "&quot;"; break;
  16. case '\'': buffer << "&apos;"; break;
  17. case '<': buffer << "&lt;"; break;
  18. case '>': buffer << "&gt;"; break;
  19. default: buffer << data[pos]; break;
  20. }
  21. }
  22. }
  23. void set_max_sql_connections_per_thread(int max)
  24. {
  25. #if TFB_MYSQL
  26. li::max_mysql_connections_per_thread = max;
  27. #elif TFB_PGSQL
  28. li::max_pgsql_connections_per_thread = max;
  29. #endif
  30. }
  31. float tune_n_sql_connections(int& nc_to_tune, std::string http_req, int port, int max, int nprocs) {
  32. std::cout << std::endl << "Benchmark " << http_req << std::endl;
  33. float max_req_per_s = 0;
  34. int best_nconn = 2;
  35. for (int nc : {1, 2, 4, 8, 32, 64, 128})
  36. {
  37. if (nc*nprocs >= max) break;
  38. nc_to_tune = nc;
  39. float req_per_s = http_benchmark(256, 1, 300, port, http_req);
  40. std::cout << nc << " -> " << req_per_s << " req/s." << std::endl;
  41. if (req_per_s > max_req_per_s)
  42. {
  43. max_req_per_s = req_per_s;
  44. best_nconn = nc;
  45. }
  46. }
  47. std::cout << "best: " << best_nconn << " (" << max_req_per_s << " req/s)."<< std::endl;
  48. nc_to_tune = best_nconn;
  49. return best_nconn;
  50. }
  51. int main(int argc, char* argv[]) {
  52. if (argc != 3)
  53. {
  54. std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
  55. return 1;
  56. }
  57. int port = atoi(argv[2]);
  58. int nprocs = std::thread::hardware_concurrency();
  59. #if TFB_MYSQL
  60. auto sql_db =
  61. mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  62. s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
  63. int sql_max_connection = sql_db.connect()("SELECT @@GLOBAL.max_connections;").template read<int>() - 10;
  64. #elif TFB_PGSQL
  65. auto sql_db =
  66. pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  67. s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8");
  68. int sql_max_connection = atoi(sql_db.connect()("SHOW max_connections;").template read<std::string>().c_str()) - 10;
  69. #endif
  70. auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
  71. s::id(s::auto_increment, s::primary_key) = int(),
  72. s::message = std::string());
  73. auto random_numbers = sql_orm_schema(sql_db, "World").fields(
  74. s::id(s::auto_increment, s::primary_key) = int(),
  75. s::randomNumber = int());
  76. int db_nconn = 64;
  77. int queries_nconn = 64;
  78. int fortunes_nconn = 64;
  79. int updates_nconn = 64;
  80. http_api my_api;
  81. my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
  82. response.set_header("Content-Type", "text/plain");
  83. response.write("Hello, World!");
  84. };
  85. my_api.get("/json") = [&](http_request& request, http_response& response) {
  86. response.write_json(s::message = "Hello, World!");
  87. };
  88. my_api.get("/db") = [&](http_request& request, http_response& response) {
  89. set_max_sql_connections_per_thread(db_nconn);
  90. response.write_json(random_numbers.connect(request.fiber).find_one(s::id = 1234).value());
  91. };
  92. my_api.get("/queries") = [&](http_request& request, http_response& response) {
  93. set_max_sql_connections_per_thread(queries_nconn);
  94. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  95. int N = atoi(N_str.c_str());
  96. N = std::max(1, std::min(N, 500));
  97. auto c = random_numbers.connect(request.fiber);
  98. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  99. for (int i = 0; i < N; i++)
  100. numbers[i] = c.find_one(s::id = 1 + rand() % 10000).value();
  101. response.write_json(numbers);
  102. };
  103. my_api.get("/updates") = [&](http_request& request, http_response& response) {
  104. set_max_sql_connections_per_thread(updates_nconn);
  105. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  106. int N = atoi(N_str.c_str());
  107. N = std::max(1, std::min(N, 500));
  108. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  109. {
  110. auto c = random_numbers.connect(request.fiber);
  111. auto& raw_c = c.backend_connection();
  112. #if TFB_MYSQL
  113. raw_c("START TRANSACTION");
  114. #endif
  115. for (int i = 0; i < N; i++)
  116. {
  117. numbers[i] = c.find_one(s::id = 1 + rand() % 10000).value();
  118. numbers[i].randomNumber = 1 + rand() % 10000;
  119. }
  120. std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
  121. c.bulk_update(numbers);
  122. #if TFB_MYSQL
  123. raw_c("COMMIT");
  124. #endif
  125. }
  126. response.write_json(numbers);
  127. };
  128. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  129. set_max_sql_connections_per_thread(fortunes_nconn);
  130. typedef decltype(fortunes.all_fields()) fortune;
  131. std::vector<fortune> table;
  132. auto c = fortunes.connect(request.fiber);
  133. c.forall([&] (auto f) { table.emplace_back(f); });
  134. table.emplace_back(0, "Additional fortune added at request time.");
  135. std::sort(table.begin(), table.end(),
  136. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  137. char b[100000];
  138. li::output_buffer ss(b, sizeof(b));
  139. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  140. for(auto& f : table)
  141. {
  142. ss << "<tr><td>" << f.id << "</td><td>";
  143. escape_html_entities(ss, f.message);
  144. ss << "</td></tr>";
  145. }
  146. ss << "</table></body></html>";
  147. response.set_header("Content-Type", "text/html; charset=utf-8");
  148. response.write(ss.to_string_view());
  149. };
  150. // Tune the number of sql connections.
  151. int tunning_port = port+1;
  152. std::thread server_thread([&] {
  153. http_serve(my_api, tunning_port, s::nthreads = nprocs);
  154. });
  155. usleep(3e5);
  156. tune_n_sql_connections(db_nconn, "GET /db HTTP/1.1\r\n\r\n", tunning_port, sql_max_connection, nprocs);
  157. tune_n_sql_connections(queries_nconn, "GET /queries?N=20 HTTP/1.1\r\n\r\n", tunning_port, sql_max_connection, nprocs);
  158. tune_n_sql_connections(fortunes_nconn, "GET /fortunes HTTP/1.1\r\n\r\n", tunning_port, sql_max_connection, nprocs);
  159. tune_n_sql_connections(updates_nconn, "GET /updates?N=20 HTTP/1.1\r\n\r\n", tunning_port, sql_max_connection, nprocs);
  160. li::quit_signal_catched = true;
  161. server_thread.join();
  162. li::quit_signal_catched = false;
  163. // Start the server for the Techempower benchmark.
  164. http_serve(my_api, port, s::nthreads = nprocs);
  165. return 0;
  166. }