lithium.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 tune_n_sql_connections(int& nc_to_tune, std::string http_req, int port, int min, int max) {
  24. std::cout << std::endl << "Benchmark " << http_req << std::endl;
  25. auto sockets = http_benchmark_connect(512, port);
  26. float max_req_per_s = 0;
  27. int best_nconn = 2;
  28. for (int i = 0; i <= 7; i++)
  29. {
  30. int nc = min + (max - min) * i / 7;
  31. nc_to_tune = nc;
  32. // Warmup.
  33. http_benchmark(sockets, 4, 200, http_req);
  34. float req_per_s = http_benchmark(sockets, 4, 1000, http_req);
  35. std::cout << nc << " -> " << req_per_s << " req/s." << std::endl;
  36. if (req_per_s > max_req_per_s)
  37. {
  38. max_req_per_s = req_per_s;
  39. best_nconn = nc;
  40. }
  41. }
  42. http_benchmark_close(sockets);
  43. std::cout << "best: " << best_nconn << " (" << max_req_per_s << " req/s)."<< std::endl;
  44. nc_to_tune = best_nconn;
  45. }
  46. int main(int argc, char* argv[]) {
  47. if (argc != 3)
  48. {
  49. std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
  50. return 1;
  51. }
  52. int port = atoi(argv[2]);
  53. int nprocs = std::thread::hardware_concurrency();
  54. #if MONOTHREAD
  55. int nthreads = 1;
  56. #else
  57. int nthreads = nprocs;
  58. #endif
  59. #if TFB_MYSQL
  60. auto sql_db = mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  61. s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
  62. #elif TFB_PGSQL
  63. auto sql_db = pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  64. s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8");
  65. #endif
  66. auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
  67. s::id(s::auto_increment, s::primary_key) = int(),
  68. s::message = std::string());
  69. auto random_numbers = sql_orm_schema(sql_db, "World").fields(
  70. s::id(s::auto_increment, s::primary_key) = int(),
  71. s::randomNumber = int());
  72. #if TFB_MYSQL
  73. int db_nconn = 4;
  74. int queries_nconn = 2;
  75. int fortunes_nconn = 4;
  76. int updates_nconn = 1;
  77. #elif TFB_PGSQL
  78. int db_nconn = 7;
  79. int queries_nconn = 4;
  80. int fortunes_nconn = 7;
  81. int updates_nconn = 3;
  82. #endif
  83. http_api my_api;
  84. my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
  85. response.set_header("Content-Type", "text/plain");
  86. response.write("Hello, World!");
  87. };
  88. my_api.get("/json") = [&](http_request& request, http_response& response) {
  89. response.write_json(s::message = "Hello, World!");
  90. };
  91. my_api.get("/db") = [&](http_request& request, http_response& response) {
  92. sql_db.max_async_connections_per_thread_ = db_nconn;
  93. response.write_json(random_numbers.connect(request.fiber).find_one(s::id = 1 + rand() % 10000).value());
  94. };
  95. my_api.get("/queries") = [&](http_request& request, http_response& response) {
  96. sql_db.max_async_connections_per_thread_ = queries_nconn;
  97. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  98. int N = atoi(N_str.c_str());
  99. N = std::max(1, std::min(N, 500));
  100. auto c = random_numbers.connect(request.fiber);
  101. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  102. for (int i = 0; i < N; i++)
  103. numbers[i] = c.find_one(s::id = 1 + rand() % 10000).value();
  104. response.write_json(numbers);
  105. };
  106. my_api.get("/updates") = [&](http_request& request, http_response& response) {
  107. sql_db.max_async_connections_per_thread_ = updates_nconn;
  108. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  109. int N = atoi(N_str.c_str());
  110. N = std::max(1, std::min(N, 500));
  111. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  112. {
  113. auto c = random_numbers.connect(request.fiber);
  114. auto& raw_c = c.backend_connection();
  115. #if TFB_MYSQL
  116. raw_c("START TRANSACTION");
  117. #endif
  118. for (int i = 0; i < N; i++)
  119. {
  120. numbers[i] = c.find_one(s::id = 1 + rand() % 10000).value();
  121. numbers[i].randomNumber = 1 + rand() % 10000;
  122. }
  123. std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
  124. c.bulk_update(numbers);
  125. #if TFB_MYSQL
  126. raw_c("COMMIT");
  127. #endif
  128. }
  129. response.write_json(numbers);
  130. };
  131. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  132. sql_db.max_async_connections_per_thread_ = fortunes_nconn;
  133. typedef decltype(fortunes.all_fields()) fortune;
  134. std::vector<fortune> table;
  135. auto c = fortunes.connect(request.fiber);
  136. c.forall([&] (auto f) { table.emplace_back(f); });
  137. table.emplace_back(0, "Additional fortune added at request time.");
  138. std::sort(table.begin(), table.end(),
  139. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  140. char b[100000];
  141. li::output_buffer ss(b, sizeof(b));
  142. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  143. for(auto& f : table)
  144. {
  145. ss << "<tr><td>" << f.id << "</td><td>";
  146. escape_html_entities(ss, f.message);
  147. ss << "</td></tr>";
  148. }
  149. ss << "</table></body></html>";
  150. response.set_header("Content-Type", "text/html; charset=utf-8");
  151. response.write(ss.to_string_view());
  152. };
  153. // Start the server for the Techempower benchmark.
  154. http_serve(my_api, port, s::nthreads = nthreads);
  155. return 0;
  156. }