lithium.cc 6.1 KB

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